JDesktopPane and JInternalFrame in JAVA MVC with Listeners

0

** Hello good day, in classes we had to see the MVC design pattern to implement it in a future project.

Testing how to separate the code from the views and that I made one using JFrame -> 2 botones + JdesktopPane ; and each button opens a JInternalFrame , up there all right, the problem I have is when I want to hear an event within those JInternalFrame , I do not know how to "listen" to them since I put a button inside each JInternalFram e to that at the time of pressing them I return in console a sout to know that it was pressed.

Variable names:

Button "Window 1" = btnVentana1

"Window 2" button = btnVentana2

JDesktopPane = dpDesktop

"Click me!" button = btnVentana1Frame

Button "Click me 2!" = btnVentana2Frame

Try with:

if (e.getSource() == v1f.btnVentana1Frame) {
        System.out.println("Botón click me presionado");
    }

expecting it to work like it did to open the JInternalFrames but it does not dial anything.

PrincipalController.java

package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import view.PrincipalView;
import view.Ventana1Frame;
import view.Ventana2Frame;

public class PrincipalController implements ActionListener{
    private PrincipalView pv;
    private Ventana1Frame v1f;
    private Ventana2Frame v2f;

    public PrincipalController(PrincipalView pv, Ventana1Frame v1f, Ventana2Frame v2f) {
        this.pv = pv;
        this.v1f = v1f;
        this.v2f = v2f;
        this.pv.btnVentana1.addActionListener(this);
        this.pv.btnVentana2.addActionListener(this);
        this.v1f.btnVentana1Frame.addActionListener(this);
        this.v2f.btnVentana2Frame.addActionListener(this);
    }

    public void iniciar(){
        pv.setTitle("Prueba");
        pv.setLocationRelativeTo(null);
        pv.setVisible(true);
        v1f = null;
        v2f = null;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        //Abrir Internal Frame 1
        if (e.getSource() == pv.btnVentana1) {
            if (v1f == null) {
            System.out.println("Ventana 1");
            v1f = new Ventana1Frame();
            pv.dpEscritorio.add(v1f);
            v1f.setVisible(true);
            } else {
            pv.dpEscritorio.getDesktopManager().activateFrame(v1f);
            }
        }
        
        //Abrir Internal Frame 2
        if (e.getSource() == pv.btnVentana2) {
            if (v2f == null) {
            System.out.println("Ventana 2");
            v2f = new Ventana2Frame();
            pv.dpEscritorio.add(v2f);
            v2f.setVisible(true);
            } else {
            pv.dpEscritorio.getDesktopManager().activateFrame(v2f);
            }
        }
    }
}

Main.java

package mvc;

import controller.PrincipalController;
import view.PrincipalView;
import view.Ventana1Frame;
import view.Ventana2Frame;

public class Main {
    public static void main(String[] args) {
    PrincipalView pv = new PrincipalView();
    Ventana1Frame v1f = new Ventana1Frame();
    Ventana2Frame v2f = new Ventana2Frame();
    
    PrincipalController pc = new PrincipalController(pv, v1f, v2f);
    pc.iniciar();
    }
    
}
    
asked by Member 21.11.2018 в 08:49
source

1 answer

0

Here you have a solution, you just have to adapt them to your requirements. A class that is responsible for listening to all the events you need.

public class ControladorPrincipal implements ActionListener, InternalFrameListener{
// puedes usar este controlador con todas las ventanas que quieras. A la final esta clase procesara todas las acciones que 
// esten en el implements que para simplicidad coloque ActionLister.(Botones)
// InternalFrameListener para cuando la ventana este activa, se este mostrando entre otros.


@Override
public void actionPerformed(ActionEvent e) {
    // aqui colocas todo el codigo
}

@Override
public void internalFrameOpened(InternalFrameEvent e) {

}

@Override
public void internalFrameClosing(InternalFrameEvent e) {

}

@Override
public void internalFrameClosed(InternalFrameEvent e) {

}

@Override
public void internalFrameIconified(InternalFrameEvent e) {

}

@Override
public void internalFrameDeiconified(InternalFrameEvent e) {

}

@Override
public void internalFrameActivated(InternalFrameEvent e) {

}

@Override
public void internalFrameDeactivated(InternalFrameEvent e) {

}
}

and here you have a simple implementation. You can place them in a class of your interest

JInternalFrame internal = new JInternalFrame("Un Internal Frame 1");
JInternalFrame internal2 = new JInternalFrame("Un Internal Frame 2");
 // por simplicidad se obvia las partes de agregar elementos a estas ventanas y el proceso de empaquetarlos.

    ControladorPrincipal cp = new ControladorPrincipal();

    // Agregando el mismo controlador para ambas ventanas.
    // Puedes hacer lo mismo con otros elementos agregando mas listeners
    internal.addInternalFrameListener(cp);
    internal2.addInternalFrameListener(cp);
    
answered by 25.11.2018 в 02:03