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

JTable.clearSelection() selects first item

XMLWordPrintable

    • 1.2fcs
    • sparc
    • solaris_2.6
    • Not verified



      Name: tb29552 Date: 10/16/98


      // JDK 1.2beta3 and Swing 1.0.3

      // JTable.clearSelection() clears the selections but then
      // selects the first item (and generates the corresponding event).
      // If there already are no selections then it does not do this.
      // I can clear the selections manually by using ctrl-mousebutton.

      // I have also noticed a similar problem switching between
      // two TableModels using JTable.setModel().

      import java.awt.*;
      import java.awt.event.*;
      import com.sun.java.swing.*;
      import com.sun.java.swing.event.*;
      import com.sun.java.swing.table.*;

      class Test extends JTable {
        class LSListener implements ListSelectionListener {
          public void valueChanged(ListSelectionEvent event) {
            if (Test.this.getSelectedRowCount() > 0)
      System.out.println("* " + event);
            else
      System.out.println("X " + event);
          }
        }

        class TM extends AbstractTableModel {
          private final String[] names = { "foo", "bar" };
          private final String[][] table =
          {
            { "aaa", "one" } ,
            { "bbb", "two" } ,
            { "ccc", "three" } ,
            { "ddd", "four" } ,
            { "eee", "five" }
          } ;
          public int getColumnCount() { return names.length; }
          public int getRowCount() { return table.length; }
          public Object getValueAt(int r, int c) { return table[r][c]; }
          public String getColumnName(int c) { return names[c]; }
          public Class getColumnClass(int c) { return String.class; }
          public boolean isCellEditable(int r, int c) { return false; }
        };
        public Test() {
          TM tm = new TM();
          setModel(tm);
          getSelectionModel().addListSelectionListener(new LSListener());
        }
        static public void main(String argv[]) {
          final Test test = new Test();
          JFrame f = new JFrame("Test");
          JMenuBar menuBar = new JMenuBar();
          JMenu menu = (JMenu)menuBar.add(new JMenu("File"));
          JMenuItem clear = (JMenuItem)menu.add(new JMenuItem("Clear Selections"));
          clear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
      test.clearSelection();
            }});
          f.setJMenuBar(menuBar);
          f.getContentPane().add(new JScrollPane(test), BorderLayout.CENTER);
          f.setSize(200,200);
          f.setVisible(true);
        }
      }
      (Review ID: 39189)
      ======================================================================

            pmilnesunw Philip Milne (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: