I have the code bellow to read data in database and then show this in the jtable with multiple color lines. This works fine but only in the last iteration. In this example I have put a for command with 5 iterations and the frame is visible from begining of execution but the rows in jtable only appears at the end of 5 iterations. I want to show the data obtained in each iteration with the info gather from each select to database actualized (bbdd_recorrer_rs)
bbdd_conectar() - Procedure that only open the database connection.
bbdd_recorrer_rs() - Procedure to select data from database and put in a resultset that is added to modelo (modelo.addRow(fila);) that is show in jtable.
how can I see the updated content of the query in the jtable in each of the iterations of the for and not only in the last?
This is the code.
// imports ...
public class MonitorSesionesNuevo {
// Constantes
public static final Color CLASE_APPLICATION = Color.BLUE.darker();
....
public ResultSet rs;
// Modelo de datos de jtable
public DefaultTableModel modelo;
public JTable t_eventos;
public Object[] fila;
public static void main(String[] args) {
new MonitorSesionesNuevo();
}
public MonitorSesionesNuevo() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
// Realiza la conexion a la base de datos.
bbdd_conectar();
modelo = new DefaultTableModel();
modelo.addColumn("Momento");
modelo.addColumn("CLASE");
modelo.addColumn("EVENTO");
t_eventos = new JTable(modelo);
t_eventos.setDefaultRenderer(Object.class, new MyCellRenderer());
JFrame frame = new JFrame("ControlSesionesNuevo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(t_eventos));
frame.pack();
frame.setLocationRelativeTo(null);
// Recorremos el resulset con los datos sacados de la bd
for (int z=1; z<=5; z++)
{
bbdd_recorrer_rs();
frame.setVisible(true);
try {
java.util.concurrent.TimeUnit.SECONDS.sleep(5);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
//frame.setVisible(false);
}
frame.setVisible(true);
}
});
}
public class MyCellRenderer extends javax.swing.table.DefaultTableCellRenderer {
public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column) {
final java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Object vClase = table.getValueAt(row, 0); // Columna 0, ClASE
String SClase = vClase.toString();
switch(SClase) {
case "CPU":
// code block
cellComponent.setBackground(Color.GREEN);
cellComponent.setForeground(Color.BLACK);
break;
case "Other":
cellComponent.setBackground(Color.YELLOW);
cellComponent.setForeground(Color.BLACK);
break;
default:
// code block
}
return cellComponent;
}
}
public void bbdd_recorrer_rs()
{
// Consultamos la bd
try {
modelo.setRowCount(0);
rs = stmt.executeQuery();
int i=1;
while (rs.next())
{
Object[] fila = new Object[9]; // 9 columnas
// Metemos los 9 campos que leemos en la bd en una fila del jtable.
fila = new Object[9]; // 9 columnas
for (int k = 0; k<9; k++) {
fila[k] = rs.getObject(k+1);
}
// Añade al jtable en una sola fila todos los campos que nos traemos en el resultset
modelo.addRow(fila);
i++;
}
rs.close();
}
catch (Exception e) {
// catching the exception
System.out.println(e);
}
// Fin Consulta a la bd
}
}
I would appreciate any suggestions.
Regards