我正在尝试使用JTable来更新Java SWING应用程序中的用户,目前当我编辑单元格时,只有单击一个不同的单元格才能获取编辑结果,从而触发tableModelListener。我希望能够在按钮事件上获取这些更改的值,而无需单击其他单元格。
我的表格定义:
DefaultTableModel tableModel = new DefaultTableModel();
table = new JTable(tableModel);
tableModel.addColumn("Key");
tableModel.addColumn("Value");
if (PatientView.getSelected() != null){
tableModel.addRow(new Object[]{"Name", PatientView.getSelected().getName()});
tableModel.addRow(new Object[]{"Age", PatientView.getSelected().getAge()});
tableModel.addRow(new Object[]{"Height", PatientView.getSelected().getHeight()});
tableModel.addRow(new Object[]{"Weight", PatientView.getSelected().getWeight()});
tableModel.addRow(new Object[]{"BMI", PatientView.getSelected().getCalculatedBMI()});
}
表监听器
tableModel.addTableModelListener(new TableModelListener(){
@Override
public void tableChanged(TableModelEvent arg0) {
int row = arg0.getFirstRow();
int column = arg0.getColumn();
Object data = tableModel.getValueAt(0, 1);
System.out.println(data);
}
});
按钮动作侦听器
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Vector data = tableModel.getDataVector();
System.out.println(table.getValueAt(0, 1));
System.out.println(data);
}
});