【问题标题】:Hiding data from a DefaultTableModel to the GUI将数据从 DefaultTableModel 隐藏到 GUI
【发布时间】:2016-04-06 19:10:32
【问题描述】:

我有一个DefaultTableModel,它有 4 列,其中一列是 ID,我不想在表格视图上显示它,但我需要在用户单击时跟踪 ID一行。

private void añadeFilas(boolean europa, boolean caribe) {
    Object[] nuevaFila = new Object[4];
    for (int i = 0; i < agencia.getCruceros().size(); i++) {
        String zona = agencia.getCruceros().get(i).getZona();
        if ((europa && zona.equals("Europa")) || (caribe && zona.equals("Caribe"))) {
            nuevaFila[0] = agencia.getCruceros().get(i).getZona();
            nuevaFila[1] = agencia.getCruceros().get(i).getDenominacion();
            nuevaFila[2] = agencia.getCruceros().get(i).getPuertoSalida();
            nuevaFila[3] = agencia.getCruceros().get(i).getCodigo();

            modeloTabla.addRow(nuevaFila);
        }
    }
}

这是我填充模型的方式,我想隐藏的列是:nuevaFila[3]

我试过这种方式:

public void actionPerformed(ActionEvent arg0) {
                if (tableCruceros.getSelectedRow() != -1) {
                    btSeleccion.setEnabled(true);
                    int fila = tableCruceros.getSelectedRow();
                    String cod = (String) ((Vector) modeloTabla.getDataVector().elementAt(fila)).elementAt(3);
                    crucero = agencia.findByCod(cod);
                    agencia.leerFicheroBarcos(crucero.getCodigoBarco());
                }
                mostrarVentanaCrucero();
            }
        });

但它抛出了这个异常:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 >= 3
at java.util.Vector.elementAt(Unknown Source)
at igu.VentanaPrincipal$5.actionPerformed(VentanaPrincipal.java:238)

238 是 -> String cod = (String) ((Vector) modeloTabla.getDataVector().elementAt(fila)).elementAt(3);

PD。如有语法或拼写错误,请见谅

【问题讨论】:

  • 创建模型,将其应用到表中,获取 TableColumnModel 并从中删除 ID 列,或者创建一个代表您的数据的 POJO,编写一个能够使用它的 TableModel 并且根本不显示ID
  • 我回应了@MadProgrammer 的说法。如果你走后一条路线,你可能会使用 POJO 的实例作为模型中的行,如果是这样,你可能希望你的模型类扩展 AbstractTableModel 而不是 DefaultTableModel。

标签: java swing jtable indexoutofboundsexception defaulttablemodel


【解决方案1】:

感谢@MadProgrammer 和@Hovercraft Full Of Eels,最后我尝试删除该列。

我用过的代码:

TableColumn columna = tableCruceros.getColumn("Codigo");
tableCruceros.removeColumn(columna);

它似乎工作:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-25
    • 2016-03-21
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多