简体   繁体   English

如何将列/行添加到现有的空JTable中?

[英]How to add columns/rows to an existing empty JTable?

I am developing a software for which I need a basic Java Swing UI. 我正在开发需要基本的Java Swing UI的软件。 I am using Netbeans which enables me to drag and drop UI component. 我正在使用Netbeans,这使我能够拖放UI组件。 However there is a final result table that I need to display using code. 但是,我需要使用代码来显示最终结果表。

In my UI using trhe IDE I created a JTabbedPane , inside which I added an empty JTable (no rows nor columns) called finalOutputTable . 在使用IDE的UI中,我创建了一个JTabbedPane ,在其中添加了一个名为finalOutputTable的空JTable (无行也无列)。 Now I want at runtime to fill this table, let's say with columns: x, y and rows: row1, row2 & row3. 现在我想在运行时填充此表,让我们用以下列来说:x,y和行:row1,row2和row3。

How can I do that by coding? 如何通过编码做到这一点?

You need to make a custom TableModel that will allow you to support this functionality. 您需要创建一个自定义的TableModel ,它将允许您支持此功能。 JTable is just there to display the table on the GUI. JTable就在那里显示在GUI上的表。 The TableModel has all the business logic of what the data is and how each cell should act. TableModel具有有关什么是数据以及每个单元如何工作的所有业务逻辑。 To do this you will have to step away from the GUI-Builder and write actual code. 为此,您将不得不离开GUI-Builder并编写实际代码。

Tutorial: Creating a Table Model 教程: 创建表模型

inside which i added an empty JTable (no rows nor columns) called "finalOutputTable". 在其中添加了一个空的JTable(无行也无列),称为“ finalOutputTable”。

You need to add the data to the TableModel. 您需要将数据添加到TableModel中。 Then you use: 然后您使用:

table.setModel(...);

to display the data. 显示数据。 You may also need to revalidate() the panel containing the table if you didn't reserve space for the table when you created the form. 如果在创建表单时没有为表保留空间,则可能还需要revalidate()包含表的面板。

How can i do that by coding? 如何通过编码做到这一点?

See the Laying Out Components Within a Container lesson of the Java Tutorial. 请参阅Java教程的“ 在容器中布置组件 ”课程。

You can also use a TableModel witch does all that hard work for you. 您还可以使用TableModel为您完成所有艰苦的工作。 https://github.com/MarkyVasconcelos/Towel/wiki/ObjectTableModel https://github.com/MarkyVasconcelos/Towel/wiki/ObjectTableModel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM