Link to home
Start Free TrialLog in
Avatar of orhanbaba
orhanbaba

asked on

how to show JInternalFrame

how can i show my internal frame when i click the menu item.
can you fix the code


public class test extends javax.swing.JFrame {
   
    /** Creates new form test */
    public test() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        intframeTest = new javax.swing.JInternalFrame();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        intframeTest.setClosable(true);
        intframeTest.setIconifiable(true);
        intframeTest.setMaximizable(true);
        intframeTest.setResizable(true);
        getContentPane().add(intframeTest, java.awt.BorderLayout.CENTER);

        jMenu2.setText("Menu");
        jMenuItem2.setText("test");
        jMenu2.add(jMenuItem2);

        jMenuBar2.add(jMenu2);

        setJMenuBar(jMenuBar2);

        pack();
    }

    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    public static void main(String args[]) {
        new test().show();
    }
   
    // Variables declaration - do not modify
    private javax.swing.JInternalFrame intframeTest;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem2;
    // End of variables declaration
   
}

ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What are you trying to do?  I think you need to have a rethink about how your UI is going to look as you seem to be adding an Internal Frame to a Frame, with the menus on the Internal Frame, and on the Frame as well...

Also, Internal Frames can't be Modal (unless you do some proper hacking -- none of which works 100% believe me I've tried)...

Tim
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;

public class Test extends javax.swing.JFrame {

    public Test() {
        initComponents();
    }

    private void initComponents() {
        intframeTest = new javax.swing.JInternalFrame( "Testing" );
        intframeTest.getContentPane().add( new JButton( "Blah" ) ) ;
        intframeTest.pack();

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        intframeTest.setClosable(true);
        intframeTest.setIconifiable(true);
        intframeTest.setMaximizable(true);
        intframeTest.setResizable(true);
        jMenu1.setText("Menu");
        jMenuItem1.setText("test");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });

        desktop = new JDesktopPane() ;
        this.setContentPane( desktop ) ;
        this.setJMenuBar( jMenuBar1 );
        jMenu1.add(jMenuItem1);
        jMenuBar1.add(jMenu1);

//        intframeTest.setJMenuBar(jMenuBar1);
        setSize( 300, 300 ) ;
        setLocationRelativeTo( null ) ;
    }

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
        // what will i write here to show my internal frame
        System.out.println( "Woo" ) ;
        intframeTest.setVisible( true );
        intframeTest.setLocation( 10, 10 );
        desktop.add( intframeTest ) ;
    }

    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    public static void main(String args[]) {
        new Test().show();
    }

    // Variables declaration - do not modify
    private javax.swing.JInternalFrame intframeTest;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JDesktopPane desktop ;
    // End of variables declaration

}
Avatar of orhanbaba
orhanbaba

ASKER

i solved it manually but thank you Tim