Java_Coder_01
Java_Coder_01

Reputation: 13

Adding a JTable with data to a JDialog

I am trying to display a JTable with data from a database. My code has a submit button and when I hit that the data has to appear in the table.

To create the application, I created a "New JDialog" using NetBeans and starting placing some components like text boxes along with the submit button. I then added a Panel to it and then added a JTable inside of the panel.

In my code, I have a method for the Default table model which returns a default table model object. As part of the submit button's event handling code, I am passing the default table model's object into the JTable component (just using _table = new JTable(default table model object). _table was declared by NetBeans when I added the table to my form.

How should I proceed and get my table to display data? NetBeans basically creates the code. Technically:

1) My class extends JDialog and then comes the constructor which takes a frame and boolean as input. 2) The initcomponents method comes in the constructor. 3) I have my methods and then the event handling code. 4) The main method contains the invoke later with a runnable object and the run method which instantiates a JDialog object and sets it to true.

If the explanation is not clear and I should post the code, please let me know. Thanks a lot!

Upvotes: 0

Views: 1313

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

I'm assuming that _table was created by the netbeans form editor and that it is a reference to the table which is on the screen. So instead of using _table = new JTable(...), you should be using _table.setModel(model)

I only see four columns, how can see all the columns and adjust the column width to display the entire column for all of the columns

Wrap the JTable in a JScrollPane...

See How to Use Scroll Panes and How to Use Tables for more details

Upvotes: 0

Related Questions