开发者

How to add columns/rows to an existing empty JTable?

开发者 https://www.devze.com 2023-02-21 04:24 出处:网络
I am developing a software for which I need a basic Java Swing UI. I am using Netbeans which enables me to drag and drop UI component. However there is a final result table that I need to display usin

I am developing a software for which I need a basic Java Swing UI. I am using Netbeans which enables me to drag and drop UI component. 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. Now I want at runtime to fill this table, let's say with columns: x, y and rows: row1, row2 & row3.

How can I do that by开发者_Python百科 coding?


You need to make a custom TableModel that will allow you to support this functionality. JTable is just there to display the table on the GUI. The TableModel has all the business logic of what the data is and how each cell should act. To do this you will have to step away from the GUI-Builder and write actual code.

Tutorial: Creating a Table Model


inside which i added an empty JTable (no rows nor columns) called "finalOutputTable".

You need to add the data to the 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.


How can i do that by coding?

See the Laying Out Components Within a Container lesson of the Java Tutorial.


You can also use a TableModel witch does all that hard work for you. https://github.com/MarkyVasconcelos/Towel/wiki/ObjectTableModel

0

精彩评论

暂无评论...
验证码 换一张
取 消