Open JInternalFrame from another

0

I was learning how to deal with JDesktopPane in order to use in future projects because I find this function very interesting. I tried calling a JInternalFrame of a button from within another JInternalFrame , but I did not succeed. I used the same code I used to call the first, but I did not succeed anyway.

public class FrameDes extends javax.swing.JFrame {

public FrameDes() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    desktop = new javax.swing.JDesktopPane();
    jMenuBar2 = new javax.swing.JMenuBar();
    jMenu3 = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout desktopLayout = new javax.swing.GroupLayout(desktop);
    desktop.setLayout(desktopLayout);
    desktopLayout.setHorizontalGroup(
        desktopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 852, Short.MAX_VALUE)
    );
    desktopLayout.setVerticalGroup(
        desktopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 428, Short.MAX_VALUE)
    );

    jMenu3.setText("File");

    jMenuItem1.setText("ChamarPP");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItem1ActionPerformed(evt);
        }
    });
    jMenu3.add(jMenuItem1);

    jMenuBar2.add(jMenu3);

    setJMenuBar(jMenuBar2);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(desktop)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(desktop, javax.swing.GroupLayout.Alignment.TRAILING)
    );

    setSize(new java.awt.Dimension(868, 488));
    setLocationRelativeTo(null);
}// </editor-fold>                        

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    PrimeiraPagina tela = new PrimeiraPagina();
    tela.setVisible(true);
    desktop.add(tela);        // TODO add your handling code here:
}                                          

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new FrameDes().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
public javax.swing.JDesktopPane desktop;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar2;
private javax.swing.JMenuItem jMenuItem1;
// End of variables declaration                   
}

This first code is from the Home Screen where JDesktopPane is, the next pages should appear inside it.

public class PrimeiraPagina extends javax.swing.JInternalFrame {

public PrimeiraPagina() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jButton1 = new javax.swing.JButton();

    setClosable(true);
    setIconifiable(true);
    setMaximizable(true);

    jButton1.setText("Abri segunda Pagina");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    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(256, 256, 256)
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(275, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(137, 137, 137)
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(163, Short.MAX_VALUE))
    );

    setBounds(0, 0, 697, 406);
}// </editor-fold>                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    SegundaPagina tela = new SegundaPagina();
    FrameDes desc = new FrameDes();
    tela.setVisible(true);
    System.out.println("mudar tela");
    desc.desktop.add(tela);
}                                        


// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
// End of variables declaration                   
}

This section above is from the first JInternalFrame , where there is a button that should call the second screen, but for some reason it does not appear.

public class SegundaPagina extends javax.swing.JInternalFrame {

public SegundaPagina() {
    initComponents();
    System.out.println("pagina mudada");
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();

    setClosable(true);
    setIconifiable(true);
    setMaximizable(true);

    jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
    jLabel1.setText("Essa e a segunda pagina");

    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(172, 172, 172)
            .addComponent(jLabel1)
            .addContainerGap(204, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(144, 144, 144)
            .addComponent(jLabel1)
            .addContainerGap(190, Short.MAX_VALUE))
    );

    setBounds(0, 0, 657, 393);
}// </editor-fold>                        


// Variables declaration - do not modify                     
private javax.swing.JLabel jLabel1;
// End of variables declaration                   
}

Finally the last screen, which should be displayed in JDesktopPane , but does not appear, note that I put println in it to check if it was at least called and yes it was, but the screen does not appear. What is the problem with the code?

    
asked by anonymous 22.06.2017 / 01:03

1 answer

0

The problem is that you are unnecessarily creating another instance of your JFrame , and adding the inner frame to it. In this method in its class PrimeiraPagina :

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    SegundaPagina tela = new SegundaPagina();
    FrameDes desc = new FrameDes();
    tela.setVisible(true);
    System.out.println("mudar tela");
    desc.desktop.add(tela);
}

Replace with:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    SegundaPagina tela = new SegundaPagina();
    System.out.println("mudar tela");
    getParent().add(tela);
    tela.setVisible(true);
}

If the internal frames will be in the same JDesktopPane , a simple getParent() solves the problem, because it returns the container of the current component, in this case, an internalframe.

Another detail is that you are setting the visibility before adding the frame in JDesktopPane , this is a bad practice. Just set the visibility of the inner frame after adding it to a container component.

    
22.06.2017 / 15:25