4 Replies - 5306 Views - Last Post: 23 April 2011 - 07:25 PM Rate Topic: -----

#1 damiencourt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 11-April 11

How to use JTable to represent data from database table

Posted 23 April 2011 - 01:26 PM

Hi guys,
I need a way to retrieve data from a mysql table and display it in a JTable. At the moment I am reading the data into an array of objects, each object representing a record from the table. My problem is that JTable takes two dimensional arrays. I was thinking of reading the data straight into a 2dim array and then display it in a table, but read somewhere that this is not a good idea(they didn't say why is that). So I look for an advice...
Thank You

Is This A Good Question/Topic? 0
  • +

Replies To: How to use JTable to represent data from database table

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,320
  • Joined: 27-December 08

Re: How to use JTable to represent data from database table

Posted 23 April 2011 - 01:49 PM

What you really want to do is instantiate a TableModel object like DefaultTableModel, and add the elements to the TableModel. Then instantiate the JTable, passing the TableModel to the constructor. Remember that the TableModel is used to manage the data the JTable is displaying.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: How to use JTable to represent data from database table

Posted 23 April 2011 - 01:50 PM

View Postdamiencourt, on 23 April 2011 - 03:26 PM, said:

My problem is that JTable takes two dimensional arrays.

So just make an array data[numberOfRow][1]
and put in
data[0][0] your first row
data[1][0] your second row
...
data[n][0] the last row

or write a method to do it

// converts a one dimensional array of Object into a 2 dimensional array
Object[][] convert(Object in[]) {
   Object[][] out = new Object[in.length][1];
   for(int i = 0; i < in.length; ++i)
      out[i][0] = in[i];
   return out;
}


Was This Post Helpful? 1
  • +
  • -

#4 damiencourt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 11-April 11

Re: How to use JTable to represent data from database table

Posted 23 April 2011 - 07:19 PM

Thanks guys, got it working with a loop similar to pbl's one.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: How to use JTable to represent data from database table

Posted 23 April 2011 - 07:25 PM

Good. Happy I could help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1