• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Radiobuttons in JTree

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Create symphonies in seed and soil. For this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic