我尝试为我正在进行的项目创建一个自定义单元格编辑器,我需要实现的是在我的单元格编辑器中完成编辑时触发表模型更改事件。我使用了自定义单元格编辑器和自定义单元格渲染器以及JXTable。
我尝试将TableModelListener绑定到表中,但是当我更改值时没有任何反应。我还将一个选择更改的侦听器附加到表格单元格的激活编辑模式。
我需要在TableModelListener中执行行System.out.println("ob = " + ob.toString());
,以便在表内容发生变化时我可以做任何我想做的事。
以下是我的问题的完整代码。
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
public class RendererTest extends javax.swing.JFrame {
public RendererTest() {
initComponents();
testresults_editor_grid.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
int col = testresults_editor_grid.getSelectedColumn();
int row = testresults_editor_grid.getSelectedRow();
if (row > -1) {
if (testresults_editor_grid.isCellEditable(row, col)) {
testresults_editor_grid.editCellAt(row, col);
testresults_editor_grid.setSurrendersFocusOnKeystroke(true);
testresults_editor_grid.getEditorComponent().requestFocusInWindow();
((JTextField) testresults_editor_grid.getEditorComponent()).selectAll();
}
}
}
});
testresults_editor_grid.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
testresults_editor_grid.setRowHeight(25);
testresults_editor_grid.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE) {
int c = e.getColumn();
int r = e.getFirstRow();
Object ob = testresults_editor_grid.getValueAt(r, c);
System.out.println("ob = " + ob.toString());
}
}
});
}
private void fillTable() {
Object[] cols = {"DESCRIPTION", "RESULTS(Editable)", "REF. RANGE"};
Object[][] data = {
{"ASHAN", 0, "60-100"},
{"B", 0, "1000-4000"},
{"C", 0, "10000-60000"}
};
DefaultTableModel def = new DefaultTableModel(data, cols) {
Boolean[] editable = {};
Boolean[] canEdit = {false, true, false};
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit[columnIndex];
}
};
testresults_editor_grid.setModel(def);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
testresults_editor_grid = new javax.swing.JTable();
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.BorderLayout(0, 5));
testresults_editor_grid.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{},
{},
{},
{}
},
new String [] {
}
));
testresults_editor_grid.getTableHeader().setReorderingAllowed(false);
jScrollPane1.setViewportView(testresults_editor_grid);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));
jButton1.setText("Fill Table");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.add(jButton1);
getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
fillTable();
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RendererTest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable testresults_editor_grid;
// End of variables declaration
}
如果有人可以帮我解决这个问题,或者指导我朝着正确的方向前进,我们非常感激。
提前致谢。
我自己纠正了这个问题。我错过的是在将新模型对象附加到表格后忘记重新附加TableModel的监听器。