将数据从DefaultTableModel隐藏到GUI

时间:2016-01-02 19:13:44

标签: java swing jtable indexoutofboundsexception defaulttablemodel

我有一个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是 - &gt; String cod =(String)((Vector)modeloTabla.getDataVector()。elementAt(fila))。elementAt(3);

PD。抱歉,如果有任何语法或拼写错误

1 个答案:

答案 0 :(得分:1)

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

我使用过的代码:

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

似乎有效:)