• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTree problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below displays a JFrame with a JScrollPane containing a JTree with just a root node ("root").

At the bottom of the frame is a button that when pressed re-initialises the JTree with no arguments (line 42). Hence once the button is clicked the JTree should have some elements in it (those default ones : colours, sports, food).

However, the reinitialised JTree is not displayed. What's the problem?


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.tree.*;

class Test extends JFrame implements ActionListener {

private JScrollPane treeScrollPane;
private JTree tree;
private DefaultMutableTreeNode root;
private JButton runButton;

public Test() {
super("Test");

this.tree = new JTree(this.root = new DefaultMutableTreeNode("root"));

this.treeScrollPane = new JScrollPane(this.tree);
this.treeScrollPane.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Tree panel"),
BorderFactory.createEmptyBorder(5,5,5,5)));

this.runButton = new JButton("click me");
this.runButton.addActionListener(this);

Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(this.treeScrollPane, BorderLayout.CENTER);
contentPane.add(this.runButton, BorderLayout.SOUTH);

this.setSize(300, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == this.runButton) {
System.out.println("DEBUG : button clicked");
this.tree = new JTree();
this.tree.expandPath(new TreePath(this.tree.getModel().getRoot()));
}
}

public static void main(String args[]) {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println(e.getMessage());
}
new Test();
}
}
 
Edin Edin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got the answer now, and in case anybody is wondering the code is missing
at the end of the actionPerformed() method.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic