您是否必须更改 Eclipse VE 生成的 Java Swing 代码才能获得正确的程序?

发布于 2024-09-16 13:13:12 字数 1000 浏览 2 评论 0原文

使用 Eclipse 中的可视化编辑器,我启动了一个 Swing UI,其中包含一个带有 2 列 (JTableColumn) 的表 (JTable)。以这种方式向表添加数据:

final DefaultTableModel model = (DefaultTableModel) this.jTable.getModel();
model.addRow(new Object[] {"Column 1", "Column 2"});

生成 ArrayIndexOutOfBoundsException。我通过设置支持表的模型的列数解决了这个问题:

model.setColumnCount(this.jTable.getColumnCount());

但是在这次调用之后,我使用 UI 编辑器定义的表的列标题更改为“A”和“B”。现在我想知道是否应该像我一样继续纠正生成的代码,或者是否有更好的方法使用可视化编辑器构建 UI?

为了完整起见,这是定义表和列的生成代码:

private JTable getJTable() {
    if (this.jTableSongs == null) {
        final TableColumn tableColumn1 = new TableColumn();
        tableColumn1.setHeaderValue("Header 1");
        final TableColumn tableColumn2 = new TableColumn();
        tableColumn2.setHeaderValue("Header 2");
        this.jTableSongs = new JTable();
        this.jTableSongs.addColumn(tableColumn1);
        this.jTableSongs.addColumn(tableColumn2);
    }
    return this.jTable;
}

Using the Visual Editor in Eclipse I started a Swing UI containing a table (JTable) with 2 columns (JTableColumn). Adding data to the table this way:

final DefaultTableModel model = (DefaultTableModel) this.jTable.getModel();
model.addRow(new Object[] {"Column 1", "Column 2"});

generated an ArrayIndexOutOfBoundsException. I solved this by setting the number of columns of the model backing the table:

model.setColumnCount(this.jTable.getColumnCount());

But after this call, the column headers of the table I defined using the UI editor, are changed to "A" and "B". Now I'm wondering if I should go on and correct the generated code like I did, or is there a better way to build UI's with Visual Editor?

To be complete, this is the generated code to define the table and columns:

private JTable getJTable() {
    if (this.jTableSongs == null) {
        final TableColumn tableColumn1 = new TableColumn();
        tableColumn1.setHeaderValue("Header 1");
        final TableColumn tableColumn2 = new TableColumn();
        tableColumn2.setHeaderValue("Header 2");
        this.jTableSongs = new JTable();
        this.jTableSongs.addColumn(tableColumn1);
        this.jTableSongs.addColumn(tableColumn2);
    }
    return this.jTable;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上青苔 2024-09-23 13:13:13

我不知道这个具体问题(表列),但过去我在 VE 上也遇到过类似的问题。

如果属性视图中没有列数,则必须以编程方式指定它,编辑代码,但 VE 不会删除手动添加的代码。

I don't know about this specific problem (table columns) but in the past i had similar problems with VE.

If you don't have the number of columns in the Property View, you have to specify it programmatically, editing the code, but VE will not remove hand added code.

只有影子陪我不离不弃 2024-09-23 13:13:13

或者有更好的方法来构建 UI
使用可视化编辑器?

使用代码生成器的问题是您花费更多时间学习 IDE 而不是学习 Java。更好的方法是使用 IDE 进行调试等,并自己构建 GUI,这样您就可以完全控制并且可以将代码从一个 IDE 移动到另一个 IDE。

我不知道生成的代码应该是什么样子,但以下内容看起来不一致:

this.jTable = new JTable(); 
this.jTableSongs.addColumn(tableColumn1); 
this.jTableSongs.addColumn(tableColumn2); 

创建了 jTable 变量(并从方法返回),但列被添加到 jTableSongs 中。所以在我看来 jTable 有 0 列,这可能会导致异常。

or is there a better way to build UI's
with Visual Editor?

The problem with using code generators is that you spend more time learning the IDE and not learning Java. The better approach is to use the IDE for debugging and so on and build the GUI's yourself so you are in full control and the code can be moved from one IDE to another.

I don't know what the generated code should look like but the following looks inconsistent:

this.jTable = new JTable(); 
this.jTableSongs.addColumn(tableColumn1); 
this.jTableSongs.addColumn(tableColumn2); 

A jTable variable is created (and returned from the method) but the columns are added to jTableSongs. So it looks to me like jTable has 0 columns which could cause an Exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文