我有如何使用固定标题列刷新JTable的问题,实际上我可以显示表格内容,如果我点击menuitem然后它会根据点击的menuitem进行刷新。这是我的源代码:
public class PanelTableItem {
Object rowData[][]=null;
JTable mainTable ;
JTable fixedTable;
public JPanel getPanelDetailItem(String separate,String group) throws ClassNotFoundException, SQLException {
String tseparate=null;
String tgroup=null;
if (separate!=null && !separate.isEmpty()) {tseparate=separate;}else {tseparate="COLLECTION 1";}
if (group!=null && !group.isEmpty()) {tgroup=group;}else {tgroup="SOFA";}
final Object rowData[][]=new OtherClass().getItem(tseparate, tgroup);
final String columnNames[] = { "Item Code", "Name"};
final TableModel fixedColumnModel = new AbstractTableModel() {
public int getColumnCount() {return 1;}
public String getColumnName(int column) {return columnNames[column];}
public int getRowCount() {return rowData.length;}
public Object getValueAt(int row, int column) {return rowData[column][row];}
};
final TableModel mainModel = new AbstractTableModel() {
public int getColumnCount() {return columnNames.length - 1;}
public String getColumnName(int column) {return columnNames[column + 1];}
public int getRowCount() {return rowData.length;}
public Object getValueAt(int row, int column) {return rowData[column + 1][row];}
};
fixedTable = new JTable(fixedColumnModel);
fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
mainTable = new JTable(mainModel);
mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
ListSelectionModel model = fixedTable.getSelectionModel();
mainTable.setSelectionModel(model);
JScrollPane scrollPane = new JScrollPane(mainTable);
Dimension fixedSize = fixedTable.getPreferredSize();
JViewport viewport = new JViewport();
viewport.setView(fixedTable);
viewport.setPreferredSize(fixedSize);
viewport.setMaximumSize(fixedSize);
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
scrollPane.setRowHeaderView(viewport);
JPanel panelTableItem=new JPanel();
panelTableItem.add(scrollPane, BorderLayout.CENTER);
panelTableItem.setVisible(true);
return panelTableItem;
}
}
这是我的otherclass.getItem:
public Object[][] getItem(String separate,String group) throws ClassNotFoundException, SQLException {
setConnection();
String SQL = "SELECT * FROM mitem WHERE tampil=1 AND separate='"+separate+"' AND groups='"+group+"' ORDER BY name";
System.out.println(SQL);
Object[][] arritem= new Object[getCountRowSQL(SQL)][getCountRowSQL(SQL)];
Connection conn = DriverManager.getConnection(getURL());
try{
Statement stmt = conn.createStatement();
try {
stmt.setQueryTimeout(getTimeout());
final ResultSet rs = stmt.executeQuery(SQL);
try {
int ctr=0;
while (rs.next()) {
arritem[0][ctr]=rs.getString("item_code");
arritem[1][ctr]=rs.getString("name");
//arritem[2][ctr]=rs.getString("item_code");
ctr++;
}
}finally {
try { rs.close(); } catch (Exception ignore) {}
}
}finally {
try { stmt.close(); } catch (Exception ignore) {}
}
}finally {
try { conn.close(); } catch (Exception ignore) {}
}
return arritem;
}