我的AbstractTableModel的isCellEditable(row, column);
方法被覆盖了,我已经检查过,当我点击相关单元格时,它会正确地返回true
或false
,但是,它不允许我编辑单元格:
private class KAMModel extends AbstractTableModel {
private final String[] column = {"Member Number", "Member Name", "Secondary Name", "Address", "Phone", "Tickets"};
private final Class<?>[] types = {Double.class, String.class, String.class, String.class, String.class, Integer.class};
private final boolean[] editable = {false, false, false, false, false, true};
private ArrayList<Object[]> data;
// skip other overridden methods
@Override
public boolean isCellEditable(int row, int column) {
System.out.println(editable[column]); // prints correct true/false based on the column
return editable[column];
}
}
为什么我会看到isCellEditable
被解雇,并且还告诉我我的列是否可编辑,但我无法编辑列。它只是突出了一行。单元格已勾画出,但我无法对其进行编辑。
更新:
可运行代码:
package kiss;
import java.math.BigDecimal;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class kiss extends javax.swing.JFrame {
private KAMModel kam = new KAMModel();
public kiss() {
initComponents();
kam.search("", "", "", "", "");
}
@SuppressWarnings("unchecked")
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(kam);
jScrollPane1.setViewportView(jTable1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(kiss.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(kiss.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(kiss.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(kiss.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new kiss().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
private class KAMModel extends AbstractTableModel {
private final String[] column = {"Member Number", "Member Name", "Secondary Name", "Address", "Phone", "Tickets"};
private final Class<?>[] types = {Double.class, String.class, String.class, String.class, String.class, Integer.class};
private final boolean[] editable = {false, false, false, false, false, true};
private ArrayList<Object[]> data;
public KAMModel() {
super();
}
@Override
public Object getValueAt(int row, int col) {
return data.get(row)[col];
}
@Override
public int getColumnCount() {
return column.length;
}
@Override
public String getColumnName(int i) {
return column[i];
}
@Override
public int getRowCount() {
if (data == null) return 0;
return data.size();
}
@Override
public Class getColumnClass(int column) {
return types[column].getClass();
}
@Override
public boolean isCellEditable(int row, int column) {
System.out.println(editable[column]);
return editable[column];
}
public void search(String memberNumber, String acctNumber, String memberName, String phoneNumber, String address) {
data = new ArrayList<>();
data.add(new Object[] {"123456789", "John Smith", "Jane Smith", "123 Main St.", "555-555-1212", ""});
fireTableDataChanged();
}
@Override
public void fireTableDataChanged() {
super.fireTableDataChanged();
resizeColumns();
}
public void resizeColumns() {
float[] percents = {8f, 20f, 20f, 39f, 10f, 3f};
int tableWidth = jTable1.getWidth();
TableColumn tc;
TableColumnModel tcm = jTable1.getColumnModel();
for (int x = 0; x < 6; x++) {
tc = tcm.getColumn(x);
int cwidth = Math.round(percents[x] * tableWidth);
tc.setPreferredWidth(cwidth);
}
}
}
}
答案 0 :(得分:4)
代码中有2个错误:
"123456789"
和""
,但这些值应该是Double和Integer类型Class.class
作为列类。将return types[column].getClass();
替换为return types[column];
。当然,您忘记实施setValueAt()
,因此即使单元格是可编辑的,编辑后的值也不会存储在模型中。