Get selected value from JXTreeTable

I create a treetable using JXTreeTabble , and I want to disable / manage the menu items depending on the value selected. 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; } 

Problem

The above only works if the contents of table not expanded. Since the index for the selected row can be larger than size t able model (the model can have two elements, and the row can have 10 when everything is expanded). In addition, the object type parent is different from children (think of a book with chapters as its children).

What would you suggest as a way to do this correctly?

+6
source share
3 answers

Assuming index is your line number, try the following to get the node object:

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

where treetable will be a pointer to a table using this table.

+6
source

in JXTreeTable, you can access the 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(); 

as a result, you get the class from the object, you can parse the object to get the value

+1
source

Indexes may vary in presentation and model. First you need to configure the selected row index using convertRowIndextToModel()

0
source

Source: https://habr.com/ru/post/900482/


All Articles