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

DefaultTableModel doc confusion between isCellEditable and setValueAt

    Details

    • Subcomponent:
    • Resolved In Build:
      kestrel
    • CPU:
      sparc
    • OS:
      solaris_2.6

      Description

      Confusion between doc and implementation: In the source code
      isCellEditable(int row,int column) will always return true, because we do nothing with row,column values. If a user creates own model extended from
      DefaultTableModel and then they override isCellEditable to always return
      false, when table is visible the view looks correct and the user can't type in new values but they can make model.setValueAt() calls to change the values under the hood. Provided they had some event to set the value. I also understand that in my extended class I can override setValueAt() method to do nothing. The code for setValueAt() in DefaultTableModel should handle the cases where isCellEditable would return false and do nothing.


      Java docs provided:

      public boolean isCellEditable(int row,int column)

            Returns true if the cell at row and column is editable. Otherwise, the setValueAt() on the cell will not
            change the value of that cell.
            Overrides:
                  isCellEditable in class AbstractTableModel
            Parameters:
                  row - the row whose value is to be looked up
                  column - the column whose value is to be looked up
            Returns:
                  true if the cell is editable.
            See Also:
                  setValueAt(java.lang.Object, int, int)



      public void setValueAt(Object aValue,
                             int row,
                             int column)

            Sets the object value for the cell at column and row. aValue is the new value. This method will generate a
            tableChanged() notification.
            Overrides:
                  setValueAt in class AbstractTableModel
            Parameters:
                  aValue - the new value. This can be null.
                  row - the row whose value is to be changed
                  column - the column whose value is to be changed
            Throws:
                  ArrayIndexOutOfBoundsException - if an invalid row or column was given.
      gary.collins@East 1999-11-18

      testcase:

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.table.*;
      import java.util.*;
      import javax.accessibility.*;

      public class SetValueAt extends JFrame {
        static boolean tests = true;
        static int count = 0;
        private int rows=3, cols=5;
        private Object[] rowData = new Object[cols];
        private Vector vRowData = new Vector(cols);
        private MyDefaultTableModel model = new MyDefaultTableModel();
        private JTable table = new JTable(model);
        
        public SetValueAt() {
          for(int c=0; c < cols; ++c)
            model.addColumn("Column " + Integer.toString(c));
          
          for(int r=0; r < rows; ++r) {
            for(int c=0; c < cols; ++c) {
      rowData[c] = "(" + r + "," + c + ")";
            }
            model.addRow(rowData);
          }
          getContentPane().add(new JScrollPane(table),
      BorderLayout.CENTER);
        }
        public static void main(String args[]) {
          SetValueAt t = new SetValueAt();
          t.setLocation(150,150);
          t.setSize(600,350);
          t.setVisible(true);
          // Below 2 lines invokes and ActionEvent. Local actionPerformed method traps call and invokes
          // insertRow call.
          ActionEvent ae = new ActionEvent(t, ActionEvent.ACTION_PERFORMED, "Test 1");
          t.actionPerformed(ae);
        }

        // This method is where the actual test occurs.
        public void actionPerformed(ActionEvent evt) {
          
          String cmd = evt.getActionCommand();
          System.out.println(cmd);

          if (cmd.equals("Test 1")) {
            System.out.println("Is the Cell's editable:="+model.isCellEditable(2,2));
            System.out.println("Cell ValueAt(2,2):="+model.getValueAt(2,2));
            model.setValueAt(model.getValueAt(1,1),2,2);
            System.out.println("Cell ValueAt(2,2) After setValueAt(2,2) call:="+model.getValueAt(2,2));
            count++;
          }
          if (count==2) {
            if(!tests)
      System.exit(1);
            else
      System.exit(0);
          }
        }
      }
      class MyDefaultTableModel extends DefaultTableModel {
        
        public MyDefaultTableModel() {
          super();
        }
        public boolean isCellEditable(int row, int column) {
          return false;
        }

      }

      gary.collins@East 1999-11-18

        Attachments

          Activity

            People

            • Assignee:
              sharonz Sharon Zakhour (Inactive)
              Reporter:
              collins Gary Collins (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: