Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Repaint in TreeSelectionListener Event  RSS feed

 
Jacky Ho
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am new to write a swing application which extends JFrame. I am displaying a JSplitPane object. On the top of the splitpane, I am displaying a JTree. My application is to display a table at the bottom of the splitpane according to what node user chooses in the JTree object (which invokes the TreeSelectionListener). It seems that the JFrame doesn't get update as I tried to do the following
private class ResultFrame extends JFrame {

JTree tree;
JScrollPane treeScrollPane;
JScrollPane tablePane;
JSplitPane splitPane;
public ResultFrame() {
super("Results");
init();
setSize(800,400);
centerWindow();
show();
}
private void init() {
/* make the tree here */
...
...
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(new TreeHandler());
treeScrollPane = new JScrollPane(tree);
tablePane = new JScrollPane();

splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(treeScrollPane);
splitPane.setBottomComponent(tablePane);
getContentPane().add(splitPane);
}
private class TreeHandler implements TreeSelectionListener {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)((JTree)e.getSource()).getLastSelectedPathComponent();
/* method makeTable() will make the table */
JTable resultTable = makeTable();
tablePane.removeAll();
tablePane.add(resultTable);
tablePane.setVisible(true);
tablePane.doLayout();
tablePane.revalidate();
tablePane.repaint();
this.doLayout();
this.revalidate();
this.repaint();
}
}
}
Thanks for the help!!!
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"jck2ho" -

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

Instead of calling removeAll() and add( resultTable ) on your tablePane, you should call tablePane.setViewportView( resultTable ) JScrollPanes shouldn't be added and removed to like normal components, since they don't actually contain the components directly... they have a viewport object that deals with that.

Thanks! and welcome to the JavaRanch!
 
Jacky Ho
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works now. Thanks for your help!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!