Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

JTree: Setting node to be a JPanel

Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
I was just wondering if it is possible to set a node on a JTree to become a JPanel? Does anyone have any suggustion or even sample code on that? Any help will be appreciated. thx
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ong hui gen
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Craig, Thanks for your output.
Another question that I want to ask you is that why is it that when I add the 3 buttons to each of the panel, I get an infinite amount of "buttons" instead? I have only modified a small portion of your code which I shall put a comment "modified".

package treepanel;

import java.awt.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public class TreePanel
Random seed;

public TreePanel()
seed = new Random();

// south section
final JLabel colorLabel = new JLabel();
Dimension d = colorLabel.getPreferredSize();
d.height = 25;

// center section
JTree tree = createTree();
tree.setCellRenderer(new PanelRenderer());
new TreeSelectionListener()
public void valueChanged(TreeSelectionEvent e)
TreePath path = e.getNewLeadSelectionPath();
DefaultMutableTreeNode node =
Color color = (Color)node.getUserObject();
String s = "" + color.getRed() + ", " + color.getGreen() +
", " + color.getBlue();

JFrame f = new JFrame();
f.getContentPane().add(new JScrollPane(tree));
f.getContentPane().add(colorLabel, "South");

private JTree createTree()
int children = 4;
int grandChildren = 3;
DefaultMutableTreeNode root = new DefaultMutableTreeNode(getColor());
DefaultMutableTreeNode node;
for(int j = 0; j < children; j++)
node = new DefaultMutableTreeNode(getColor());
for(int k = 0; k < grandChildren; k++)
node.add(new DefaultMutableTreeNode(getColor()));
DefaultTreeModel model = new DefaultTreeModel(root);
return new JTree(model);

private Color getColor()
return new Color(seed.nextInt(0xffffff));

public static void main(String[] args)
new TreePanel();

class PanelRenderer implements TreeCellRenderer
JPanel panel;

public PanelRenderer()
panel = new JPanel(new BorderLayout());

public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus)

Color color = (Color)((DefaultMutableTreeNode)value).getUserObject();
panel.setLayout(new FlowLayout());

ButtonGroup panelButtons = new ButtonGroup();
JRadioButton button1 = new JRadioButton();
JRadioButton button2 = new JRadioButton();
JRadioButton button3 = new JRadioButton();
return panel;
ong hui gen
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually another thing that I am curious about is how is it possible to check those boxes. When I run the program that I written above, I found that the radiobutton does not respond at all when I click on them. Can you tell me why or tell me what can I do to enable the radiobuttons to respond to clickings.
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic