Hi, everyone, I just start study Swing . I have some small question about the JTable.
Is there anyway I can make the Jtable to auto resize during the runtime when I maximize the window?
My code did not display the stuff I put onto the Table, what did I do wrong ?
Also, later, if I was to have a new sql statement like
"SELECT * FROM WAITING_ITEMS WHERE ID= 103 "
onto the same table, how do I cause the table to display the new ResultSet ?

This is what I got so far: (I love the wrap code feature :) )

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;


class Main extends JFrame 
{    
    private JScrollPane myScrollPane;
    private JTable myTable;
    private JPanel myPanel;
    
    public Main ()
    {
        try
        {  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");   }
        catch(ClassNotFoundException cnfe)
        { cnfe.printStackTrace(System.err);  }

        Connection con = null;
        Statement stmt = null;
        try
        {
            // Connect to the Inventory database
            con = DriverManager.getConnection("jdbc:odbc:BookInventory");
             // Create statement objects.
            stmt = con.createStatement();  
            
            initialize();
            //the table name is WAITING_ITEMS
            createTable(stmt.executeQuery("SELECT * FROM WAITING_ITEMS"));
            stmt.close();
            con.close();
        }
        catch(SQLException sqle)
        { sqle.printStackTrace(System.err); }
    }
    public void initialize() {
        myScrollPane = new JScrollPane();
        myTable = new JTable ();
        myScrollPane.setViewportView(myTable);
        myPanel = new JPanel();
        myPanel.add(myScrollPane);
        
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        setContentPane(myPanel);
        
        pack();
    }
    public void createTable (ResultSet rs) {
        Vector data = null;
        Vector columnNames = null;
        try {
            data = getRowData(rs);;
            columnNames = getColumnName(rs);
        }
        catch (SQLException ex) {
            ex.printStackTrace();
        }
        myTable = new JTable(data,columnNames);
        //The repaint doesn't seem to work
        myTable.repaint();
    }
    public Vector getColumnName (ResultSet rs) throws SQLException {
        ResultSetMetaData meta = rs.getMetaData();
        Vector colNames = new Vector ();
        
        for( int col = 0; col < meta.getColumnCount(); col++) {
           colNames.add(meta.getColumnName(col+1));
        }        
        return colNames;
    }
    public Vector  getRowData (ResultSet table) throws SQLException {
        ResultSetMetaData meta = table.getMetaData();
        String[] colNames = new String[meta.getColumnCount()];
        Vector cells = new Vector();
        
        for( int col = 0; col < colNames.length; col++) {
            colNames[col] = meta.getColumnName(col + 1);
            cells.add(new Vector());
        } 

         // hold data from result set
        while(table.next()) {
            for(int col = 0; col < colNames.length; col++) {
               Object cell = table.getObject(colNames[col]);
               ( (Vector)cells.elementAt(col)).add(cell);
            }
        }
        return cells;
    }
    
    public static void main(String args[]) {
        //*** NEED TO LOOK UP MORE INFORMATION ***
        //don't know why this code is needed to run the GUI
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
}

hello blue,
the normal way of working with the data in a table is to get the data into the table model class. The Table model mostly accepts Object Arrays. The result set data must be converted into an Object Array. The Object Array then passed to the TableModel as an argument to the constructor. The resultant TableModel then should be passed as a constructor to the JTable that you will create.
Runtime modification of the UI will only be possible if you work with the undellying data model. This is true for most UI components.

as far as your code goes, your colnames have not been retrieved but the colcount has been.

secondly invokeLater is a good mechanism to run our programs as separate threads.

If you still have difficulty with the table, send me your email ID, I will send you some sample JTable Code

Thanks to antaryami :), I was able to look up more information on Jtable and table model ( Shame on me for ignore interface class in the sun api). I am now able to create my own MyTableModel extending AbstractTableModel to displace with the JTable.

However, I hit a slight problem. I was looking around and found the JdbcRowSetImpl class, it seem to be pretty useful since it do update both to the JTable and the database, so I put it inside MyTableModel.
The problem is that I have implement some basic method like addRow, deleteRow, updatRow, search... inside the MyTableModel, however, I can't figure out how to call those methods.

The solution I could think of right now is to create my own Jtable that have those methods call up. Is there any other way around it ? or I shouldn't have put those method inside MyTableModel at all in the first place ?

This article has been dead for over six months. Start a new discussion instead.