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

Selecting an item in a combo box cell renderer of a JTable transfers focus

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.3.0
    • client-libs



      Name: skT88420 Date: 10/28/99


      java version "1.3beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
      Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)
      // In this example, if from the combo box in the 2nd column of the table an item
      // is selected with the mouse, the input focus is transferred to the 1st
      // control in the frame.
      // If the item is selected with the keyboard, the focus stays in the table as
      // it is supposed to.

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

      public class TableErr extends JFrame {
          JTextField jTextField1 = new JTextField();
          JScrollPane jScrollPane1 = new JScrollPane();
          JTable jTable1 = new JTable();

          public TableErr() {
              try {
                  jbInit();
              }
              catch(Exception e) {
                  e.printStackTrace();
              }
          }

          private void jbInit() throws Exception {
              jScrollPane1.setPreferredSize(new Dimension(454, 100));
              this.getContentPane().add(jTextField1, BorderLayout.NORTH);
              this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
              jScrollPane1.getViewport().add(jTable1, null);
              jTable1.setRowHeight(jTable1.getRowHeight() + 6);

              // fill in table values
              String[] colNames = {
                  "no.", "methode", "id 1", "id2", "sample weight", "unit"
              };
              Object[][] data = {
                   {
                      new Integer(1), "chlorid", "123456789012345678901234",
                      "4711 - 0815", new Float(7.2348), "g"
                  }, {
                      new Integer(2), "chlorid", "123456789012345678901234",
                      "4711 - 0815", new Float(7.2348), "g"
                  }, {
                      new Integer(3), "chlorid", "123456789012345678901234",
                      "4711 - 0815", new Float(7.2348), "g"
                  }
              };
              DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
              dtm.setDataVector(data, colNames);

              // use a combo box as cell renderer for the 2nd column
              JComboBox cb = new JComboBox();
              cb.addItem("chlorid");
              cb.addItem("HCl");
              cb.addItem("water");
              cb.addItem("NaCl");
              cb.addItem("cristall water");
              cb.addItem("anything");
              cb.setFont(new Font("SansSerif",Font.PLAIN,12));
              TableColumn col = jTable1.getColumnModel().getColumn(1);
              col.setCellEditor(new DefaultCellEditor(cb));

          }

          public static void main(String[] args) {
              TableErr frame = new TableErr();
              frame.pack();
              frame.setVisible(true);
          }

      }
      (Review ID: 97155)
      ======================================================================

            mdavidsosunw Mark Davidson (Inactive)
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: