Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Selected Value from JXTreeTable

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?

like image 898
user276002 Avatar asked Nov 01 '11 06:11

user276002


2 Answers

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.

like image 104
Andre Holzner Avatar answered Oct 02 '22 23:10

Andre Holzner


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

like image 34
Roufiq Avatar answered Oct 02 '22 21:10

Roufiq