在从keyTyped发送的触发事件后未调用getTableCellRendererComponent

时间:2012-09-05 20:06:12

标签: java jtable tablecellrenderer

您有一个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(),但它是同样的事情。因此它似乎与火功能无关。有关这个问题的根源以及解决方法的方法吗?

提前致谢。

2 个答案:

答案 0 :(得分: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”表示十进制数),第五种是用reggex替换。

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

          jtable.setModel(new SelectEmployeeTableModel());

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

          model.change(collectionName);

如果您只是出于某种原因想要刷新,那么

          model.fireListeners();

并且渲染器从未失败过。少了,你就不在实体中设置吸气剂。

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

答案 1 :(得分:0)

好的,最后我发现了问题。我有一个ListSelectionListener,在某些情况下,它发送了一个fireTableCellUpdated(-1,0)。这是“stupid Java”解释为“删除表呈现”的命令。