在运行时更改JTable引用 - 不出现在GUI中

时间:2022-03-28 11:07:43

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.

我正试图掌握掌上电脑的Java ME开发。我正在运行NSIcom CrE-ME仿真器并使用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.

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

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.

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

What am I doing wrong? Is there some obvious updating method 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.

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

The populateFields() method is called at various times during the program.

在程序期间的不同时间调用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();
    }

2 个解决方案

#1


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?

任何人都可以解释为什么使用下面的代码而不是setViewportView()方法不起作用?

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

Thanks!

#2


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.

但问题是父面板布局管理器有自己对旧scrollPaneSurvey的引用。因此,当您重新创建它时,您永远不会替换现有组件,并且此新组件永远不会添加到渲染管道中。你的班级知道它,但是父母没有看到某些事情发生变化的通知。

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.

在您发布的第二部分中,您告诉scrollPaneSurvey它应该显示什么,这会生成自动重绘的通知。

#1


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?

任何人都可以解释为什么使用下面的代码而不是setViewportView()方法不起作用?

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

Thanks!

#2


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.

但问题是父面板布局管理器有自己对旧scrollPaneSurvey的引用。因此,当您重新创建它时,您永远不会替换现有组件,并且此新组件永远不会添加到渲染管道中。你的班级知道它,但是父母没有看到某些事情发生变化的通知。

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.

在您发布的第二部分中,您告诉scrollPaneSurvey它应该显示什么,这会生成自动重绘的通知。