0

您好,有一个 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() 但它是一回事。因此,它似乎与火函数无关。关于这个问题的起源以及解决方法的任何想法?

提前致谢。

4

2 回答 2

1

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

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

渲染器从未失败过。至少你没有在实体中设置吸气剂。

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

于 2012-09-05T20:46:02.770 回答
0

好的,我终于找到了问题所在。我有一个 ListSelectionListener,在某些情况下,它发送了一个 fireTableCellUpdated(-1,0)。这是“愚蠢的Java”解释为“删除表格渲染”的命令。

于 2012-09-08T17:34:21.300 回答