0

im am try to fetch data from MS Access database into a JTable.When i run this code snippet in console its working fine but when i do it on a button event in netbeans its not showing any thing.I am using following code and my table name is table1.

     private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                          

    Vector columnNames = new Vector();
    Vector data = new Vector();

    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:rrr");
    String sql = "Select * from client";
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery( sql );
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    for (int i = 1; i <= columns; i++) {
      columnNames.addElement( md.getColumnName(i) );
    }
    while (rs.next()) {
    Vector row = new Vector(columns);
    for (int i = 1; i <= columns; i++){
      row.addElement( rs.getObject(i) );
    }
    data.addElement( row );
    }
    rs.close();
    stmt.close();
    }
    catch(Exception e){
   System.out.println(e);
    }
    JTable table = new JTable(data, columnNames);
    TableColumn col;
    for (int i = 0; i < table.getColumnCount(); i++) {
    col = table.getColumnModel().getColumn(i);
     col.setMaxWidth(250);
    }


    }          
0

2 Answers 2

1

First you need a top level window, which should be doable with the Netbeans framework (which I guess you're using).

Then add the table and the button to that window and register an Action or ActionListener to the button. In that action you spawn a new worker thread (e.g. SwingWorker), load the date inside that thread and update the table's model when finished.

Sign up to request clarification or add additional context in comments.

2 Comments

thank @Thomas but im new to swing can u help me out with a code snippet for above..
@prakash_d22 have a look at the Swing tutorials at the oracle site. You should get a basic sence of how Swing works.
1

In your current solution, you make a new JTable instance when the button is pressed. However, that JTable is not added to any component, so once your actionPerformed method returns, the reference to the JTable is lost and it will not show.

JTable table = new JTable(data, columnNames);
TableColumn col;
for (int i = 0; i < table.getColumnCount(); i++) {
  col = table.getColumnModel().getColumn(i);
   col.setMaxWidth(250);
}
//here is a missing statement, like panel.add( table );

If you already have an existing table, it might be better to update the model of that existing table instead of creating a new one, as Thomas already suggested. Also, consider constructing the table model on another thread then the EDT by using a SwingWorker. This avoids a blocked UI while the database is queried and the table constructed. Example usages of the SwingWorker can be found in the Swing Concurrency tutorial

1 Comment

Code snippets (actually, full working examples) can be found in the tutorial to which I linked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.