//下面是我的table
class Store_table
{
Object data[][];
Object name[]={"商品编号","商品名称","价格","上架日期","保质日期","库存"};
Store_table(String bq)
{
String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=Store;IntegratedSecurity=True";//
// Declare the JDBC objects.
Connection con=null;
Statement sql=null;
ResultSet rs=null;
try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url);
sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery("SELECT * FROM 仓库 where 标签='"+bq+"'");
rs.last();
data=new Object[rs.getRow()][6];
// Iterate through the data in the result set and display it.
int i=0;
rs.beforeFirst();
while (rs.next()) {
long number=rs.getLong("商品编号");
String name=rs.getString("商品名称");
float price=rs.getFloat("价格");
Date date=rs.getDate("上架日期");
int staydate=rs.getInt("保质日期");
int repertory=rs.getInt("库存");
data[i][0]=number;
data[i][1]=name;
data[i][2]=price;
data[i][3]=date;
data[i][4]=staydate;
data[i][5]=repertory;
i++;
}
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null)
try {
rs.close();
} catch (Exception e) {
}
if (sql != null)
try {
sql.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
}
}
}
//我用JComboBox调用和更新JTable
public void actionPerformed(ActionEvent arg0) {
String a=comboBox.getSelectedItem().toString();
store=new Store_table(a);
//把store放进JTable
ta=new JTable(store.data,store.name)
{
public boolean isCellEditable(int row, int column) {return false;};//设置列表数据不能修改
};
//把ta列表放进滚动列表
ta.setModel(model);
JScrollPane jsp=new JScrollPane(ta);
//JScrollPane jsp=new JScrollPane(store);
//getContentPane().add(jsp,BorderLayout.CENTER);
ta.setBackground(Color.ORANGE);
jsp.setBackground(Color.GRAY);
//jsp.setBackground(Color.ORANGE);
contentPane.add(jsp);
jsp.setBounds(0, 41, 743, 252);
}
});
怎么让JTable在JComboBox改动的时候更新呢?
据说需要setModel 再repaint 再updateUI
可是具体怎么做呢?