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

JTable.selectAll boundary handling

XMLWordPrintable

    • b16
    • generic
    • generic

      Name: yyT116575 Date: 06/06/2001


      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)


      JTable.selectAll (which has been updated in 1.4beta) doesn't do anything if
      there are no rows or no columns. It should still select all columns if there are
      no rows and the other way round. Otherwise for example isColumnSelected() will
      still return false for all
      columns after calling selectAll() if there happened
      to be no rows. No matter whether there are rows or not, all columns should
      become selected from selectAll() and the same for the rows.

      There is a difference between no rows and no columns (when really nothing can be
      selected), and just no rows or no columns. With JTable, not the data is
      selected, but rows and columns independently. selectAll() should honor that.


      For example:

      Case with no columns
      --------------------
      JTable with row or cell selection, no columns, but some rows
      selectAll() will do nothing
      if then columns are added, nothing will be selected

      This is vastly different behaviour from when there is even single column before
      the call to selectAll()


      Case with no rows
      -----------------
      JTable with column or cell selection, the TableModel happens to have no rows
      selectAll() will do nothing
      if then rows are added, no column will be selected

      if there were rows before the call to selectAll(), now all columns would be
      selected

      The souce code to reproduce the problem:

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

      public class SAT {
        public static void main(String[] args) {

          // TableModel with no rows, but 10 columns
          DefaultTableModel data = new DefaultTableModel(0, 10);

          JTable table = new JTable(data);

          // columns can be selected
          table.setColumnSelectionAllowed(true);
          table.setRowSelectionAllowed(false);

          table.selectAll();

          // After selectAll(), I would expect all columns to be selected, no matter
          // whether there are rows or not.
         // but:
         System.out.println("Column 0 is selected: "+table.isColumnSelected(0));

          // It works as expected when there is a row
          data.addRow(new Object[0]);

          table.selectAll();

          System.out.println("Column 0 is selected: "+table.isColumnSelected(0));
        }
      }


      Suggested change:
      ----------------------------------------------

      Current version:

        public void selectAll() {
          // If I'm currently editing, then I should stop editing
          if (isEditing()) {
            removeEditor();
          }
          if (getRowCount() > 0 && getColumnCount() > 0) {
            setRowSelectionInterval(0, getRowCount()-1);
            setColumnSelectionInterval(0, getColumnCount()-1);
          }
        }

      Suggested version:

        public void selectAll() {
          // If I'm currently editing, then I should stop editing
          if (isEditing()) {
            removeEditor();
          }
          if (getRowCount() > 0)
            setRowSelectionInterval(0, getRowCount()-1);

          if (getColumnCount() > 0)
            setColumnSelectionInterval(0, getColumnCount()-1);
        }
      (Review ID: 125732)
      ======================================================================

            psadhukhan Prasanta Sadhukhan
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: