Good afternoon,
I have the following problem and I want to visualize all the data of an arrayList of type Counter, which is an object that I have created, and I do not know how to add all the counters to that table because I have only managed to insert 1.
The main class is the following
public class main {
public static void main(String[] args) {
ArrayList<Contador> contadores = new ArrayList<Contador>();
contadores.add(new Contador("P44152F", "10:00:00", "21/03/2017", 100.00));
contadores.add(new Contador("R41525R", "11:00:00", "22/03/2017", 99.00));
GroupableHeaderExample frame = new GroupableHeaderExample(contadores);
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
GroupableTableHeaderExample class code
public class GroupableHeaderExample extends JFrame {
public GroupableHeaderExample(ArrayList<Contador> contadores) {
super( "Tabla Ejemplos Header" );
JTable table = null;
//Se crea el vector para almacenar todo el array
Vector <Contador> datos = new Vector();
for(int i=0;i<contadores.size();i++){
datos.addElement(contadores.get(i));
}
//Se crea el modelo de la cabecera
DefaultTableModel dm = new DefaultTableModel();
for(int i=0;i<datos.size();i++){
dm.setDataVector(
new Object[][]{{datos.get(i).getFecha(),datos.get(i).getLectura(),"0"}},
new Object[]{"Fecha","Lectura","Consumo"});
//Creación de la tabla con el modelo de cabecera principal
table = new JTable( dm ) {
protected JTableHeader createDefaultTableHeader() {
return new GroupableTableHeader(columnModel);
}
};
//Se crea un segundo modelo
TableColumnModel cm = table.getColumnModel();//Se coge el modelo del header principal
ColumnGroup contador = new ColumnGroup(datos.get(i).getSerialNumber());//Se crea un grupo
contador.add(cm.getColumn(1));
contador.add(cm.getColumn(2));
GroupableTableHeader header = (GroupableTableHeader)table.getTableHeader();
header.addColumnGroup(contador);
}
JScrollPane scroll = new JScrollPane(table);
getContentPane().add( scroll );
setSize( 400, 120 );
}
}
The result that I have at the moment is the following:
As I mentioned before, it only shows me 1 counter, and I have two in the arrayList, and I can not find a way to put the two together.
Could someone help me organize the code correctly so that all the elements of the arrayList come out?
If anyone can lend me a hand, it would be very helpful. Thanks in advance