We help IT Professionals succeed at work.
Get Started

Convert table data to a JTable so it can be displayed on a form

Lettis
Lettis asked
on
229 Views
Last Modified: 2010-03-31
I am trying to create a class which will create a JTable with Column Headings and the data from the table.

I can extract the column headings easily enough with code like:

Using these variables:

        String ColumnNames[];
        Object RowData[][];
        int ColumnCount = 0;
 
      // First establish how many columns we've been sent
        ColumnCount = theData.getMetaData().getColumnCount();


   // Get the columnNames we were sent
        for(int x=1;x<ColumnCount+1;x++){
          theColumnName = theData.getMetaData().getColumnName(x);}

Where theData is the RecordSet sent to class.

However, I am struggling to extract the data into the object array:

   // Get the Data from the Query
        try{
            int y = 1;            
            theData.beforeFirst();
            theData.next();
           
            while(theData.next()) {
                (for int z = 1; z<x;z++){
                 ' RowData is defined as an object array
                 ' Cant get this bit to work no matter what I put
                 RowData[][] = {??????};
                 }
                x+=1;
            }
           
        }
        catch(Exception ex){
            // If something went wrong here simply throw an exception back to the
            // calling routine
            throw ex;
        }

If RowData is defined as an Object array like Object RowData[][]; then how do I achieve this part? I won't know how many columns are being sent or how many rows but I need to put the data into an object so I can put the data into a JTable:

         final JTable table = new JTable(RowData, columnNames);

and then return the "table" to the calling routine.


I want to do this as a separate class so that I can reuse as I will be interrogating many tables within my application and I need this class (or something similar) to display the data that's currently in the table they are trying to add a record to.

Any suggestions would be appreciated


Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE