Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Replacing tables  RSS feed

 
Rafal Szulejko
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, my name is Rafal and I'm new member here. I'm writing my first bigger program (mysql connection and the first thing bigger than 300 lines ). What I'd like to do is to render data from MySQL using JTable - I managed to handle all the queries but I don't know how to update the table on screen. I've tried to use repaint() but nothing happened. I'm not posting whole code because you'll probably not interested, below are the most important things. I thought about using TableModel but I have no idea how.
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rafal, welcome to the Ranch!

What you should do is this:

1. Create a TableModel object which contains the data from the SQL query.

2. Call the JTable's setModel(TableModel) method with that object.

This will automatically redisplay the JTable. No need to call repaint.
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to Paul's comments, when you use a table you would always use a ScrollPane with the table. So the basic code is:



The scroll pane is necessary for the column headers to be displayed.

I suggest you read the Swing tutorial on How to Use Table for basic information and examples.
 
Rafal Szulejko
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, problem solved :p
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!