1
我的問題是我想在jTable中的組合框上有itemSateChanged監聽器。當我更改組合框的值時,我想將數據添加到jTable的受尊敬的行中的單元格。在jTable裏面的組合框上觸發itemStateChanged事件
這是創建組合框的代碼。
public void setUpSectionColumn(JTable table, TableColumn statusColumn) {
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Lending");
comboBox.addItem("Childen Lending");
comboBox.addItem("Reference");
comboBox.addItem("Special Collection");
statusColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
statusColumn.setCellRenderer(renderer);
}
這是我來爲不工作聽衆的方法。
public void fillTable2() {
jTable2.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent evt) {
int row = jTable2.getSelectedRow();
String section;
if (row == -1) {
section = "Lending";
} else {
section = jTable2.getValueAt(row, 3).toString();
}
int accessNo = bdao.getLastAccessNo(section);
DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
Object[] rowdata = {Integer.toString(accessNo), "", ""};
model.addRow(rowdata);
}
});
}
肯定是可能的,但在大多數情況下是不是有某種原因覆蓋的JComboBox與的ItemListener,我會在的DefaultTableModel覆蓋setValueAt,生成混凝土行 – mKorbel