Hi!

How should I change the TableModel that extends AbstractTableModel to allow displaying empty data sets? Should the getValueAt method be chaged as follows?

public Object getValueAt(int row, int col) {
        Object str = ((Object[]) cache.elementAt(row))[col];
        if (str != null)
            return str;
        else return "";
      }

...or something else should be define?
Thanks!

Recommended Answers

All 5 Replies

Apply to your JTable method:

jotTable.setFillsViewportHeight(true);

Your method should look typically:

public Object getValueAt(int row, int col) {
        return ((Object[]) cache.elementAt(row))[col];
      }

Use DefaultTableCellRenderer
Here is an example.

DefaultTableCellRenderer LPRenderer = new DefaultTableCellRenderer() {

            @Override
            public void setValue(Object value) {
                int cellValue = (value instanceof Number) ? ((Number) value).intValue() + 1 : 1;
                String disp = (new StringBuilder()).append(" ").append(new Integer(cellValue)).append(" ").toString();
                setText(value != null ? disp : "null");//diplay text "null" if null value
            }

            public void setBackground(Color c) {
                super.setBackground(new Color(213, 213, 213));
            }
        };

LP is one of columns (TableColumn)
LP.setCellRenderer(LPRenderer);

Thank you very much!

But what if I populate JTable with some data from DB? Usually I get a NullPointer error when SQL query returns an empty data set. But I want to get just a blank table. Will jotTable.setFillsViewportHeight(true); do this job?

catch java.lang.NullPointerException
for example

try
{
   data_set = SQL_query.getData();
   table.addData(data_set);
}
catch (NullPointerException nullPointer)
{
   table.addData(NULL_DATA_SET);  // in case of NullPointerException
}

No, jotTable.setFillsViewportHeight(true) has a different purpose (sometimes solve the specific problems of visibility of jotTable in the GUI ).

Some info about exceptions:Effective Java NullPointerException Handling
http://www.javaworld.com/community/node/2833

Thanks a lot! You've helped me so much!

I'm very glad to not less than you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.