gpt4 book ai didi

java - 重写 AbstractTableModel 类中的 isCellEditable 方法后 JTable 不可编辑

转载 作者:行者123 更新时间:2023-11-29 03:01:47 25 4
gpt4 key购买 nike

我的 AbstractTableModel 的 isCellEditable(row, column); 方法被覆盖,我检查过当我点击有问题的单元格时它返回 truefalse 正确,但是,它不允许我编辑单元格:

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);
}
}
}

最佳答案

代码中有2处错误:

  1. 您要在模型中添加 "123456789""",尽管这些值应该是 Double 和 Integer 类型
  2. 您要为每个列返回 Class.class 作为列类。将 return types[column].getClass(); 替换为 return types[column];

当然,您忘记实现 setValueAt(),所以即使单元格是可编辑的,编辑后的值也不会存储在模型中。

关于java - 重写 AbstractTableModel 类中的 isCellEditable 方法后 JTable 不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34421759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com