0

我正在努力掌握 Pocket PC 的 Java ME 开发。我正在运行 NSIcom CrE-ME 模拟器并使用 NetBeans 6.5 构建我的应用程序。

该应用程序基于三个选项卡面板,每个选项卡面板都有 Swing 组件。内容 Swing 组件在应用程序的各个点进行更新。这些组件包括 JTextArea、JTextFields,最重要的是 JScrollPane 中的 JTable。

JTable 引起了问题。如果我通过 Matisse 使用一些示例数据对其进行初始化,它就会出现。但是,如果我尝试在运行时在下面的 populateFields() 方法中设置 JTable 的引用,则不会出现任何内容。请注意,这只是使用 Sun 教程中的示例表数据,甚至不是我的自定义 TableModel。

我究竟做错了什么?是否有一些我需要调用的明显更新方法,或者我错过了一些其他明显的错误/conecpt?我几乎尝试了我遇到的所有可能的方法,我认为这些方法可能与它有关。

在程序期间的不同时间调用 populateFields() 方法。

    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();
    }
4

2 回答 2

1

好的,我终于发现显然这就是我所需要的:

this.tableSurvey = new JTable(new DefaultTableModel(data, columnNames));       
this.scrollPaneSurvey.setViewportView(this.tableSurvey);
this.scrollPaneSurvey.validate();

谁能解释为什么使用下面的代码而不是 setViewportView() 方法不起作用?

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

谢谢!

于 2008-12-30T16:16:13.213 回答
0

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.

于 2008-12-31T01:01:30.843 回答