I have a class designed to handle the events of the frames that I have created, that allows me to play with them and not instantiate code in their structure, to maintain a relationship Vista, Model, Controller . Within the mainframe I have a JTable and I would like each time the frame is started that JTable will take the structure of a DefaultTableModel created within the class of the events, by the way, I do not know if to create a single class of events and another of functions, the code of the class manager of events is the following:
public class ControladorEstudiante{
EstudianteAdmin estAdmin= new EstudianteAdmin();
EstudianteDAOImpl estDAO= new EstudianteDAOImpl();
public ControladorEstudiante(EstudianteAdmin estAdmin, MostrarEstudiante estMostrar){
this.estDAO= estDAO;
this.estAdmin= estAdmin;
}
public void initEstudiante(){
}
public void ListarTabla(JTable tabla){
DefaultTableModel modeloTabla= new DefaultTableModel();
tabla.setModel(modeloTabla);
modeloTabla.addColumn("NOMBRE");
modeloTabla.addColumn("MATRICULA");
modeloTabla.addColumn("NOTA");
Object[] columna= new Object[3];
int objGuardados= estDAO.extraerTodos().size();
for (int i = 0; i < objGuardados; i++) {
columna[0]= estDAO.extraerTodos().get(i).getNombre();
columna[1]= estDAO.extraerTodos().get(i).getMatricula();
columna[2]= estDAO.extraerTodos().get(i).getNota();
modeloTabla.addRow(columna);
}
}
}
The following is the frame code:
package comm.estudiante.interfaz;
public class EstudianteAdmin extends javax.swing.JFrame {
public EstudianteAdmin() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
menuFunciones = new javax.swing.JPopupMenu();
submenu_Modificar = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JPopupMenu.Separator();
submenu_Eliminar = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JPopupMenu.Separator();
submenu_EliminarTodos = new javax.swing.JMenuItem();
txtBuscar_estudiante = new javax.swing.JTextField();
btnAgr_estudiante = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tablaEstudiante = new javax.swing.JTable();
submenu_Modificar.setText("Modificar");
menuFunciones.add(submenu_Modificar);
menuFunciones.add(jSeparator1);
submenu_Eliminar.setText("Eliminar");
menuFunciones.add(submenu_Eliminar);
menuFunciones.add(jSeparator2);
submenu_EliminarTodos.setText("Eliminar todos");
menuFunciones.add(submenu_EliminarTodos);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnAgr_estudiante.setIcon(new javax.swing.ImageIcon(getClass().getResource("/comm/estudiante/imagenes/Nuevo Contacto.jpg"))); // NOI18N
btnAgr_estudiante.setText("Crear Estudiante");
tablaEstudiante = new javax.swing.JTable(){
public boolean isCellEditable(int rowIndex,int columnIndex){
return false;
}
};
tablaEstudiante.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
tablaEstudiante.setComponentPopupMenu(menuFunciones);
tablaEstudiante.setFocusable(false);
jScrollPane1.setViewportView(tablaEstudiante);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(txtBuscar_estudiante, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAgr_estudiante, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtBuscar_estudiante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnAgr_estudiante))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(EstudianteAdmin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(EstudianteAdmin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EstudianteAdmin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EstudianteAdmin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EstudianteAdmin().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAgr_estudiante;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPopupMenu.Separator jSeparator1;
private javax.swing.JPopupMenu.Separator jSeparator2;
private javax.swing.JPopupMenu menuFunciones;
private javax.swing.JMenuItem submenu_Eliminar;
private javax.swing.JMenuItem submenu_EliminarTodos;
private javax.swing.JMenuItem submenu_Modificar;
private javax.swing.JTable tablaEstudiante;
private javax.swing.JTextField txtBuscar_estudiante;
// End of variables declaration
public javax.swing.JTextField getTxtBuscar_estudiante(){
return txtBuscar_estudiante;
}
public javax.swing.JButton getBtnAgr_estudiante(){
return btnAgr_estudiante;
}
public javax.swing.JTable getTablaEstudiante(){
return tablaEstudiante;
}
public javax.swing.JMenuItem getSubmenu_Modificar(){
return submenu_Modificar;
}
public javax.swing.JMenuItem getSubmenu_Eliminar(){
return submenu_Eliminar;
}
public javax.swing.JMenuItem getSubmenu_EliminarTodos(){
return submenu_EliminarTodos;
}
}
This is the code of the extractAll () method where DefaultTableModel fills the table:
public List<Estudiante> extraerTodos() {
Connection con = null;
Statement stmnt = null;
ResultSet rs = null;
String sql = "SELECT * FROM notas";
List<Estudiante> lista = new ArrayList<>();
try {
con = dbcon.conectar();
stmnt = con.createStatement();
rs = stmnt.executeQuery(sql);
while (rs.next()) {
lista.add(new Estudiante(
rs.getInt("id"),
rs.getString("nombre"),
rs.getString("matricula"),
rs.getInt("nota")
));
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return lista;
}
What I would like to know is how I can pass the ListTable method as constructor method parameter of the main frame following the MVC pattern and avoiding to instantiate code in a Vista or how can I simply do to start the frame and have the table take that DefaultTableModel .