Search code examples
javaswingnetbeansjtreejdialog

Add JTree (CheckboxTree) to JDialog


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);
}

Solution

  • If you indeed created your JDialog with Netbeans GUI (it's a JDialog form) and the component you are trying to integrate into what you already have provides a zero argument constructor, try the following:

    1. open your existing JDialog in Design mode
    2. In the Navigator view right-click [JDialog] or whatever your top-level node is (should be a sibling of "Other Components") and select Add From Palette > Beans > Choose Bean
    3. enter a fully qualified name for the class which represents your JTree component (ex. com.example.jtree.SomeJTreeComponent) and confirm. If the component is trully a JTree it will probably get added without any problems.
    4. (optional) At this point the tree may or may not be enclosed within a JScrollPane. If it is not, you can manually achieve this by repeating parts of step 2. on your newly added component and choosing Enclose in this time around.

    You will need to program other stuff by hand. I suggest you read a JTree Tutorial or refer to the documentation of your 3rd party component to help you through it.

    You might also want to read more about the tool you are using to build your GUI.