简体   繁体   中英

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

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 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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