aspose file tools*
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
Author

JTree: Setting node to be a JPanel

ong hui gen
Greenhorn

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
Greenhorn

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();
colorLabel.setHorizontalAlignment(JLabel.CENTER);
Dimension d = colorLabel.getPreferredSize();
d.height = 25;
colorLabel.setPreferredSize(d);

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

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(tree));
f.getContentPane().add(colorLabel, "South");
f.getContentPane().setSize(800,800);
f.setLocation(200,200);
f.setVisible(true);
}

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());
root.add(node);
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.setBackground(color);
panel.setLayout(new FlowLayout());

//modified
ButtonGroup panelButtons = new ButtonGroup();
JRadioButton button1 = new JRadioButton();
JRadioButton button2 = new JRadioButton();
JRadioButton button3 = new JRadioButton();
button2.setSelected(true);
panelButtons.add(button1);
panelButtons.add(button2);
panelButtons.add(button3);
panel.add(button1);
panel.add(button2);
panel.add(button3);
return panel;
}
}
ong hui gen
Greenhorn

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