-
Bug
-
Resolution: Not an Issue
-
P3
-
None
-
1.1.6, 1.2.0
-
generic, x86
-
solaris_2.5, windows_nt
JTree doesn't update/refresh when a treenode is changed (added or deleted) until a forced refresh - explicit invocation of SwingUtilities.updateComponentTreeUI(tree);
The following is the test case, you click on button "ChangePath", the treenode "Beethoven" is replaced with "mozart", but the tree is not updated untill you click button "refresh".
import com.sun.java.swing.*;
import com.sun.java.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestTreeUpdate extends JFrame implements ActionListener
{
JTree tree = null;
DefaultTreeModel treeModel = null;
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Music");
JScrollPane sp;
JPanel bottom = new JPanel();
JButton refresh, changePath;
public TestTreeUpdate(){
getContentPane().setLayout(new BorderLayout());
DefaultMutableTreeNode beethoven;
DefaultMutableTreeNode style;
DefaultMutableTreeNode testNode;
// Beethoven
top.add(beethoven = new DefaultMutableTreeNode("Beethoven"));
beethoven.add(style = new DefaultMutableTreeNode("Concertos"));
style.add(new DefaultMutableTreeNode("No. 1 - C Major"));
style.add(new DefaultMutableTreeNode("No. 2 - B-Flat Major"));
tree = new JTree(treeModel = new DefaultTreeModel(top));
tree.setBackground(Color.lightGray);
sp = new JScrollPane(tree);
changePath = new JButton("Change Path"){
public Dimension getPreferredSize(){
return new Dimension(100, super.getPreferredSize().height);
}
};
changePath.addActionListener(this);
changePath.setActionCommand("changePath");
refresh = new JButton("Refresh"){
public Dimension getPreferredSize(){
return new Dimension(100, super.getPreferredSize().height);
}
};
refresh.addActionListener(this);
refresh.setActionCommand("refresh");
bottom.setLayout(new FlowLayout(FlowLayout.CENTER));
bottom.add(changePath);
bottom.add(refresh);
getContentPane().add("Center", sp);
getContentPane().add("South", bottom);
setSize(300,300);
show();
}
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
if(command.equals("changePath")){
// Mozart
top.removeAllChildren();
DefaultMutableTreeNode mozart = new DefaultMutableTreeNode("Mozart");
mozart.add(new DefaultMutableTreeNode("Concertos"));
top.add(mozart);
}else if(command.equals("refresh")){
SwingUtilities.updateComponentTreeUI(tree);
}
}
public static void main(String[] args){
new TestTreeUpdate();
}
}
The following is the test case, you click on button "ChangePath", the treenode "Beethoven" is replaced with "mozart", but the tree is not updated untill you click button "refresh".
import com.sun.java.swing.*;
import com.sun.java.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestTreeUpdate extends JFrame implements ActionListener
{
JTree tree = null;
DefaultTreeModel treeModel = null;
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Music");
JScrollPane sp;
JPanel bottom = new JPanel();
JButton refresh, changePath;
public TestTreeUpdate(){
getContentPane().setLayout(new BorderLayout());
DefaultMutableTreeNode beethoven;
DefaultMutableTreeNode style;
DefaultMutableTreeNode testNode;
// Beethoven
top.add(beethoven = new DefaultMutableTreeNode("Beethoven"));
beethoven.add(style = new DefaultMutableTreeNode("Concertos"));
style.add(new DefaultMutableTreeNode("No. 1 - C Major"));
style.add(new DefaultMutableTreeNode("No. 2 - B-Flat Major"));
tree = new JTree(treeModel = new DefaultTreeModel(top));
tree.setBackground(Color.lightGray);
sp = new JScrollPane(tree);
changePath = new JButton("Change Path"){
public Dimension getPreferredSize(){
return new Dimension(100, super.getPreferredSize().height);
}
};
changePath.addActionListener(this);
changePath.setActionCommand("changePath");
refresh = new JButton("Refresh"){
public Dimension getPreferredSize(){
return new Dimension(100, super.getPreferredSize().height);
}
};
refresh.addActionListener(this);
refresh.setActionCommand("refresh");
bottom.setLayout(new FlowLayout(FlowLayout.CENTER));
bottom.add(changePath);
bottom.add(refresh);
getContentPane().add("Center", sp);
getContentPane().add("South", bottom);
setSize(300,300);
show();
}
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
if(command.equals("changePath")){
// Mozart
top.removeAllChildren();
DefaultMutableTreeNode mozart = new DefaultMutableTreeNode("Mozart");
mozart.add(new DefaultMutableTreeNode("Concertos"));
top.add(mozart);
}else if(command.equals("refresh")){
SwingUtilities.updateComponentTreeUI(tree);
}
}
public static void main(String[] args){
new TestTreeUpdate();
}
}