Tree view with button action in toolwindow
Hello everyone!
I'm developing a plugin with a toolwindow. In my toolwindow, i must have a tree and buttons near of all child elements in the tree.
I create tree and added buttons. Once, i will show you how did i do.
Here is MyCellRenderer class
static class MyCellRenderer implements TreeCellRenderer, ActionListener {
JLabel label;
JButton button;
JPanel renderer;
DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
Color backgroundSelectionColor;
Color backgroundNonSelectionColor;
public MyCellRenderer(){
Border emptyBorder = BorderFactory.createEmptyBorder(2, 0, 2, 2);
renderer = new JPanel(new GridLayout(1,6));
label = new JLabel(" ");
label.setForeground(Color.black);
label.setBorder(emptyBorder);
button = new JButton();
button.setIcon(defaultSuppress);
//any of the button code under below didn't work now
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
button.setSize(16,16);
renderer.add(label);
renderer.add(button);
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
Component returnValue = null;
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject = ((DefaultMutableTreeNode) value)
.getUserObject();
if (userObject instanceof Leaf) { // my tree gets elements title and icon from the Leaf class
Leaf leafObj = (Leaf) userObject;
label.setText(leafObj.getTitle());
button.setIcon(leafObj.getButtonImage());
renderer.setEnabled(tree.isEnabled());
returnValue = renderer;
}
}
if (returnValue == null) {
returnValue = defaultRenderer.getTreeCellRendererComponent(tree,
value, selected, expanded, leaf, row, hasFocus);
}
return returnValue;
}
}
And I have one more class which i created the tree in that class. I didn't share it, cause it works fine with icon and name of element.
Here is my problem, I want to add actions to icons on the right side of every child element but i didn't.
When user clicks this buttons, one html page should open on the ide.
May someone help me, please? Thanks a lot from now!
Regards,
Melek
Please sign in to leave a comment.
Hi Melek,
You could try to use com.intellij.ui.treeStructure.treetable.TreeTable for complex renderers.
Also, you could look at the Settings dialog and its tree, where icons are aligned to the right.
See com.intellij.openapi.options.newEditor.SettingsTreeView.MyRenderer
Regards,
Sergey