[英]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. 我有一个具有4列的DefaultTableModel
,其中一列是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);
}
}
}
This is how I fill the model and the column I want to hide is: nuevaFila[3]
这是我填充模型的方式,我要隐藏的列是: 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); 238是->字符串编码=(字符串)((向量)modeloTabla.getDataVector()。elementAt(fila))。elementAt(3);
PD. PD。 sorry if there is any grammar or spelling mistake 对不起,如果语法或拼写错误
Thanks @MadProgrammer and @Hovercraft Full Of Eels, finally I tried removing the column. 感谢@MadProgrammer和@Hovercraft Full Of Eels,最后我尝试删除该列。
The code i have used: 我使用的代码:
TableColumn columna = tableCruceros.getColumn("Codigo");
tableCruceros.removeColumn(columna);
and it seems to work :) 它似乎有效:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.