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

<TAB> in JTable with combobox not working well

XMLWordPrintable

      FULL PRODUCT VERSION :
      C:\JOBS\server>java -version
      java version "1.5.0_02"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
      Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      If you use a JTable using a renderer for a JComboBox, I cannot change the item selected of the combo using the navigation keys.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Suppose that I've got a JTable with 2 collumns. One for data and the other for a JComboBox. If I click, in the first collumn, I can change the data, that's ok. But if I press <TAB>, seems that the focus go to the second collumn, but I can't see where is the focus and cannot change the content of the JComboBox, using the navigation keys.

      The only way to change the item in the combo is clicking on it. This don't happen if the combo is out of a JTable... I can change normally using the navigation keys.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I've expected that using the <TAB> in a JTable, even if I have a JComboBox or JCheckBox I could change using the keyboard, not only using the mouse.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import java.awt.Component;

      import javax.swing.DefaultCellEditor;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
      import javax.swing.table.AbstractTableModel;
      import javax.swing.table.DefaultTableCellRenderer;


      public class JTableSimples extends JFrame {
          private String[] nomes = { "Nome", "Sobrenome", "Curso"};
          private Object[][] data;
          private JTable tabela;
          private JScrollPane scroll;
          private int linha;
          private int coluna;

          /**
           * Construtor
           *
           */
          JTableSimples() {
              linha = 5;
              coluna = 3;

              data = new Object[linha][coluna];

              /*
               * Insere dados
               */
              data[0][0] = new String("Zé");
              data[0][1] = new String("Luciano");
              data[0][2] = new String("Engenharia");

              data[1][0] = new String("Gilson");
              data[1][1] = new String("Watanabe");
              data[1][2] = new String("Computação");

              data[2][0] = new String("Walmir");
              data[2][1] = new String("Watanabe");
              data[2][2] = new String("Odontologia");

              data[3][0] = new String("Fabrício");
              data[3][1] = new String("Massuda");
              data[3][2] = new String("Relações Internacionais");

              data[4][0] = new String("Rodolfo");
              data[4][1] = new String("Cerezer");
              data[4][2] = new String("Computação");

              /*
               * Adiciona os checkbox
               */
              tabela = new JTable(data, nomes);
              tabela.setModel(new Modelo());

              /*
               * Adiciona cursos
               */
              JComboBox cmbcurso;
              tabela.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(
                      cmbcurso = new JComboBox()));
              cmbcurso.addItem("Engenharia");
              cmbcurso.addItem("Computação");
              cmbcurso.addItem("Matemática");
              cmbcurso.addItem("Fisioterapia");
              tabela.setRowHeight(20);
              tabela.getColumnModel().getColumn(2).setCellRenderer(new ComboRender());

              scroll = new JScrollPane(tabela);
              getContentPane().add(scroll, BorderLayout.CENTER);
              setTitle("Tabela Faculdade");
              setSize(750, 320);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
          }

          /*
           * Principal
           */
          public static void main(String[] args) {
              JFrame.setDefaultLookAndFeelDecorated(true);
              new JTableSimples();
          }

          /**
           * Modelo da tabela
           */
          private class Modelo extends AbstractTableModel {
              public void setValueAt(Object obj, int row, int col) {
                  data[row][col] = obj;
                  fireTableCellUpdated(row, col);
              }

              public boolean isCellEditable(int row, int col) {
                  return true;
              }

              public String getColumnName(int col) {
                  return nomes[col];
              }

              public int getColumnCount() {
                  return nomes.length;
              }

              public int getRowCount() {
                  return data.length;
              }

              public Object getValueAt(int row, int col) {
                  return data[row][col];
              }
          }

          /**
           * Renderiza Combobox
           */
          private class ComboRender extends DefaultTableCellRenderer {
              public Component getTableCellRendererComponent(JTable arg0,
                  Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) {
                  JComboBox cmbcurso = new JComboBox();
                  cmbcurso.removeAllItems();
                  cmbcurso.addItem(arg1);
                  cmbcurso.setSelectedItem(arg1);

                  return cmbcurso;
              }
          }
      }

      ---------- END SOURCE ----------

            shickeysunw Shannon Hickey (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: