how to use ColoredTreeCellRenderer and make first column editable

Answered

I user Treetable and add ColoredTreeCellRenderer to treetable and i also call append method , i make column editable by code

public boolean isCellEditable(int row, int column) {
return true;
}

 but first column is not editable!  How to make TreeColumn eidtable?

 

class Renderer extends ColoredTreeCellRenderer {
@Override
public void customizeCellRenderer(@NotNull JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
CustomNode node = (CustomNode) value;
// setBorder(BorderFactory.createLineBorder(JBColor.BLACK, 3));
append(node.getKey());
// setIcon(TYPE_ICONS.get(node.getType()));
}
}

 

 

5 comments
Comment actions Permalink

Hello!

Did you configure TableCellEditor for this column?

Also, I recommend to use JBTreeTable instead.

0
Comment actions Permalink

i use com.intellij.ui.dualView.TreeTableView

solved by Override method

public TableCellEditor getCellEditor(int row, int column) {
if(column == 0){
String key = (String) getValueAt(row, column);
return new DefaultCellEditor(getKeyTextField(key));
}
return super.getCellEditor(row, column);
}

 

another problem is column can not remove!

 

table create code :

private void renderingJsonTreeTablePanel() {
jsonTreeTable = createJsonParamKeyValueTable();
// setUpTypeColumn(jsonTreeTable.getColumnModel().getColumn(1));
ToolbarDecorator toolbarDecorator = ToolbarDecorator.createDecorator(jsonTreeTable);
toolbarDecorator.setMoveDownAction(null);
toolbarDecorator.setMoveUpAction(null);


toolbarDecorator.setAddAction(anActionButton -> {
int selectedRow = jsonTreeTable.getSelectedRow();
if (selectedRow == -1 || selectedRow == 0) {
CustomNode root = (CustomNode) ((ListTreeTableModelOnColumns) jsonTreeTable.getTableModel()).getRowValue(0);
root.insert(new CustomNode("","",TypeUtil.Type.String.name()),0);
} else {
CustomNode node = (CustomNode) ((ListTreeTableModelOnColumns) jsonTreeTable.getTableModel()).getRowValue(selectedRow);
CustomNode addNode = new CustomNode("","",TypeUtil.Type.String.name());
if(TypeUtil.Type.Array.name().equals(node.getType()) || TypeUtil.Type.Object.name().equals(node.getType())){
node.insert(addNode,0);
} else {
CustomNode parentNode = (CustomNode) node.getParent();
parentNode.insert(addNode,parentNode.getIndex(node)+1);
}
}
refreshJsonParamTable();
}
).setRemoveAction(anActionButton -> {
int selectedRow = jsonTreeTable.getSelectedRow();
CustomNode node = (CustomNode) ((ListTreeTableModelOnColumns) jsonTreeTable.getTableModel()).getRowValue(selectedRow);
CustomNode parent = (CustomNode) node.getParent();
parent.remove(node);

refreshJsonParamTable();
});
jsonTreeTablePanel = toolbarDecorator.createPanel();

}
0
Comment actions Permalink

It is not needed to override JTable class.
You can use the following method to set your editor:

`table.getColumn(id).setCellEditor(...)`

0
Comment actions Permalink

thanks very much for your reply!

second question have been solved, treetable refresh by set Root node

((DefaultTreeModel)jsonTreeTable.getTableModel()).setRoot(root);

then delete button can click

 

error code is (refresh table)

there is no need to setModel again


jsonTreeTable.setModel(model)

 

0

Please sign in to leave a comment.