aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Radiobuttons in JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Radiobuttons in JTree" Watch "Radiobuttons in JTree" New topic
Author

Radiobuttons in JTree

Istvan Hartung
Greenhorn

Joined: Feb 09, 2009
Posts: 3
Hi!

I want to create a specific gui.
I have a JTree that can already list the items i want to show (from a database). Now i want to but buttons (radiobuttons would be the best) next to some specific elements of the JTree... And by clicking on the OK button at the bottom of the screen i want to reach the state of those buttons and the ID of the specific element that is in that row to be able to make changes in my data.

Is there any easy way to implement that?

Thanks,
Pityu
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

You can do it by providing renderers.
Check this out for details
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#display

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Renderers alone probably aren't enough. You need to set the values as well. You can do that by using editors, or by simply using a mouse listener.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Istvan Hartung
Greenhorn

Joined: Feb 09, 2009
Posts: 3
Thank you, I've already looked for this tutorial and tryed to use my own renderer. I just can't find what functions I should override to be able to put objects next to the text displayed on the node.

If I could do that I think it wouldn't be hard to add a mouselistener to be able to change these values...

Could you please give me to hint in that?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

I've done something similar in the past with check boxes, so I have a little experience

Step one: maintain a mapping to tree paths to selected states. A Map<TreePath,Boolean> could do, or a Set<TreePath> that only contains the selected tree paths.

Step two: the renderer. This is my class with check boxes (modified slightly):


Step three: the editor (you can skip this if you make your tree read-only):


Step four: make sure this renderer and editor are used:


Step five: add the mouse listener. My example is simple; when anywhere on the tree item is clicked the state is toggled:

The toggle selection is easy with a JCheckBox, but with a JRadioButton it may be a bit harder; just calling setSelected(false) doesn't work always.
The repaint is necessary or you won't see the changes. The easy way is to call revalidate() and repaint() on the tree, but firing a treeNodesChanged event (see TreeModelListener) would be better.

If you only want to catch clicks on the actual check box / radio button, that's a bit harder but still possible. In pseudo code:

Please note that I haven't tested this last part, but I've done something similar in a JTable and that worked just fine.
Istvan Hartung
Greenhorn

Joined: Feb 09, 2009
Posts: 3
Thank you for your help, i managed to solve the problem with the help of the example code.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Radiobuttons in JTree