JTable doesnt show

48 views Asked by At

First I have created JSplitPane:

JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT ,new JScrollPane(textArea), new JScrollPane(table));
add(splitPane);//added to frame

then I have created table with default model:

DefaultTableModel model = new DefaultTableModel(columns, 0);
JTable table = new JTable(model);

in which i set these values as colum array: A B C D E F P1 P2 R1 R2 R3 S1 S2.

The problem is when I later after some action (in action listener) try to add data with this code:

model.addRow(parts);

where parts are(as array):

3.0 2.0 5.0 4.0 6.0 1.0 4.0 1.0 -4.0 -30.0 5.0 -1.0 -6.0  
6.0 1.0 3.0 2.0 5.0 4.0 0.5 -5.5 0.75 -38.5 7.0 1.5 -5.5 

textArea shows correctly but table does not.

I dont know what I am doing wrong, I have been following this tutorial https://docs.oracle.com/javase/tutorial/uiswing/components/table.html but it doesnt help.

P.S. If I set table = new JTable(10,10) just after splitPane initialization it shows normally in split pane.

1

There are 1 answers

0
camickr On BEST ANSWER

The table variable is null when you add it to the split pane. So no component got added to the split pane.

You can't then later create an instance of the JTable. Sure the variable "table" may now contain a reference to the JTable, but you haven't actually added the table to the split pane.

You can only add the table to the split pane AFTER you create the JTable.

So the code must be something like:

table = new JTable(column, 0);
JSplitPane splitPane = new JSplitPane(... , new JScrollPane(textArea), new JScrollPane(table));

Then later in your code you can use:

DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addRow( ... );