• Post Reply Bookmark Topic Watch Topic
  • New Topic

JScrollPane doesn't adapt to change of data in JTable  RSS feed

 
Wolfgang Tintemann
Ranch Hand
Posts: 65
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I write a JApplet showing some data in a JTable.
I added a JTable to a JScrollPane in the init() method ( scrollPane=new JScrollPane(table); )
and I can see the data in my JPanel to which I added the JScrollpane.

Now I want to refresh the data by clicking on a button.
So in the actionPerformed method I wrote according to advice here in the forum


But what I see now is the new data in the former JScrollPane : former number of rows even
though the new data contain more rows.

I have no idea how i can generate a JScrollPane that contains all the data.

I tried this line after the above mentioned :

but then I only see an empty screen without any scrollbars.

I tried all sorts of remove(), validate() and other methods but I fail.
I am not very educated in Swing yet.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Those are the only two lines of code you need. The model is responsible for invoking the fireXXX methods. In this case the table know the data has been changes because you used the setModel() method.

If the code doesn't work then it probably means you have class and local variables defined for the model and/or table. So you are updating the wrong reference.
 
Wolfgang Tintemann
Ranch Hand
Posts: 65
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my JPanel nesting in the init() method of JApplet :



I use no local variables as far as I see. All are class variables.

When I print model.getColumnCount() and model.getRowCount()
in actionPerformed I get correct values.
The columns BTW are correctly scrolled ( here I use a constant value ).
Only the rows are not scrolled completely.
 
Wolfgang Tintemann
Ranch Hand
Posts: 65
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found my error !

I had set the Preferred Size of the table in the init() method with
a value of the rows read.

I added this line in the actionPerformed method an now it works.

Thank you.

 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added this line in the actionPerformed method an now it works.


You should NEVER set the preferred size of the JTable. The preferred size is calculated automatically.
 
Wolfgang Tintemann
Ranch Hand
Posts: 65
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also thought already about that.

So I will make a try. Its all very complex if one isnt
informed about the internals of Swing.

Thank you very much for your hints. Your advice in the
forum threads about the subject were extremely helpful.
I would never have found the setModel() trick...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!