0

Ok, so I've got a table setup in which I've added a JComboBox to a specific cell as they've done in the example here, but for some reason the combo box won't display until that cell has been selected. If I select that cell, the combo box opens it's list for me to select from. Whether I change the selection or not, if I click to another cell in the table, it then displays the text of the item selected from the combo box as if it was a simple string displayed in the table as desired.

My question is: How do I get it to display the selected value in the JComboBox without having to select the cell first?

edit: One thing I forgot the mention is; rather than declaring the DefaultTableModel data before-hand like they have, items are instead added to the DTM later using model.addRow();

Community
  • 1
  • 1
DGolberg
  • 2,109
  • 4
  • 23
  • 31
  • 1
    So you modified working code and now it doesn't work. Maybe the problem is with the modified code? Since you didn't post your [SSCCE](http://sscce.org/) we can't guess what you might have done. – camickr Feb 21 '13 at 22:55
  • Well, I figured the edit was simple enough it wouldn't be that hard to figure out... I basically changed `DefaultTableModel model = new DefaultTableModel(data, columnNames);` to `DefaultTableModel model = new DefaultTableModel(columnNames);` and then did `model.addRow();` to put items in the table after the table is created. I wouldn't imagine it being much different than creating the table the way they had been then updating it later with different data. – DGolberg Feb 21 '13 at 23:20
  • It is no different. It doesn't matter if the model is updated using the addRow() method. – camickr Feb 22 '13 at 01:22

2 Answers2

2

This is the normal behaviour. A table uses renderers and editors. The default renderer for a cell is just a JLabel so all you see is the text. When you click on the cell the editor is invoked so you see the combo box.

If you want the cell to look like a combo box even when it is not being edited then you need to create a combo box renderer for that column.

Read the section from the Swing tutorial on Using Custom Renderers for more information.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I guess I worded it incorrectly... Seeing it as a string is fine, actually desired, but I don't even see that until I've selected the cell then switched to another. The cell just appears blank until then. – DGolberg Feb 21 '13 at 22:40
  • Hmm, I just thought of a work-around, but it seems a bit of an odd way to do it... Anyway, when I update the table (get the data/new data to display in it) I just set the value at that cell to display the combo box's selected item: `projDTM.setValueAt(cbItems.getSelectedItem(), z, 1);` Is there a better way to do this, or is this the intended method? – DGolberg Feb 21 '13 at 22:53
1

You can either try creating your own Renderer as in this example.

public void example(){  

    TableColumn tmpColum =table.getColumnModel().getColumn(1);
    String[] DATA = { "Data 1", "Data 2", "Data 3", "Data 4" };
    JComboBox comboBox = new JComboBox(DATA);

    DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox);
    tmpColum.setCellEditor(defaultCellEditor);
    tmpColum.setCellRenderer(new CheckBoxCellRenderer(comboBox));
    table.repaint();
}


/**
   Custom class for adding elements in the JComboBox.
*/
class CheckBoxCellRenderer implements TableCellRenderer {
    JComboBox combo;
    public CheckBoxCellRenderer(JComboBox comboBox) {
    this.combo = new JComboBox();
    for (int i=0; i<comboBox.getItemCount(); i++){
        combo.addItem(comboBox.getItemAt(i));
    }
    }
    public Component getTableCellRendererComponent(JTable jtable, 
                           Object value, 
                           boolean isSelected, 
                           boolean hasFocus, 
                           int row, int column) {
    combo.setSelectedItem(value);
    return combo;
    }
}

or you can customize the default Renderer like in this example.

final JComboBox combo = new JComboBox(items);
TableColumn col = table.getColumnModel().getColumn(ITEM_COL);
col.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;
    }
    });

The first example makes the cell look like the JComboBox after its clicked. The second example adds an arrow icon to the JComboCox that showcases that the JComboBox is clickable. I used the second example, result can be seen here.

Community
  • 1
  • 1
Adalars1
  • 36
  • 1
  • 5
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Ruby Racer Nov 07 '14 at 10:26
  • Added significant parts of the links. – Adalars1 Nov 07 '14 at 14:03
  • It's been so long since I'd asked this question that I forgot what it was I was going to use it for (I believe I went with an alternate method in the end), but your second option does at least provide a suitable work-around that shows the field is different from the rest so you can expect a different response when clicking on it. Thank you! – DGolberg Nov 07 '14 at 20:03