3

I have JTable in my swing app. In case if model is empty (row count == 0) I want to show text "0 Results". How can I do this?

K.

KrzyH
  • 4,256
  • 1
  • 31
  • 43
  • 1
    What is the thing you cannot figure out: how to get the row count or how to display the text? Where do you want to display the "0 Results" text? – Dan D. Aug 23 '12 at 10:03
  • 1
    Use a `JLabel`; Take a look a `JLayer` (Java 7) or `JXLayer` (Java 6 or below) – MadProgrammer Aug 23 '12 at 10:09
  • I'm looking for solution similar to GWT CellTable http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/cellview/client/CellTable.html#setEmptyTableWidget(com.google.gwt.user.client.ui.Widget) – KrzyH Aug 23 '12 at 10:19
  • which part of it? Keep in mind that I'm not familiar with gwt, so better explain what exactly you want or reference a screenshot ... – kleopatra Aug 23 '12 at 11:15
  • My link was not truncated :) http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/cellview/client/CellTable.html#setEmptyTableWidget(com.google.gwt.user.client.ui.Widget) CellTable provides method setEmptyTableWidget(Widget widget) which I find very usefull. – KrzyH Aug 24 '12 at 11:41

2 Answers2

3

you can to use and to put text (and or with Icon / ImageIcon) in JLabel to the

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

if you extend AbstractTableModel, as shown in this example, you can check the size of your chosen data structure and condition the overridden methods to return a single row and column having the desired value. For example,

@Override
public Object getValueAt(int row, int col) {
    if (keys.length == 0) {
        return "No results.";
    }
    ...
}

Related changes would be required in the constructor and remaining methods getRowCount(), getColumnCount() and getColumnName().

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • If the JTable has 5 columns, wouldn't you get 'No result' displayed 5 times ? – aymeric Aug 23 '12 at 18:39
  • @aymeric: Only if you do not similarly condition any other overridden methods. The view displays what the model provides. – trashgod Aug 23 '12 at 19:29