This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Can't display new JTree after GUI up Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can Watch "Can New topic

Can't display new JTree after GUI up

Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
Hi -
I'm writing a simple Swing application where the user reads a clear-text
configuration file, parses it, and displays its contents in a JTree.
I'm using the DefaultMutableTreeNode and the DefaultTreeModel.
The problem is that I never get the tree displayed. There's no problem if I set up
the tree *before* I display the GUI (which is what all the examples I have at hand
seem to do). But I can't figure out how to get it to display AFTERWARDS (for
example, after somebody does a [Browse...], and reads my configuration file).
The basic problem is illustrated in the sample program below:
1. Create a frame, with a pushbutton and an (empty) JTree
2. When you push the button, you should create - and see - a tree
3. It fails. You never see the tree - only the pushbutton.
Any help would be deeply appreciated!
Thank you in advance .. PaulSM
I also posted this question on
* HelloJTree: try to create JTree after main frame displays
* - This program is supposed to create an (empty, zero-sized) JTree
* and a push button. When you push the button, the actual tree should
* be created and displayed.
* - Instead, all that's ever shown is the pushbutton - the tree
* is never displayed.
* DATE MODIFIED: 2.24.2004
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.tree.*;
public class HelloJTree extends WindowAdapter {
* Program main
public static void main (String[] args) {
HelloJTree app = new HelloJTree (); ();
* Initialize GUI
void run () {
// Create basic frame (use default "FlowLayout" manager)
JFrame jf = new JFrame ("Hello JTree");
// Create the JTree object, leave empty/uninitialized for now
m_root = new DefaultMutableTreeNode (null);
m_model = new DefaultTreeModel (m_root);
m_jt = new JTree (m_model);
m_jt.setEditable (true);
// Place the JTree in ScrollPane; add the ScrollPane to the main frame
jf.getContentPane ().add (
new JScrollPane(m_jt) );
// We're going to create the actual tree when we push the button
JButton jb = new JButton ("Create tree!");
jb.addActionListener (
new ActionListener () {
public void actionPerformed (ActionEvent e) {
create_tree ();
jf.getContentPane ().add (jb);
// Handle "Exit" window control
jf.addWindowListener (new HelloJTree());
// Size the JFrame to fit its contents
jf.pack ();
// Render frame visible
jf.setVisible (true);
* Exit program
public void windowClosing (WindowEvent e) {
System.exit (0);
* Try to build and display JTree: this part isn't working!!!
private void create_tree () {
System.out.println ("create_tree()...");
// Rebuild the tree model
DefaultMutableTreeNode branch = new DefaultMutableTreeNode ("Mammal");
m_root.add (branch);
DefaultMutableTreeNode leaf = new DefaultMutableTreeNode ("Cat");
branch.add (leaf);
leaf= new DefaultMutableTreeNode ("Dog");
branch.add (leaf);
branch = new DefaultMutableTreeNode ("Bird");
m_root.add (branch);
leaf= new DefaultMutableTreeNode ("Emu");
branch.add (leaf);
// Try to update the view
// ... This doesn't work...
// MutableTreeNode current_root = (MutableTreeNode)m_model.getRoot ();
// System.out.println ("dtm.getRoot()= "
// + m_model.getRoot () + ", "
// + (MutableTreeNode)m_model.getRoot () );
// m_model.insertNodeInto (m_root, current_root, 0);
// ... Neither does this ...
TreeNode[] nodes = m_model.getPathToRoot (m_root);
TreePath path = new TreePath (nodes);
m_jt.scrollPathToVisible (path);
m_jt.makeVisible (path);
* Member data
private DefaultMutableTreeNode m_root;
private DefaultTreeModel m_model;
private JTree m_jt;

Paul M. Santa Maria, SCJP
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

"PaulSM" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

The first code that is currently commented out should work... all you need to do is call one of the following methods of DefaultTreeModel - "reload()", "nodesWereInserted( TreeNode, int childIndices )", or "nodeStructureChanged( TreeNode )".

Thanks! and welcome to the JavaRanch!

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
Thank you very much.
I also got the code working. There were two problems:
1. Because I failed to set a layout manager, the pushbutton was
preventing me from ever seeing the JTree.
2. Once that was squared away, I was able to successfully build
my new structure and then display it in the JTree with the
following two lines:
less =>
DefaultTreeModel model = (DefaultTreeModel)m_jt.getModel ();
model.reload ();
Thank you again for your help (and I hope the new signature is OK!)
I agree. Here's the link:
subject: Can't display new JTree after GUI up
It's not a secret anymore!