This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Radiobuttons in JTree

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it by providing renderers.
Check this out for details
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#display
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Istvan Hartung
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help, i managed to solve the problem with the help of the example code.
 
Rob Spoor
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic