ccot
ccot

Reputation: 1873

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?

Upvotes: 0

Views: 6832

Answers (4)

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

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

Upvotes: 2

Andrew Thompson
Andrew Thompson

Reputation: 168825

How can i do that by coding?

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

Upvotes: 2

camickr
camickr

Reputation: 324118

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.

Upvotes: 3

unholysampler
unholysampler

Reputation: 17321

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

Upvotes: 6

Related Questions