Me encuentro desarrollando una GUI en Java y he encontrado un problema con ella y es que al momento de ingresar datos nuevos no se actualiza la información.
Este es el código:
Parte 1:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class DBConnect {
String PBM;
String Year;
DBConnect(String PBM, String Year){
this.PBM = PBM;
this.Year = Year;
}
public ArrayList <Object> ConnectDB(){
try{
Connection c = DriverManager.getConnection("jdbc:sqlite://C:/Users/Trocx/OneDrive/Documents/Proyecto/PBM_DB.db");
System.out.println("Connection to database has been performed!");
String sql_ft = "SELECT FACTOR FROM PBM_FACTOR WHERE PBM =" + this.PBM;
Statement stmt_ft = c.createStatement();
ResultSet rs_ft = stmt_ft.executeQuery(sql_ft);
String Factor = rs_ft.getString("FACTOR");
stmt_ft.close();
String sql_sal = "SELECT SALARY FROM AN_SAL WHERE YEAR =" + this.Year;
Statement stmt_sal = c.createStatement();
ResultSet rs_sal = stmt_sal.executeQuery(sql_sal);
Double Salary = rs_sal.getDouble("SALARY");
stmt_sal.close();
c.close();
ArrayList <Object> dat = new ArrayList<>(3);
dat.add(true);
dat.add(Factor);
dat.add(Salary);
return dat;
}catch (Exception e) {
System.out.println(e.getClass().getName() + ": " + e.getMessage());
ArrayList <Object> dat = new ArrayList<>(1);
dat.add(false);
return dat;
}
}
}
Parte 2:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import javax.swing.*;
public class GUI extends JFrame{
String pbmGot;
String yearGot;
public void init(){
//Paneles
JPanel Frame1 = new JPanel();
JPanel Buttons = new JPanel();
JPanel Principal = new JPanel();
JLabel jlName = new JLabel("Nombre");
JLabel jlPbm = new JLabel("PBM");
JLabel jlYears = new JLabel("Año");
JTextField jtName = new JTextField();
JTextField jtPbm = new JTextField();
TabCalc TableR = new TabCalc();
JComboBox<String> Years = new JComboBox<>();
Years.addItem("");
Years.addItem("2019");
Years.addItem("2020");
Years.addItem("2021");
Years.addItem("2022");
Years.addItem("2023");
Years.addItem("2024");
jtPbm.addFocusListener(new FocusListener(){
@Override
public void focusGained(FocusEvent e){
}
@Override
public void focusLost(FocusEvent e){
try {
Integer PBMd = Integer.parseInt(jtPbm.getText());
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(Frame1, "¡Ingresa un PBM válido!", "Error!", JOptionPane.ERROR_MESSAGE);
jtPbm.setText("");
}
}
});
JButton buttOK = new JButton("Enviar");
buttOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
pbmGot = jtPbm.getText();
yearGot = Years.getSelectedItem().toString();
if(pbmGot.equals("") || yearGot.equals("")){
JOptionPane.showMessageDialog(Principal, "¡Información incompleta!", "Error", JOptionPane.ERROR_MESSAGE);
}else{
if(Integer.parseInt(pbmGot) > 100 || Integer.parseInt(pbmGot) <= 0){
JOptionPane.showMessageDialog(Principal, "¡Información errónea!", "Error", JOptionPane.ERROR_MESSAGE);
}else{
DBConnect sql = new DBConnect(pbmGot,yearGot);
ArrayList<Object> s = sql.ConnectDB();
boolean x = (boolean) s.get(0);
if(x == true){
JOptionPane.showMessageDialog(Principal, "¡La consulta fue exitosa!", "Aviso", JOptionPane.INFORMATION_MESSAGE);
TableR.tabFill(pbmGot, yearGot);
Principal.add(TableR,BorderLayout.CENTER);
Principal.updateUI();
}
}
}}
});
JButton buttDel = new JButton("Borrar");
buttDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
jtName.setText("");
jtPbm.setText("");
Years.setSelectedItem("");
Principal.remove(TableR);
Principal.updateUI();
JOptionPane.showMessageDialog(Principal, "¡Consulta eliminada!", "Aviso", JOptionPane.INFORMATION_MESSAGE);
}
});
Buttons.setLayout(new GridLayout(1,2,2,5));
Buttons.add(buttOK);
Buttons.add(buttDel);
Frame1.setLayout(new GridLayout(4,2,5,2));
Frame1.add(jlName);
Frame1.add(jtName);
Frame1.add(jlPbm);
Frame1.add(jtPbm);
Frame1.add(jlYears);
Frame1.add(Years);
Principal.setLayout(new BorderLayout());
Principal.add(Frame1,BorderLayout.NORTH);
Principal.add(Buttons,BorderLayout.SOUTH);
add(Principal);
setTitle("Calculadora de matrícula");
setSize(400, 300);
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
GUI gui1 = new GUI();
gui1.init();
}
}
Parte 3:
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
public class MatCalc extends DefaultTableModel{
String PBM;
String Year;
public void creaTable(String PBM, String Year){
ArrayList <String> Labels = new ArrayList<String>();
Labels.add("Bienestar");
Labels.add("Sistematización");
Labels.add("Póliza o Seguro Estudiantíl");
Labels.add("Matrícula");
Labels.add("Total");
this.addColumn("Concepto");
this.addColumn("Valor Unitario");
DBConnect sql = new DBConnect(PBM,Year);
ArrayList<Object> s = sql.ConnectDB();
s.remove(0);
Double factor = Double.parseDouble(s.get(0).toString());
Double salary = Double.parseDouble(s.get(1).toString());
long mat = Math.round(factor*0.93*salary);
long bt = Math.round(0.1*mat);
long st = Math.round(0.076*salary);
long pz;
if(Integer.parseInt(Year) >= 2023){
pz = Math.round(25000.0);
}else{
pz = Math.round(24890.0);
}
long total = mat + bt + st + pz;
s.set(0, bt);
s.set(1, st);
s.add(2, pz);
s.add(3, mat);
s.add(4,total);
var k = Labels.toArray();
String[] data = new String[2];
int i = 0;
int n = 0;
for(int j=0;j<10;j++){
if(i%2 == 0){
data[i] = k[n].toString();
}else{
data[i] = String.format("%1$,.1f", Double.parseDouble(s.get(n).toString()));
}
i += 1;
if((i%2 == 0) && i!=0){
this.addRow(data);
i = 0;
n += 1;
data = new String[2];
}
}
System.out.println("Prueba");
}
}
Parte 4:
import javax.swing.*;
import java.awt.*;
public class TabCalc extends JPanel {
public void tabFill(String PBM, String Year){
setLayout(new BorderLayout());
MatCalc matc = new MatCalc();
matc.creaTable(PBM, Year);
JTable table = new JTable(matc);
JScrollPane scroll = new JScrollPane(table);
add(scroll,BorderLayout.CENTER);
}
}
La interfaz recibe la primera vez los datos correctamente y ejecuta el código y muestra todo perfecto, pero al momento de borrar la información "en teoría" e ingresar nuevos datos se sigue mostrando el resultado de la consulta anterior.
¿Cómo puedo solucionar esto? ¡Gracias!
En teoría dentro del método del actionListener se deberían tomar los nuevos datos (que creo que lo hace) pero al parecer entonces no llegan a la consulta o algo parecido.