Changing JTable link at runtime - not showing in GUI
I am trying to get around developing Java ME for Pocket PC. I am running the NSIcom CrE-ME emulator and building my application using NetBeans 6.5.
The app is based on three tabs, each with Swing components. The content of Swing components is updated at different points in the application. These components include the JTextArea, JTextFields, and most importantly, the JTable in the JScrollPane.
JTable is causing problems. If I initialize it via Matisse with some sample data it appears. But if I try to set the JTable reference at runtime in the populateFields () method below, nothing appears. Note that this is simple with the table data examples from the Sun tutorial, even my custom TableModel.
What am I doing wrong? Is there some obvious update method I need to call, or some other egregious / conecpt error that I missed? I've tried pretty much every possible method I've come across, which I thought might have something to do with it.
The populateFields () method is called at different times during the program.
public void populateFields()
{
String[] columnNames = {"First Name", "Last Name","Sport", "# of Years", "Vegetarian"};
Object[][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)} };
this.tableSurvey = new JTable(new DefaultTableModel(data, columnNames));
this.scrollPaneSurvey = new JScrollPane(this.tableSurvey);
DefaultTableModel dtm = (DefaultTableModel) this.tableSurvey.getModel();
dtm.fireTableStructureChanged();
dtm.fireTableDataChanged();
this.scrollPaneSurvey.invalidate();
this.scrollPaneSurvey.validate();
this.panelSurvey.validate();
this.panelSurvey.repaint();
}
Ok, I finally figured out that apparently this is all I need:
this.tableSurvey = new JTable(new DefaultTableModel(data, columnNames));
this.scrollPaneSurvey.setViewportView(this.tableSurvey);
this.scrollPaneSurvey.validate();
Can someone explain why using the code below instead of the setViewportView () method doesn't work?
this.scrollPaneSurvey = new JScrollPane(this.tableSurvey);
Thank!
I think based on your code snippets what you are doing
this.scrollPaneSurvey = new JScrollPane(this.tableSurvey);
But the problem is that the parent panel's layout manager has its own reference to the old scrollPaneSurvey. So when you recreate it, you never replace an existing component, and that new component is never added to the render pipeline. Your class is aware of this, but the parent does not see the notification that something has changed.
It makes sense?
Where, as in the second part you are posting, you tell the scrollPaneSurvey what it should display, which generates notifications for automatic redrawing.
source to share