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

Enter key causes change in JTable row selection

XMLWordPrintable

      Name: yyT116575 Date: 07/31/2001


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

      (also occurs with windows jdk1.2.2, and Solaris 7 jdk1.3.0 and 1.3.1)

      Pressing the Enter key causes the row selection in a JTable to advance one row.
      This is a problem, because our users want to select rows using arrow keys and
      then use the Enter key to Enter their selection (cause a dialog commit). It's
      easy enough to add the enter-commit, but then the wrong row is selected due to
      this other behavior.

        To reproduce:
      1. Run the code below, which is based on Sun's online table demo.
      2. Press the enter key - the row selection is printed to the console. You can
      see the selection changes each time the enter key is pressed.
      3. If you uncomment lines 86-92, recompile and rerun, you can see the same
      behavior when the KeyboardAction to activate a button is added.


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

      public class SimpleTableEnterDemo extends JPanel {
        private boolean DEBUG = true;
        JTable table;
        ListSelectionModel selModel;
        final JButton close = new JButton ("Close");
        CloseAction closeAction;
        JPanel panel;

        public SimpleTableEnterDemo() {
          super("SimpleTableEnterDemo");
          panel = new JPanel (new BorderLayout());
          Object[][] data = {
              {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)},
              {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)},
              {"Kathy", "Walrath", "Chasing toddlers", new Integer(2), new Boolean(false)},
              {"Mark", "Andrews", "Speed reading", new Integer(20), new Boolean(true)},
              {"Angela", "Lih", "Teaching high school", new Integer(4), new Boolean(false)}
          };

          String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};

          table = new JTable(data, columnNames);
          table.setPreferredScrollableViewportSize(new Dimension(500, 70));

          if (DEBUG) {
            table.addMouseListener(new MouseAdapter() {
              public void mouseClicked(MouseEvent e) {
                printDebugData(table);
              }
            });
          }

          //Create the scroll pane and add the table to it.
          JScrollPane scrollPane = new JScrollPane(table);

          //Add the scroll pane to this window.
          // getContentPane().add(scrollPane, BorderLayout.CENTER);
          panel.add(scrollPane, BorderLayout.CENTER);

          // addWindowListener(new WindowAdapter() {
          // public void windowClosing(WindowEvent e) {
          // System.exit(0);
          // }
          // });
          doMore();
        }

        private void doMore() {

          selModel = table.getSelectionModel();
          selModel.addListSelectionListener( new ListSelectionListener() {
            public void valueChanged (javax.swing.event.ListSelectionEvent evt) {
              if ( !evt.getValueIsAdjusting()) {
                int SelectedRow = table.getSelectedRow();
                System.out.println (" valueChanged: row " + SelectedRow + " selected.");
              }
            }
          });

          //close button

          closeAction = new CloseAction();
          close.setAction(closeAction);
          // getContentPane().add (close, "East");
          panel.add (close, "East");
      /*
          addKeyListener( new KeyAdapter( ) {
            public void keyTyped ( KeyEvent e ) {
              if ( e.getKeyChar( ) == KeyEvent.VK_ENTER ) {
                close.doClick();
              }
            }
          });
      */
          add (panel);

        }

        private void printDebugData(JTable table) {
          int numRows = table.getRowCount();
          int numCols = table.getColumnCount();
          javax.swing.table.TableModel model = table.getModel();

          System.out.println("Value of data: ");
          for (int i=0; i < numRows; i++) {
            System.out.print(" row " + i + ":");
            for (int j=0; j < numCols; j++) {
              System.out.print(" " + model.getValueAt(i, j));
            }
            System.out.println();
          }
          System.out.println("--------------------------");
        }

        class CloseAction extends AbstractAction {
          public CloseAction() { super ("Close");}

          public void actionPerformed (ActionEvent evt) {
            int row = table.getSelectedRow();
            System.out.println("CloseAction: Selected row is: " + row) ;
          }

        }

        public void commit () {
          System.out.println("demo.commit())");
          close.doClick();

        }

        public static void main(String[] args) {
          SimpleTableEnterDemo demo = new SimpleTableEnterDemo();
          JFrame frame = new JFrame();
          frame.getContentPane().add (demo.panel);
          frame.pack();
          frame.setVisible(true);
        }
      }
      (Review ID: 129113)
      ======================================================================

            shickeysunw Shannon Hickey (Inactive)
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: