简体   繁体   中英

Hiding data from a DefaultTableModel to the GUI

I have a DefaultTableModel that have 4 columns, one of the columns is a ID and I don't want to show it on the view of the table but I need to keep track of the ID when the user clicks on a row.

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

This is how I fill the model and the column I want to hide is: nuevaFila[3]

I have tried this way:

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

But it throws me this exception:

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 is -> String cod = (String) ((Vector) modeloTabla.getDataVector().elementAt(fila)).elementAt(3);

PD. sorry if there is any grammar or spelling mistake

Thanks @MadProgrammer and @Hovercraft Full Of Eels, finally I tried removing the column.

The code i have used:

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

and it seems to work :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM