I'm Using Camick's ListTableModel and RowTableModel, NO DATA IN JTABLE
I am trying to use the ListTableModel and tried following some examples online and still finding it difficult to show 'resultSet' data in my Jtable. Im using the model-view-controller method building a GUI application in netbeans. All of my GUI components are placed in views and the controller is handling any updates as well visibility to this view. The model class has all the queries and is responsible for accessing the database directly using JDBC.
The "Jtable" "searchTable" is created and initialized inside the view class and is being accessed by my controller via a method (getSearchTable()).
public JTable getSearchTable(){
// view class
return searchTable;
}
I have the "ListTableModel" being created inside the controller class and following Camick's tutorial.
private Product_View productView;
private Product_Model productModel;
private Product product;
public Product_Controller(Product_Model model, Product_View view){
this.productMode开发者_如何学Gol = model;
this.productView = view;
view.addSaveListener(new SaveListener());
view.addCloseListener(new CloseListener());
view.addSearchSingleListener(new SearchListener());
}
public void displayProductView(){
this.productView.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.productView.setVisible(true);
} class SearchListener implements ActionListener {
public SearchListener() {
}
@Override
public void actionPerformed(ActionEvent ae) {
ListTableModel model = null;
String prodName = productView.getProductName();
JTable tempTable = productView.getSearchTable();
model = productModel.getProduct(prodName, model);
tempTable = new JTable(model);
}
}
Finally the method "getProduct" is inside the model class and being called by my controller class.
public ListTableModel getProduct(String productName, ListTableModel model){
String query = "SELECT * FROM Products WHERE name='" + productName + "';";
Statement stmt;
try {
stmt = this.conn.createStatement();
stmt.executeQuery (query);
ResultSet rs = stmt.getResultSet();
writeResultSet(rs);
model = ListTableModel.createModelFromResultSet(rs);
rs.close ();
stmt.close ();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return model;
}
The Jtable does not return anything, how can i view my ResultSet data (i have printed out the values being returned by "writeResultSet" to the console and so the query is working correctly?
Creating a table doesn't add the table to the GUI. Somewhere in your code you need code like:
frame.add( table );
If you are updating an existing table then you don't create a new table all you do is:
table.setModel(...);
after recreating the model.
Edit:
To test your SQL create a simple SSCCE with code something like:
ListTableModel model = ListTableModel.createModelFromResultSet( resultSet );
JTable table = new JTable( model );
frame.add( new JScrollPane( table ) );
frame.setSize(...);
frame.setVisible( true );
精彩评论