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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

JTree: Setting node to be a JPanel

 
Greenhorn
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
Greenhorn
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();
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
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
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic