gpt4 book ai didi

java - 从 keyTyped 发送火灾事件后未调用 getTableCellRendererComponent

转载 作者:行者123 更新时间:2023-12-01 15:11:28 26 4
gpt4 key购买 nike

您好,有一个 Jtable,其中有一列具有特定的单元格渲染模型。这种模型,只是在这一列中放置按钮而不是文本数据。

表格的第一个显示效果很好。当我使用更改内容顺序的函数时,仍会为此列调用模型(通过调用 fireTableDataChanged() 强制重绘)。

现在,我希望删除键删除表中的当前行。为此,我使用以下代码:

public void keyTyped(KeyEvent arg0) {
if (arg0.getKeyChar() == KeyEvent.VK_DELETE) {
int currentRow = this.m_data.currentRow();
if (currentRow >= 0) {
this.m_data.deleteRow(currentRow);
System.out.println("mise a jour après destruction de ligne");
this.fireTableRowsDeleted(currentRow, currentRow);
}
}
}

(m_data 是我的表模型)。令人惊讶的是,数据已更新(行已消失),但第一列为空。我的跟踪显示仅调用了 getValueAt() 。未调用 getTableCellRendererComponent()。

我尝试触发 fireTableDataChanged() 而不是 fireTableRowsDeleted(),但它是同一件事。因此它似乎与火功能无关。您知道这个问题的根源以及解决方法吗?

提前致谢。

最佳答案

这是我们用于表模型的代码。你可以使用它。如您所见,它非常简单干净。

            public abstract class BeanTableModel<B> extends ArrayList<B>
implements TableModel {

private List<TableModelListener> listeners = new ArrayList();
private TableModelAttributeList attributeList;

public BeanTableModel() {
super();
}

public BeanTableModel(Object[][] attributes) {
super();
this.attributeList = new TableModelAttributeList(attributes) {
};
}

public TableModelAttributeList getAttributeList() {
return attributeList;
}

public final void setAttributeList(TableModelAttributeList attributeList) {
this.attributeList = attributeList;
}

@Override
public final int getRowCount() {
return size();
}

@Override
public final int getColumnCount() {
if (attributeList != null) {
return attributeList.size();
} else {
return 0;
}
}

@Override
public final String getColumnName(int columnIndex) {
if (attributeList != null) {
return attributeList.getColumnName(columnIndex);
} else {
return null;
}
}

@Override
public final Class<?> getColumnClass(int columnIndex) {
if (attributeList != null) {
return attributeList.getColumnClass(columnIndex);

} else {
return Object.class;
}
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}

@Override
public final Object getValueAt(int rowIndex, int columnIndex) {
try {
if (rowIndex >= 0) {
return (attributeList != null) ? attributeList.getValue(this.get(
rowIndex), columnIndex) : null;
} else {
return get(rowIndex);
}
} catch (Exception ex) {
Logger.getLogger(BeanTableModel.class.getName()).log(Level.SEVERE,
null, ex);
return null;
}
}

@Override
public final boolean add(B e) {
boolean result = super.add(e);

if (result) {
fireListeners();
}

return result;
}

@Override
public final boolean addAll(Collection<? extends B> c) {
boolean result = super.addAll(c);

if (result) {
fireListeners();
}

return result;
}

public final void change(Collection<? extends B> c) {
super.clear();
super.addAll(c);
fireListeners();
}

@Override
public final boolean addAll(int index, Collection<? extends B> c) {
boolean result = super.addAll(index, c);

if (result) {
fireListeners();
}

return result;
}

@Override
public final void clear() {
super.clear();
fireListeners();
}

@Override
public final boolean remove(Object o) {
boolean result = super.remove(o);

if (result) {
fireListeners();
}

return result;
}

@Override
public final boolean removeAll(Collection<?> c) {
boolean result = super.removeAll(c);

if (result) {
fireListeners();
}

return result;
}

@Override
protected final void removeRange(int fromIndex, int toIndex) {
super.removeRange(fromIndex, toIndex);
fireListeners();
}

@Override
public final boolean retainAll(Collection<?> c) {
boolean result = super.retainAll(c);

if (result) {
fireListeners();
}

return result;
}

@Override
public final B remove(int index) {
B b = super.remove(index);
fireListeners();
return b;
}

@Override
public final void add(int index, B element) {
super.add(index, element);
fireListeners();
}

@Override
public final B set(int index, B element) {
B b = super.set(index, element);
fireListeners();
return b;
}

@Override
public final void trimToSize() {
super.trimToSize();
fireListeners();
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//Do Nothing
}

@Override
public final void addTableModelListener(TableModelListener l) {
listeners.add(l);
}

@Override
public final void removeTableModelListener(TableModelListener l) {
listeners.remove(l);
}

public final void fireListeners() {
TableModelEvent evt = new TableModelEvent(this);

for (TableModelListener listener : listeners) {
listener.tableChanged(evt);
}
}

public final ArrayList<B> serialize() {
return new ArrayList<B>(this);
}
}

这是关于格式及其解释方式的:

            public abstract class TableModelAttributeList {

public static final int COLUMN_NAME = 0;
public static final int CLASS = 1;
public static final int ATTRIBUTE_NAME = 2;
public static final int FORMAT = 3;
public static final int REPLACE = 4;
private final Object[][] attributes;
private final Pattern[] patterns;

public TableModelAttributeList(Object[][] attributes) {
this.attributes = attributes;
patterns = new Pattern[attributes.length];

for (int i = 0, size = attributes.length; i < size; i++) {
if (attributes[i][REPLACE] != null) {
patterns[i] = Pattern.compile((String) attributes[i][FORMAT]);
}
}
}

public final String getColumnName(int column) {
return (String) attributes[column][COLUMN_NAME];
}

public final Class<?> getColumnClass(int column) {
return (Class<?>) attributes[column][CLASS];
}

public final int size() {
return attributes.length;
}

public final Object getValue(Object bean, int column) throws
Exception {

try {
Object value = null;

if (attributes[column][ATTRIBUTE_NAME] == null) {
return "";
}

if (attributes[column][ATTRIBUTE_NAME].equals("this")) {
return bean;

} else {
try {
value = PropertyUtils.getProperty(bean,
(String) attributes[column][ATTRIBUTE_NAME]);
} catch (NullPointerException ex) {
return "";

} catch (NestedNullException ex) {
return "";
}
}
if (value == null) {
return null;

} else if (patterns[column] != null) {
Matcher matcher = patterns[column].matcher(String.valueOf(value));
if (matcher.find()) {
return matcher.replaceAll(
(String) attributes[column][REPLACE]);
} else {
return null;
}

} else if (value instanceof Date) {
if (attributes[column][FORMAT] != null) {
DateFormat format = new SimpleDateFormat(
(String) attributes[column][FORMAT]);
return format.format((Date) value);

} else {
return value;
}

} else if (attributes[column][FORMAT] != null) {
return String.format(
(String) attributes[column][FORMAT], value);
} else {
return value;
}
} catch (IllegalAccessException ex) {
String message = "The property named '"
+ (String) attributes[column][ATTRIBUTE_NAME]
+ "' has a private getter method";
throw new Exception(message, ex);
} catch (InvocationTargetException ex) {
String message = "An exception has ocurred while retrieving value for bean property '"
+ (String) attributes[column][ATTRIBUTE_NAME] + "'";
throw new Exception(message, ex);
} catch (NoSuchMethodException ex) {
String message = "The bean doesn't have a property named '"
+ (String) attributes[column][ATTRIBUTE_NAME] + "'";
throw new InvalidModelAttributesException(message, ex);
}
}
}

这就是创建表模型的方式:

            public class SelectEmployeeTableModel extends BeanTableModel<Empleados> {

public static final String NOMBRE = "Nombre";

public static final String DEPARTAMENTO = "Departamento";

public static final String FECHA_BAJA = "FechaBaja";

public SelectEmployeeTableModel() {
super(new Object[][]{
{NOMBRE , Object.class, "nombreCompleto" , null, null},
{DEPARTAMENTO , Object.class, "departamentoId.nombre" , null, null},
{FECHA_BAJA , Object.class, "fechaBaja" , null, null}
});
}
}

第一列是可见列名称,第二列是数据类型(如果要显示复选框,请将其替换为 Boolean.class),第三列是不带“get”前缀的名称,如下所示它使用反射,第四个是格式(对于日期,它可以是“dd/MM/yyyy”或十进制数的“$%.2f”),第五个是用正则表达式替换。

然后在您想要使用它的面板中执行以下操作:

          jtable.setModel(new SelectEmployeeTableModel());

如果您需要更改表模型中的数据

          model.change(collectionName);

如果您只想出于某种原因刷新它,您可以这样做

          model.fireListeners();

并且渲染器从未失败过。至少您没有在实体中正确设置 setter/getter 。

如果您有任何疑问,请不要害怕询问!祝你有美好的一天。

关于java - 从 keyTyped 发送火灾事件后未调用 getTableCellRendererComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12288951/

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