2

How can I use JComboBox as cell editor in JXTreeTable? Can you give me a working example please?

siegi
  • 5,646
  • 2
  • 30
  • 42
joycollector
  • 1,986
  • 4
  • 17
  • 18

2 Answers2

2
table.getColumn(0).setCellEditor(
    new DefaultCellEditor(new JComboBox(new Object[]{"A", "B", "C"})));
Konrad Garus
  • 53,145
  • 43
  • 157
  • 230
0

You can use JComboBox using cell editor for all column using below code except a column which JXTreeTable uses to display hierarchical data.
Ex:

    TableColumnExt column = this.tree.getColumnExt(1);
    column.setCellEditor(new DefaultCellEditor(new JComboBox());

i.e. you can not use JComboBox for a column which JXTreeTable uses to display hierarchical data.

In JXTreeTable java doc they have mention that,

JXTreeTable is a specialized table consisting of a single column in which to display hierarchical data, and any number of other columns in which to display regular data.

JXTreeTable creates TreeTableCellEditor internally which is used to display hierarchical data.

TreeTableModel provides method getHierarchicalColumn() in which you can specify which colum uses to display hierarchical data

Now if you want to provide JComboBox at first column(0th position column is by default hierarchical column) then you have to provide different column index using TreeTableModel otherwise you have to shift your column(recommended from my side).

Code to provide different column for hierarchical data

this.tree = new JXTreeTable(new DefaultTreeTableModel() {
    public int getHierarchicalColumn() {
       return 2;
    }
 });

Preview enter image description here

NOTE: I have provided this solution such that you can achieve your solution with minimum customization code. There can be another way but I personally find this solution very easy to implement