Search code examples
javaswingnetbeansnetbeans-7jinternalframe

displaying jInternalFrame from another jinternalFrame over desktop pane in netbeans


I want to display jInternalFrame1 over a desktop pane in a jFrame.jInternalFrame1 contains a Button for displaying jInternalFrame2 over the desktop pane by removing jInternalFrame1.

public class main extends javax.swing.JFrame {

a aa=new a();//jInternalFrame1
public main() {
    initComponents();
    jDesktopPane1.add(aa);
    aa.setVisible(true);
}
public static void main(String args[]) {

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

private javax.swing.JDesktopPane jDesktopPane1; }

code under button in jInternalFrame1;

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
     b bb=new b();//jInternalFrame2
this.dispose();
   bb.setVisible(true);
}

But this code is not displaying the jInternalFrame2.I have seen many solutions for this.But I dont understand how to add these into the desktop pane.sorry for my poor English. Thank you


Solution

  • You forgot to add bb to the jDesktpPane1. To do that, you need to take a JDesktopPane argument as a parameter in your jInternalFrame1. Then use that JDesktopPane to add the second one. I put together a simple test run.

    Code from MainFrame

    private void jMenuItem1ActionPerformed(ActionEvent evt) {                                           
        IFrameOne iFrame1 = new IFrameOne(jDesktopPane1);
        jDesktopPane1.add(iFrame1);
        iFrame1.setVisible(true);
    }  
    

    Code from IFrmaeOne

    JDesktopPane desktop;
    public IFrameOne(JDesktopPane desktop) {
        initComponents();
        this.desktop = desktop;
    }
    ...
    private void jButton1ActionPerformed(ActionEvent evt) {                                         
        IFrameTwo iFrameTwo = new IFrameTwo();
        desktop.add(iFrameTwo);
        this.dispose();
        iFrameTwo.setVisible(true);
    } 
    

    IFrameTwo is just an empty JInternalFrame class. This works for me. Created with GUI Builder also


    UPDATE

    "sir actually I tried cardLayout.But I cant add the cards using navigator (add from palette).For me coding is difficult.So I used drag and drop.Can you tell Why its not possible to add the cards?Yesterday I added.But today cant"

    If you want to use a CardLayout for the JInternalFrame here are the steps

    1. You can create JPanel forms. So create two of them.

    2. In your JInternalFrame drag and drop a JPanel and extend it to the size of the frame.

    3. You need to hand code the CardLayout yourself. But it in the constructor

      public MyInternalFrame() {
          initComponents();
          CardLayout cardlayout = new CardLayout();
          jPanel1.setLayout(cardLayout);
      }
      
    4. Then add the two JPanel forms you created in step one.

      jPanel1.add(new PanelForm1(), "panel1");
      jPanel1.add(new PanelForm2(), "panel2");
      
    5. Then in your actionPerformed you can just switch between panels

      public void jButton1ActionPerformed(java.awt.events.ActionEvent e) {
          cardLayout.show(jPanel1, "panel2");
      }
      
    6. You could switch back and forth if you need to.