This is probably a simple question, I'm not very used to Java programming. But I need to create a dialog with a CheckboxTree (a variant of JTree
with checkboxes, see http://www.javaworld.com/javaworld/jw-09-2007/jw-09-checkboxtree.html)
Please note: I have created the JDialog
in the graphical environment of NetBeans, so it has generated code for adding buttons etc. So I need to know how to add this tree after the creation of the main parts, so to speak... Maybe that's the problem, because if I do something like this:
JPanel panel = new JPanel();
this.setContentPane(panel);
Then I actually see the tree showing up in the dialog, but all the buttons and all are gone...
I have been able to add it to a JFrame
and an optionspane, but I want it in a custom JDialog
. Could anyone please explain to me in very simple terms what I need to do?
Here are my feeble attempts so far:
Constructor for the JDialog:
public MetadataUI(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
Container panel = getContentPane();
panel.add(getTree());
panel.repaint();
this.pack();
}
getTree method that creates the checkboxtree:
private static CheckboxTree getTree() {
DefaultMutableTreeNode root, child;
root = new DefaultMutableTreeNode("root");
child = new DefaultMutableTreeNode("Colors");
root.add(child);
child.add(new DefaultMutableTreeNode("Cyan"));
child.add(new DefaultMutableTreeNode("Magenta"));
child.add(new DefaultMutableTreeNode("Yellow"));
child.add(new DefaultMutableTreeNode("Black"));
CheckboxTree checkboxTree = new CheckboxTree(root);
checkboxTree.setVisible(true);
return checkboxTree;
}
This repainting and all that is the last attempt based on something I found Googling, but it made no difference whatsoever, so I'm guessing I'm way off.
The simplest way to add the tree and make it visible would be appreciated. It seems to work exactly as a JTree
with regards to adding it, but I cannot make it work. So even if no one has experience with this particular checkboxtree plugin, the same (simplest) code for using a JTree
in a JDialog
would probably do!
EDIT:
In response to Andrew, here is the same thing (my best attempt) with a regular JTree:
private static JTree getTree() {
DefaultMutableTreeNode root, child;
root = new DefaultMutableTreeNode("root");
child = new DefaultMutableTreeNode("Colors");
root.add(child);
child.add(new DefaultMutableTreeNode("Cyan"));
child.add(new DefaultMutableTreeNode("Magenta"));
child.add(new DefaultMutableTreeNode("Yellow"));
child.add(new DefaultMutableTreeNode("Black"));
JTree tree = new JTree(root);
tree.setVisible(true);
return tree;
}
EDIT 2:
In response to Maxim, I'm confused. Things that are obvious to you are probably lost on me. Borrowing some stuff from your code this is the best I could come up with (doesn't work):
public MetadataUI(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
JScrollPane s = new JScrollPane();
s.getViewport().add(getTree());
getContentPane().add(s, BorderLayout.CENTER);
setVisible(true);
}