G
Guest
Gast
Hallo,
könnt ihr mir verraten warum nach dem Klicken auf ein Element im Baum (vom Typ Container oder PrimitiveElement) der entsprechende Button nicht im JPanel zu sehen ist?
könnt ihr mir verraten warum nach dem Klicken auf ein Element im Baum (vom Typ Container oder PrimitiveElement) der entsprechende Button nicht im JPanel zu sehen ist?
Code:
public class DesignPatternCompositeExampleAsTreeModel extends JFrame implements TreeSelectionListener
{
JTree tree;
JPanel infoPanel;
public DesignPatternCompositeExampleAsTreeModel(String title)
{
super(title);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container root = DesignPatternCompositeExample.makeExampleTree();
DPCTreeModel model = new DPCTreeModel(root);
tree = new JTree(model);
tree.addTreeSelectionListener(this);
JScrollPane pane = new JScrollPane(tree);
infoPanel = new JPanel();
setLayout(new GridLayout(0,2));
add(pane);
add(infoPanel);
setLocation(500, 100);
setSize(700, 250);
setVisible(true);
}
public void valueChanged(TreeSelectionEvent e)
{
TreePath path = tree.getSelectionPath();
Object node = path.getLastPathComponent();
if( node instanceof Container )
{
infoPanel = new JPanel();
JButton button1 = new JButton("Container");
infoPanel.add(button1);
this.add(infoPanel);
}
else if( node instanceof PrimitiveElement )
{
infoPanel = new JPanel();
JButton button2 = new JButton("PrimitiveElement");
infoPanel.add(button2);
this.add(infoPanel);
}
}
public static void main(String[] args)
{
new DesignPatternCompositeExampleAsTreeModel("My Own Tree Model Using The Composite Design Pattern");
}
}