• 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

Repaint in TreeSelectionListener Event

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works now. Thanks for your help!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic