如何刷新JTable?使用來自Access數據庫的JcomboBox項目刷新JTable數據
這是我的代碼。
public void itemStateChanged(ItemEvent evt)
{
String text=(String)to_Cmb2.getSelectedItem();
try
{
// Connect to an Access Database
Connection con=DriverManager.getConnection("jdbc:odbc:flightdsn");
Statement s=con.createStatement();
// Read data from a table
ResultSet rs = s.executeQuery("SELECT FlightNo,City,To,Arrives,Departs FROM I_Flights_Routes WHERE To ='"+text+"' ");
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++)
{
columnNames.addElement(md.getColumnName(i));
}
// Get row data
while (rs.next())
{
Vector<Object> row = new Vector<>(columns);
for (int i = 1; i <= columns; i++)
{
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
rs.close();
s.close();
con.close();
}
catch (Exception e)
{
System.out.println(e);
}
// Create table with database data
JTable table = new JTable(data, columnNames)
{
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(50,10,400,200);
td.add(scrollPane);
// td is Jpanel object
td.setVisible(true);
}
你有什麼錯誤? – AndreDuarte 2014-09-10 16:36:41
當我從JComboBox中選擇下一個項目JTable劑量未用新數據更新時, – 2014-09-10 16:39:01
並在另一個表格後面顯示新表格,其中包含以前的數據行和新行中的新數據。 – 2014-09-10 16:44:31