0

I have a problem anyone can help me. This is my JTable and one column rendered in it have a combo box in its cell. Here is my problem in my table: it wont popup when I click the combo box. When I print, table.getValueAt(row,column), it will get back the prev data of the cell not the data in combo box, and it doesn't show the combo box to let the user choose.

The code of the JTable:

public void setUpSportColumn(JTable table, TableColumn sportColumn) {
    // Set up the editor for the sport cells.
    JComboBox<String> comboBox = new JComboBox<String>();
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
    model.addElement("Snowboarding");
    model.addElement("Rowing");
    model.addElement("Knitting");
    model.addElement("Speed reading");
    model.addElement("Pool");
    model.addElement("None of the above");
    comboBox.setModel(model);
    sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

    ComboBoxTableCellRenderer renderer = new ComboBoxTableCellRenderer();
    renderer.setModel(model);
    sportColumn.setCellRenderer(renderer);
}

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • here is the code , its when the rendering done , if i can ask anothe question , i am adding data to the jtable from a database is there any thing to do with that ? – Dyar Ibrahim Jul 30 '16 at 11:13

1 Answers1

1

TableRenderDemo is a complete, working example as modified below.

image

  1. To see that the editor updates the TableModel, enable debugging in MyTableModel.

    private boolean DEBUG = true;
    

    The result will be shown on the console:

    $ java TableRenderDemo
    Setting value at 0,2 to None of the above (an instance of class java.lang.String)
    New value of data:
        row 0:  Mary  Campione  None of the above  5  false
        row 1:  Alison  Huml  Rowing  3  true
        row 2:  Kathy  Walrath  Knitting  2  false
        row 3:  Sharon  Zakhour  Speed reading  20  true
        row 4:  Philip  Milne  Pool  10  false
    
  2. Use one of the approaches shown here to let the table editor have the design of an ordinary JComboBox; the accepted answer using a UIManager icon is illustrated above.

    sportColumn.setCellRenderer(new DefaultTableCellRenderer(){
    
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            JLabel label = (JLabel) super.getTableCellRendererComponent(table,
                value, isSelected, hasFocus, row, column);
            label.setIcon(UIManager.getIcon("Table.descendingSortIcon"));
            return label;
        }
    });
    
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045