viernes, 21 de octubre de 2011

Abrir JInternalFrame desde otro JInternalFrame

Hola como respuesta a un comentario en esta entrada (por fin alguien comenta en el blog xD) . veremos (justo lo que solicitan e indica el titulo de la entrada)
Crearemos 3 Clases.(LafMain.java, JIF.java, JIF2.java)
el codigo de JIF2.java no lo escribo porque es un JinternalFrame vacio :D

Si necesitan o quieren ver como crear un menú pueden entrar acá o acá :D
la clase principal:



import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.UIManager;

/**
 *
 * @author McCubo
 */
public class LaFMain extends javax.swing.JFrame {

    private JMenuBar menu = new JMenuBar();
    private JMenu action = new JMenu("Action");
    private JMenuItem actionItem = new JMenuItem("perform Action");
    private static JDesktopPane jDesktopPane = new JDesktopPane();
    private static McLaFNimbus mcLaFNimbus;

    public LaFMain() {
        initComponents();
        setVisible(true);
    }

    private void initComponents() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600, 400);
        setLayout(new BorderLayout());
        actionItem.addActionListener(getListener());
        action.add(actionItem);
        menu.add(action);
        getContentPane().add(menu, BorderLayout.NORTH);
        getContentPane().add(jDesktopPane, BorderLayout.CENTER);
    }

    private ActionListener getListener() {
        return new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JIF jIF = new JIF();
                jIF.setVisible(true);
                jDesktopPane.add(jIF);
            }
        };
    }

    public static JDesktopPane getjDesktopPane() {
        return jDesktopPane;
    }

    public static void main(String[] args) {
        new LaFMain();
    }
}

class validador {

    public static HashMap<String, JInternalFrame> jIframes = new HashMap<String, JInternalFrame>();

    public static void addJIframe(String key, JInternalFrame jiframe) {
        jIframes.put(key, jiframe);
    }

    public static JInternalFrame getJInternalFrame(String key) {
        return jIframes.get(key);
    }
}

Listo, un JFrame bastante sencillo, con una barra de menú en la parte superior y en el centro un JDesktopPane (tipo statico debido que el método que lo retorna es estatico).
ahora crearemos JInternalFrame que llamara a otro JInternalFrame (el diseño con la ayuda del IDE NetBeans) y el listener del boton.
la clase en cuestion:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JInternalFrame;

/**
 *
 * @author McCubo
 */
public class JIF extends javax.swing.JInternalFrame {

    /** Creates new form JIF */
    public JIF() {
        setTitle("TITULO");
        initComponents();
        jButton1.addActionListener(getButtonActionListener());
    }

    private ActionListener getButtonActionListener(){
        return new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                JInternalFrame ji = validador.getJInternalFrame(JIF2.class.getName());
                if(ji == null || ji.isClosed()){
                    ji = new JIF2();
                    LaFMain.getjDesktopPane().add(ji);
                    validador.addJIframe(JIF2.class.getName(), ji);
                }
                ji.setVisible(true);
            }

        };
    }
    /*Codigo del diseño Generado por Netbeans*/
y esto seria el resultado:

1 comentario:

  1. excelente es algo que me ayudara mucho..!!! pero me pregunto si me podrías dejar la carpeta del proyecto

    ResponderEliminar