java swing: add custom graphical button to JTree item
Asked Answered
A

4

5

i would like to add an additional button with a small icon to the right of an item in a JTree. can this be done? if so, how?

thanks!

Allegorize answered 18/8, 2010 at 8:32 Comment(0)
O
3

You will need to CustomCellRenderer which implement TreeCellRenderer, and attach it to JTree.

In your CustomCellRenderer you can put button and icon.

JTree tree = new JTree(rootVector);
TreeCellRenderer renderer = new CustomCellRenderer();
tree.setCellRenderer(renderer);

Check this example: (referenced above code from here)

http://www.java2s.com/Code/Java/Swing-JFC/TreeCellRenderer.htm

Omphale answered 18/8, 2010 at 8:57 Comment(0)
V
5

Clamp,

Did you have success with this? I wanted to do the same thing and had a hard time getting the JButton to respond to the user. Setting up the renderer to get the button to display went smoothly, but all mouse events were handled by the JTree itself and not passed along to my button.

I finally found a solution and thought I would post it here for others to comment on (maybe there is a better way?)

In addition to my custom renderer, I also created a custom editor that extends DefaultTreeCellEditor. My custom renderer is passed into the custom editor via the constructor. In the editor I override isCellEditable to return true and I override getTreeCellEditorComponent to return renderer.getTreeCellRendererComponent. This was the key. It returns the renderer component so then all clicks are passed to my panel within my custom renderer and my JButton then responded normally to action events.

Here is my editor:

public class MyTreeCellEditor extends DefaultTreeCellEditor  {



    public MyTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer) {
        super(tree, renderer);
    }

    public Component getTreeCellEditorComponent(JTree tree, Object value,
            boolean isSelected, boolean expanded, boolean leaf, int row) {
        return renderer.getTreeCellRendererComponent(tree, value, true, expanded, leaf, row, true);
    }

    public boolean isCellEditable(EventObject anEvent) {
        return true; // Or make this conditional depending on the node
    }
}

On your tree, be sure to set your custom editor:

myTree.setCellEditor(new MyTreeCellEditor(myTree, (DefaultTreeCellRenderer) myTree.getCellRenderer()));
Vickievicksburg answered 22/9, 2010 at 12:24 Comment(3)
+1. I only used the TreeCellEditor interface, because DefaultTreeCellEditor has lots of unnecessary features.Booster
The renderer is responsible for rendering what it looks like only. When you click into a component you're editing it, so responsibility is passed off to the editor. For example, JTable functions that way. If you want a clickable button in a JTable, then the editor is what actually handles the button clicking, not the renderer. That said, that's why I don't like your answer. you need to separate out responsibility. You shouldn't be calling into your renderer to get something functional to act on. You need different components for renderer and editor to be more in line with the Swing Framework.Pu
(FYI there are important reasons why you should be separating editor and renderer btw. It's not as arbitrary as it sounds.) Swing is a nice framework for abstracting a lot of things away from you, as a developer, so you type less. You still have a responsibility to understand the API you're calling to have effective logic. This is typically why Swing gets a bad reputation, because people just assume that because the API is so abstract, that they don't have to think about what they're doing, which is a really bad way to develop code.Pu
O
3

You will need to CustomCellRenderer which implement TreeCellRenderer, and attach it to JTree.

In your CustomCellRenderer you can put button and icon.

JTree tree = new JTree(rootVector);
TreeCellRenderer renderer = new CustomCellRenderer();
tree.setCellRenderer(renderer);

Check this example: (referenced above code from here)

http://www.java2s.com/Code/Java/Swing-JFC/TreeCellRenderer.htm

Omphale answered 18/8, 2010 at 8:57 Comment(0)
B
1

You can add a TreeCellRenderer to your JTree. This Renderer can render an Icon on each element of the tree.

Boice answered 18/8, 2010 at 8:52 Comment(0)
A
1

From the JTree Javadoc:

To use JTree to display compound nodes (for example, nodes containing both a graphic icon and text), subclass javax.swing.tree.TreeCellRenderer and use setCellRenderer to tell the tree to use it.

A compound node is what you want. You have to implement on single method which will return a Composite object which is in, in your case, a small panel containing a button and a label side by side.

Alyciaalyda answered 18/8, 2010 at 8:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.