Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Swing / AWT / SWT and the fly likes JTree: Setting node to be a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree: Setting node to be a JPanel" Watch "JTree: Setting node to be a JPanel" New topic

JTree: Setting node to be a JPanel

ong hui gen

Joined: Aug 01, 2003
Posts: 3
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
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
ong hui gen

Joined: Aug 01, 2003
Posts: 3
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

Joined: Aug 01, 2003
Posts: 3
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.
subject: JTree: Setting node to be a JPanel