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

Multiple selection problem of JTable

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.2.1, 1.2.2
    • client-libs
    • beta
    • generic
    • generic



      Name: rlT66838 Date: 07/25/99


      Steps to reproduce the problem:

      1. Create a JTable and set the selection mode to SINGLE_SELECTION
         ...
         JTable table = new JTable(...);
         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
         ...

      2. Add at least 3 items, the more the better to reproduce the problem, in the table and display this table in a window.

      3. Select an item in the table and see this item highlighted.

      4. Hold down the Control key and move the mouse pointer on the highlighted item. Press down the left mouse button and hold it down. You will see the item unhighlighted.

      5. While holding down the mouse button, and drag the mouse pointer up or down. When the mouse pointer leaves the originally selected item, the originally selected item will be highlighted again. Drag the mouse pointer up and down, and multiple items can be selected, event for SINGLE_SELECTION.


      Java version information:

      java -version
        java version "1.2.2"
        Classic VM (build JDK-1.2.2-W, native threads, symcjit)

      java -fullversion
        java full version "JDK-1.2.2-W"

      It happens on JDK 1.2.1 also.

      ========Test Case======================


      Here is the piece of code that generates the table, run it and
      follow the steps to reproduce the problem. There is no error
      messages, except
      that one does expect multiple items can be highlighted when only
      single
      selection is allowed.

      CODE BEGINS HERE
      ---------------------------------------------------------------------

      //
      // The following piece of code will create a window with a table
      // Normally you can only select one item in the table since a call
      //
      // tree.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      //
      // to make the table to be allowed only for single selection
      // But if you following the steps listed below, you can actully select
      // more than one items in the table.
      //
      // 1. Select an item in the table and see this item highlighted.
      //
      // 2. Hold down the Control key and move the mouse pointer on
      // the highlighted item. Press down the left mouse button
      // and hold it down. You will see the item unhighlighted.
      //
      // 3. While holding down the mouse button, and drag the mouse
      // pointer up or down while holding shift. When the mouse pointer leaves the
      // originally selected item, the originally selected item
      // will be highlighted again. Drag the mouse pointer
      // up and down, and multiple items can be selected,
      // even for SINGLE_SELECTION.
      //

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

      public class JTableProblem extends JFrame {

          class MyDataModel extends AbstractTableModel {
      String[] data = {};
      String[] columnNames = { "String", "String Length" };
      public MyDataModel(String[] items) {
      data = items;
      }
      public int getRowCount() {
      return data.length;
      }
      public int getColumnCount() {
      return 2;
      }
      public Object getValueAt(int row, int col) {
      if (col == 0)
      return data[row];
      else
      return new Integer(data[row].length());
      }
      public String getColumnName(int col) {
      return columnNames[col];
      }
          }

          public JTableProblem(String[] items) {
      super("JTableProblem: Only one item is allowed to be selected");

      JTable table = new JTable(new MyDataModel(items));

      //
      // Setting selection mode to SINGLE_SELECTION
      //
      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

      JScrollPane sp = new JScrollPane(table);
      JPanel p = new JPanel();
      p.setLayout(new BorderLayout());
      p.add(sp, BorderLayout.CENTER);
      getContentPane().setLayout(new BorderLayout());
      getContentPane().add(p, BorderLayout.CENTER);
      addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
      System.exit(0);
      }
      });
          }

          public static void main(String[] args) {
      String[] items = { "One", "Two", "Three", "Four", "Five",
      "Six", "Seven", "Eight", "Nine", "Ten" };
      JTableProblem f = new JTableProblem(items);
      f.pack();
      f.show();
          }
      }

      ---------------------------------------------------------------------
      CODE ENDS HERE

      (Review ID: 88329)
      ======================================================================

      Name: rlT66838 Date: 07/26/99


      Single selection mode works well in a JTable except for one specific case, where the user can still select multiple cells.

      1. Start by creating a JTable
      2. call setSelectionMode(ListSelectionModel.SINGLE_SELECTION)

      Now from the user's standpoint:
      3. select a single row
      4. notice that single selection mode works flawlessly if you try to select another row, or control-select another row, or shift-select another row.
      5. BUT, in one case you can still select multiple cells:
      5a. select a single row again
      5b. now hold the control key
      5c. with the control key still held, start a drag operation inside the selected row. That is, click on the selected row and drag to other cells.

      At this point, on our machines, the JTable has multiple selected rows.

      Bug report 4106272 is identical to this but it is marked closed and fixed, even though it is still present.
      (Review ID: 85706)
      ======================================================================

            pmilnesunw Philip Milne (Inactive)
            rlewis Roger Lewis (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: