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

Removing a Table Column causes wrong selected column

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 6u12
    • client-libs

      J2SE Version (please include all output from java -version flag):
      java version "1.6.0_12"
      Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
      Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)

      Does this problem occur on J2SE 5.0.x or 6.0? Yes / No (pick one)
      Yes

      Operating System Configuration Information (be specific):
      Client: Microsoft Windows XP [Version 5.1.2600]

      Bug Description:
      Removing a Table Column causes wrong selected column

      When removing a column from a table, DefaultTableColumnModel is adjusting the selection (implying that a column that is not selected is selected) then removing the column.

      Run Code and press the remove column button
      Expected: Label to say 'Selected Column: column 2'
      Actual: Label says 'Selected Column: column 1'

      import static java.awt.BorderLayout.*;
      import static javax.swing.JFrame.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.event.*;

      public class Test {
        private static JLabel label;
        private static JTable table;

        public static void main(String[] args) {
          label = new JLabel();
          table = new JTable(new Object[][] {{"", ""}, {"", ""}}, new Object[] {"column 1", "column 2"});
          table.setRowSelectionAllowed(false);
          table.setColumnSelectionAllowed(true);
          table.getColumnModel().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent anEvent) {
              int selectedIndex = table.getSelectedColumn();
              label.setText(selectedIndex == -1 ? "No Column is Selected" :
                  ("Selected Column: " + table.getColumnName(selectedIndex)));
            }
          });
          table.setColumnSelectionInterval(1, 1);
          JFrame frame = new JFrame();
          frame.add(label, NORTH);
          frame.add(new JScrollPane(table));
          frame.add(new JButton(new AbstractAction("Remove First Column") {
            @Override
            public void actionPerformed(ActionEvent e) {
              table.removeColumn(table.getColumnModel().getColumn(0));
              if (table.getColumnCount() == 0) {
                setEnabled(false);
              }
            }
          }), SOUTH);
          frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
          frame.pack();
          frame.setVisible(true);
        }
      }

            Unassigned Unassigned
            tyao Ting-Yun Ingrid Yao (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: