Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4911092

Can't override getRowCount in DefaultTableModel

    XMLWordPrintable

    Details

    • Subcomponent:
    • Understanding:
      Fix Understood
    • CPU:
      generic
    • OS:
      generic

      Description

      Name: dmR10075 Date: 08/22/2003



      Compile and run the test case below. It exits with NPE in getRowCount.
      Exception in thread "main" java.lang.NullPointerException
              at TestTableModel.getRowCount(TestTableModel.java:29)
              at
      javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:194)
              at
      javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:98)
              at
      javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:116)
              at TestTableModel.<init>(TestTableModel.java:9)
              at TestTableModel.main(TestTableModel.java:46)

      This happens because DefaultTableMode.setDataVector calls getRowCount
      before TestTableModel has initialized its data in constructor. This
      makes getRowCount unusable when overriding DefaultTableModel. Instead,
      setDataVector should have used the value passed into constructor to
      determine the amount of rows during creation. See comments for the diffs
      for suggested change in DefaultTableModel.

      Test:

      import javax.swing.*;
      import javax.swing.table.*;

      public class TestTableModel extends DefaultTableModel {
          private String[] names;
          private String[] values;
          public TestTableModel(String[] names, String[] values) {
              super(new String[]{"Field", "Value"}, names.length);
              this.names = names;
              this.values = values;
          }

          public int getColumnCount() {
              return 2;
          }

          public String getColumnName(int column) {
              switch(column) {
                case 0:
                    return "Field";
                case 1:
                    return "Value";
              }
              throw new IllegalArgumentException("there is no such column: " +
      column);
          }

          public int getRowCount() {
              return names.length;
          }

          public Object getValueAt(int row, int col) {
              switch(col) {
                case 0:
                    return names[row];
                case 1:
                    return values[row];
              }
              throw new IllegalArgumentException("there is no such column: " +
      col);
          }
          public boolean isCellEditable(int row, int col) {
              return false;
          }

          public static void main(String[] args) {
              JTable table = new JTable(new TestTableModel(new String[]
      {"field1"}, new String[] {"value1"}));
          }
      }

      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            domsunw Dom Dom (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Imported:
              Indexed: