Get Selected Value from JXTreeTable

3.7k views Asked by At

I am building a treetable using JXTreeTabble and I want to disable/able menu items depending on the selected value. So, I tried to put this code in my table model:

public Object getValueAt(int index) {
        if (index >= 0 && index < root.getSize()){
            return root.get(index);
        }

        return null;

    }

The problem

The above only works if the contents of the table are not expanded. Because the index of the selected row might be larger than the size of the table model (model can have two items and row can have 10 when everything is expanded). Also, the object type of the parent is different from the children (think of a book with chapters as it children).

What would you suggest as a way to do the above correctly?

3

There are 3 answers

0
zeller On

The indexes may differ in the view and the model. You have to adjust the selected row's index first using convertRowIndextToModel()

0
Andre Holzner On

assuming index is your row number, try the following to get hold of the node object:

TreePath path = treetable.getPathForRow(index);
Object node = path.getLastPathComponent();

where treetable would be a pointer to the table using this table model.

0
Roufiq On

at JXTreeTable you could access value based on row and nodeClass from your treeTable. example:

int row=treeTable.getSelectedRow();
//get value from column
Object object= treeTable.getValueAt(row, yourColumn);
TreePath path= treeTable.getPathForRow(row);
Object o= path.getLastPathComponent();
Class<? extends Object> entity=o.getClass();

the result you would get an Class from the object, you could parse the object to get the value