Results 1 to 7 of 7
  1. #1
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default creating a Jtable using swing

    Hi ,

    I'm connecting to db2 using a JDBC driver and then running the piece of code below :

    while (rs.next()) {
    String a1 = rs.getString(1);
    String a2 = rs.getString(2);
    String a3 = rs.getString(3);
    String a4 = rs.getString(4);
    String a5 = rs.getString(5);
    System.out.printf(" %s %s %s %s %s\n", a1, a2, a3, a4, a5);
    System.out.print("");
    }

    Then I get the below output:

    T01KATD1 58105 TKUGOM1 db2jcc_application UOWWAIT
    T01KATD1 58105 TKUGOM1 db2jcc_application UOWWAIT
    T01KATD1 58105 TKUGOM1 db2jcc_application UOWWAIT
    T01KATD1 58105 TKUGOM1 db2jcc_application UOWWAIT

    Now I wonder what do I need to do to display/insert the above output into a Jtable. My intention is to create a a small monitor tool uisng GUI's and then basically in the GUI the window should look like:


    DBNAME AGENT_ID AUTHID APPL_NAME APPL_STATUS
    ---------------------------------------------------------
    T01KATD1 58105 TKUGOM1 db2jcc_application UOWWAIT
    T01KATD1 58105 TKUGOM1 db2jcc_application UOWWAIT
    T01KATD1 58105 TKUGOM1 db2jcc_application UOWWAIT
    T01KATD1 58105 TKUGOM1 db2jcc_application UOWWAIT


    Please any ideas will be highly appreciated. Thanks.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Its been a while, but I remember something about needing to create a JTableModel

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yep, I'd look into creating a DefaultTableModel object and filling it with rows that consist of data held in each result set row.

    e.g.,
    Java Code:
          String[] columnNames = {"DB Name", "Agent ID", "Auth ID", "Appl Name", "Appl Status"};
          DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0); // load column names and 0 rows
          //....   get data from database
          while (rs.next()) {
             // create an array of Strings holding one row of data
             // use addRow(...) method to add data array as a row
          }
          
          // then set your JTable to use the tableModel object above as its model

  4. #4
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Thanks a lot for this tip. Actually I implemented my tablemodel but I was having some difficulties getting the data so the contructors available in the tablemodel can take it and process it as you specify but I'll try your suggestion which sounds quite good to me. Thanks again.

  5. #5
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Actually after I started trying to implement my tablemodel I was heading in the right track but now from some reason I'm just loading one row. I added some displays/prints into the program and I can see that my array has more data than 1 row but not sure. Below is a copy of my tablemodel:

    class MyTableModel extends AbstractTableModel {
    String[] columnNames = {"DBNAME","AUTHID","APPLID","APPLSTATUS","APLLSTS" };
    Class1 arrayRetrieval = new Class1();
    Object[][] data = arrayRetrieval.getData();

    public int getColumnCount() {
    return columnNames.length;
    }

    public int getRowCount() {
    return data.length;
    }

    public String getColumnName(int col) {
    return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
    return data[row][col];
    }


    and the below code is how I get the data from DB2:

    Object[][] data1 = new Object[20][]; // {{"","","","",""}};
    DBConnect testSQL = new DBConnect();
    try {
    do {
    data1 = testSQL.test(driver, url, user, password, sql);
    } while (data1 == null);
    } catch (SQLException ex) {
    Logger.getLogger(TableDemo.class.getName()).log(Le vel.SEVERE, null, ex);
    }
    return data1;

  6. #6
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Hi ,

    Finally I got this to work. Below is the code I'm using now.

    Java Code:
    while (rs.next()) {
                ArrayList<Object> al = new ArrayList<Object> ();
                al.add (rs.getString(1));
                al.add (rs.getString(2));
                al.add (rs.getString(3));
                al.add (rs.getString(4));
                al.add (rs.getString(5));
    
                Object[] o = new Object[al.size()];
                al.toArray(o);
    
                Object[][] obj2 = new Object[resultCount][];
                System.arraycopy(obj, 0, obj2, 0, obj.length);
                obj = obj2;
                obj[resultCount-1] = o;
                resultCount++;

    Another thing I wanted to ask is whether if I can add Jbuttons to this Jtable or I have to create a Jpanel and then in there create the Jbuttons. Thanks again to all of you for tips.:)

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by hariza View Post
    Another thing I wanted to ask is whether if I can add Jbuttons to this Jtable or I have to create a Jpanel and then in there create the Jbuttons. Thanks again to all of you for tips.:)
    Read this: Adding a JButton to the Cells of a Jtable :)

Similar Threads

  1. How to add JTextArea in JTable - Swing
    By phungho2000 in forum AWT / Swing
    Replies: 5
    Last Post: 12-17-2008, 09:18 PM
  2. Java Swing JTable Simple Doubt
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 11-26-2008, 01:46 PM
  3. creating executable jars from Swing programs
    By gotenks05 in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 01:51 AM
  4. Swing problem: JTable/TableModel
    By Levish2002 in forum AWT / Swing
    Replies: 2
    Last Post: 08-24-2008, 08:53 PM
  5. Creating popup menus with Swing
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •