What I want to do is create a Controller for each Vista , but when creating a second controller it does not work, does not open the window or JIntenarFrame that corresponds.
I have this code in the controller of the main window, it works fine, because it triggers the orders of the Vista :
package ControladorProyecto;
import InterfazProyecto.Proyecto_Admin;
import InterfazProyecto.Proyecto_Clientes;
import static java.awt.Frame.MAXIMIZED_BOTH;
import java.awt.event.*;
import javax.swing.*;
public class Proyecto_ControladorAdmin implements ActionListener{
private Proyecto_Clientes cliente= null;
private Proyecto_Admin factureAdmin= null;
private enum Ventana{
getJMenuClientes,
}
public Proyecto_ControladorAdmin(Proyecto_Admin factureAdmin){
this.factureAdmin= factureAdmin;
}
public void abrirFacture(){
//Inicialización del Frame factureAdmin
this.factureAdmin.setTitle("FACTURE");
this.factureAdmin.setExtendedState(MAXIMIZED_BOTH);
this.factureAdmin.setVisible(true);
//Definición de los eventos de los Componentes
this.factureAdmin.getJMenuClientes().addActionListener(this);
this.factureAdmin.getJMenuClientes().setActionCommand("getJMenuClientes");
//this.cc= new ControladorCliente();
}
//Centrar los JInternalFrame en el Desktop
public JInternalFrame centralizarInternalFrame(JInternalFrame InternalFrame) {
int x = (this.factureAdmin.getDesktop().getWidth() / 2) - InternalFrame.getWidth() / 2;
int y = (this.factureAdmin.getDesktop().getHeight() / 2) - InternalFrame.getHeight() / 2;
if (InternalFrame.isShowing()) {
InternalFrame.setLocation(x, y);
} else {
this.factureAdmin.getDesktop().add(InternalFrame);
InternalFrame.setLocation(x, y);
InternalFrame.setVisible(true);
InternalFrame.moveToFront();
}
return InternalFrame;
}
@Override
public void actionPerformed(ActionEvent ae) {
switch (Ventana.valueOf(ae.getActionCommand())) {
case getJMenuClientes:
if (!(this.cliente instanceof Proyecto_Clientes)) {
this.cliente = new Proyecto_Clientes();
this.cliente.setTitle("CLIENTES");
this.centralizarInternalFrame(this.cliente);
} else if (this.cliente instanceof Proyecto_Clientes) {
JOptionPane.showMessageDialog(null, "La ventana ya está abierta");
}
break;
}
}
}
The true problem is in this code that corresponds to the second Controller , does not trigger the Vista orders, opening the JInternalFrame which should:
package ControladorProyecto;
import InterfazProyecto.Proyecto_Admin;
import InterfazProyecto.Proyecto_Clientes;
import InterfazProyecto.Proyecto_NuevoCliente;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
public class Proyecto_ControladorCliente implements ActionListener{
private Proyecto_Admin factureAdmin= null;
private Proyecto_Clientes clientes= null;
private Proyecto_NuevoCliente nuevoCliente= null;
private enum Componentes{
txtBuscarCliente,
tablaClientes,
getBtnRegistrarCliente
}
public Proyecto_ControladorCliente(Proyecto_Clientes clientes){
this.clientes= clientes;
}
public void abrirCliente(){
this.clientes.getBtnRegistrarCliente().addActionListener(this);
this.clientes.getBtnRegistrarCliente().setActionCommand("getBtnRegistrarCliente");
}
public JInternalFrame centralizarInternalFrame(JInternalFrame InternalFrame) {
int x= (this.factureAdmin.getDesktop().getWidth()/2)- InternalFrame.getWidth()/2;
int y= (this.factureAdmin.getDesktop().getHeight()/2)- InternalFrame.getHeight()/2;
if(InternalFrame.isShowing()){
InternalFrame.setLocation(x, y);
}else{
this.factureAdmin.getDesktop().add(InternalFrame);
InternalFrame.setLocation(x, y);
InternalFrame.setVisible(true);
InternalFrame.toFront();
}
return InternalFrame;
}
@Override
public void actionPerformed(ActionEvent ae) {
switch (Componentes.valueOf(ae.getActionCommand())) {
case getBtnRegistrarCliente:
if (!(this.nuevoCliente instanceof Proyecto_NuevoCliente)) {
this.nuevoCliente = new Proyecto_NuevoCliente();
this.nuevoCliente.setTitle("NUEVO CLIENTE");
this.centralizarInternalFrame(this.nuevoCliente);
} else if (this.nuevoCliente instanceof Proyecto_NuevoCliente) {
JOptionPane.showMessageDialog(null, "La ventana ya está abierta");
}
break;
}
}
}
I do not know why the second Controller does not work for me.
The other codes correspond to the other JInternalFrame so they can reproduce the problem.
The following code corresponds to the class that initializes the project.
package Proyecto;
import ControladorProyecto.Proyecto_ControladorAdmin;
import ControladorProyecto.Proyecto_ControladorCliente;
import InterfazProyecto.Proyecto_Clientes;
import InterfazProyecto.Proyecto_Admin;
public class IncializarProyecto {
public static void main(String[] args) {
Proyecto_Admin factAdmin = new Proyecto_Admin();
Proyecto_Clientes cl = new Proyecto_Clientes();
new Proyecto_ControladorAdmin(factAdmin).abrirFacture();
new Proyecto_ControladorCliente(cl).abrirCliente();
}
}
This is from the main Project Window or ProjectAdmin :
package InterfazProyecto;
public class Proyecto_Admin extends javax.swing.JFrame {
public Proyecto_Admin() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
desktop = new javax.swing.JDesktopPane();
jPanel1 = new javax.swing.JPanel();
menus = new javax.swing.JMenuBar();
jMenuProduccion = new javax.swing.JMenu();
jSubMenuClientes = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
desktop.setBackground(new java.awt.Color(0, 102, 102));
javax.swing.GroupLayout desktopLayout = new javax.swing.GroupLayout(desktop);
desktop.setLayout(desktopLayout);
desktopLayout.setHorizontalGroup(
desktopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
desktopLayout.setVerticalGroup(
desktopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 250, Short.MAX_VALUE)
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 23, Short.MAX_VALUE)
);
jMenuProduccion.setText("Producción");
jSubMenuClientes.setText("Clientes");
jMenuProduccion.add(jSubMenuClientes);
menus.add(jMenuProduccion);
setJMenuBar(menus);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(desktop)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(desktop)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
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(Proyecto_Admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Proyecto_Admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Proyecto_Admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Proyecto_Admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Proyecto_Admin().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JDesktopPane desktop;
private javax.swing.JMenu jMenuProduccion;
private javax.swing.JPanel jPanel1;
private javax.swing.JMenuItem jSubMenuClientes;
private javax.swing.JMenuBar menus;
// End of variables declaration
public javax.swing.JDesktopPane getDesktop(){
return desktop;
}
public javax.swing.JMenuItem getJMenuClientes(){
return jSubMenuClientes;
}
}
Next, this code corresponds to the JInternalFrame ProjectClients:
package InterfazProyecto;
public class Proyecto_Clientes extends javax.swing.JInternalFrame {
public Proyecto_Clientes() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
tablaClientes = new javax.swing.JTable();
txtBuscarCliente = new javax.swing.JTextField();
btnRegistrarCliente = new javax.swing.JButton();
setClosable(true);
setIconifiable(true);
setMaximizable(true);
setResizable(true);
setToolTipText("");
tablaClientes.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{},
{},
{},
{}
},
new String [] {
}
));
jScrollPane1.setViewportView(tablaClientes);
btnRegistrarCliente.setText("Registrar Cliente");
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)
.addGroup(layout.createSequentialGroup()
.addComponent(txtBuscarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
.addComponent(btnRegistrarCliente)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtBuscarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnRegistrarCliente))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton btnRegistrarCliente;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tablaClientes;
private javax.swing.JTextField txtBuscarCliente;
// End of variables declaration
public javax.swing.JTable getTablaClientes(){
return tablaClientes;
}
public javax.swing.JTextField getTxtBuscarClientes(){
return txtBuscarCliente;
}
public javax.swing.JButton getBtnRegistrarCliente(){
return btnRegistrarCliente;
}
}
And finally the JInternalFrame code that does not work when the JInternalFrame button described above is (ClientProject) and corresponds to the second controller, _is this JInternalFrame which is not displayed in the JDesktopPane of the Project_Admin _ .
package InterfazProyecto;
public class Proyecto_NuevoCliente extends javax.swing.JInternalFrame {
public Proyecto_NuevoCliente() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
lblFotoCliente = new javax.swing.JLabel();
lblFotoCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/InterfazProyecto/imagenes/1475738020_kuser.png"))); // NOI18N
lblFotoCliente.setBorder(javax.swing.BorderFactory.createEtchedBorder());
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(lblFotoCliente)
.addContainerGap(572, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(lblFotoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(217, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel lblFotoCliente;
// End of variables declaration
}