How to make JComboBox dropdown always visible in JTable

I am using JComboBox with JTables but the dropdown is only "visible" when clicked. How can I change this default behavior and make it always visible and usable?

public void start(){
    TableColumn column = table.getColumnModel().getColumn(0);
    JComboBox comboBox = new JComboBox();
    DefaultComboBoxModel model = new DefaultComboBoxModel();
    model.addElement("a");
    model.addElement("b");
    comboBox.setModel(model);
}

      

+3


source to share


1 answer


As I understand it, you would like the cells to always look like JComboBoxes, not jLabels.

This can be done easily by adding a TableCellRenderer to the TableColumn. Inserting in the following code should have the desired effect.



column.setCellRenderer(new TableCellRenderer() {
    JComboBox box = new JComboBox();

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {
        box.removeAllItems();
        box.addItem(value.toString());
        return box;
    }
});

      

0


source







All Articles