关于网友提出的“ 如何再程序中终止JTable的编辑状态?”问题疑问,本网通过在网上对“ 如何再程序中终止JTable的编辑状态?”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 如何再程序中终止JTable的编辑状态?
描述: JTable的编辑状态不能自动终止,只有将光标移开后才终止,并保存数据,如果cell还处于编辑状态就取不到新值,我试了将JTable中的EditingStoped方法的实现拿出来做,还试不行?
求教了!!!
我的做发:
1,响应保存数据事件(button action)
2,判断table是否处于编辑状态:isTableEditing()
3,取出editer,(JTable中的EditingStoped的实现方法)
TableCellEditer editer = table.getCellEditer();
Object o = editer.getValue();
可取出的o = null???
为什么?我改怎么做?
解决方案1: to caeserwliu (白云) :你得那个问题可以解决,我上面得代码没有任何错误。我之所以出现那个问题是因为我覆盖了editingStopped()函数才会出现那个问题,现在我已经解决了。
你不要覆盖editingStopped()方法(否则话,编辑后cell周围仍有那个黑框,且处于编辑状态),在第二步判断如果正处于编辑状态。则用以下代码:
--------------------------------
CellEditor ce = JTable.getCellEditor(row_edit, col_edit);
ce.stopCellEditing();
Object value = ce.getCellEditorValue();
DefaultTableModel tableModel = (DefaultTableModel)JTable.getDefaultModel();
tableModel.setValueAt(value, row_edit, col_edit);
--------------------------------
TableCellEditor中没有getValue()这个函数。的调用CellEditor.getCellEditorValue()
获取编辑后得新值,然后在TableModel中用
setValueAt(Object aValue, int rowIndex, int columnIndex) 保存编辑后得值,
然后用tableModel.fireTableDataChanged(); 刷新JTable。
以上介绍了“ 如何再程序中终止JTable的编辑状态?”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3582301.html