views:

554

answers:

2

Hi there,

I'm trying to get to grips with Java ME development for the Pocket PC. I'm running the NSIcom CrE-ME emulator and building my application with NetBeans 6.5.

The application is based around three tab panels, each of which have Swing components. The contents Swing components are updated at various points in the application. These components include a JTextArea, JTextFields, and most significantly, a JTable within a JScrollPane.

The JTable is causing problems. If I initialise it through Matisse with some sample data, it appears. But if I try to set the reference of the JTable at runtime in the populateFields() method below, nothing appears. Note that this is simply with the sample table data from the Sun tutorial, not even my custom TableModel.

What am I doing wrong? Is there some obvious updating emthod I need to call, or some other glaring error/conecpt I've missed? I've tried practically every possible method I've come across that I thought might have something to do with it.

The populateFields() method is called at various 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();
    }
+1  A: 

OK, I've finally found 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 anyone explain why using the code below instead of the setViewportView() method did not work?

this.scrollPaneSurvey = new JScrollPane(this.tableSurvey);

Thanks!

Because new components need to be added somewhere to show up.
iny
A: 

I think, based on your code snippets, that you are doing the

this.scrollPaneSurvey = new JScrollPane(this.tableSurvey);

But the problem is that the parent panel layout manager has its own reference to the old scrollPaneSurvey. So when you recreate it, you never replace the existing component, and this new component is never added to the render pipeline. Your class knows about it, but the parent doesn't see notification that something changed.

Does that make sense?

Where as in the second part that you post, you're telling the scrollPaneSurvey what it should be displaying, which generates the notifications to repaint automatically.

Rob Elsner