user276002
user276002

Reputation: 178

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?

Upvotes: 6

Views: 3796

Answers (3)

Roufiq
Roufiq

Reputation: 21

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

Upvotes: 1

Andre Holzner
Andre Holzner

Reputation: 18695

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.

Upvotes: 7

zeller
zeller

Reputation: 4974

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

Upvotes: 0

Related Questions