0

I am using eclipse 4.2 with Java.

I have 2 java program : AppWin.java Form1.java

AppWin.java is gui windows application with menu/menu item1.

Form1.java is a Gui Jframe

I like to call Form1.java from AppWin.java by click the menu/menu item1. When close Form1.java, it is back to AppWin.java.

This is something like MDIFORM. I really cannot find answer. Please help , if you know eclipse menu.

Thanks

package top;

import java.awt.EventQueue;

public class AppWin {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AppWin window = new AppWin();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

...

With your help, I made a big step. Thanks to all of you!

Next is my final demo, in windows 7, eclipse 4.2, java Gui Hope it is helpful to others.

There are 3 parts : AppWin, Form1, Form2. AppWin is top main which call Form1 and Form2 with menu/item.

//1
package top;

import java.awt.EventQueue;

public class AppWin {

    private JFrame frame;

    private Form1 form1;
    private Form2 form2;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AppWin window = new AppWin();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public AppWin() {
        initialize();
        form1 = new Form1();
        form2 = new Form2();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu mnNewMenu = new JMenu("Menu1");
        menuBar.add(mnNewMenu);

        JMenuItem mntmNewMenuItem = new JMenuItem("menu item1");
        mntmNewMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                form1.setVisible(true);
            }
        });
        mnNewMenu.add(mntmNewMenuItem);

        JMenuItem mntmNewMenuItem_1 = new JMenuItem("menu item2");
        mntmNewMenuItem_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                form2.setVisible(true);
            }
        });
        mnNewMenu.add(mntmNewMenuItem_1);

        JMenu mnNewMenu_1 = new JMenu("Menu2");
        menuBar.add(mnNewMenu_1);

        JMenuItem mntmMenuItem = new JMenuItem("Menu item3");
        mnNewMenu_1.add(mntmMenuItem);
    }

}

//2
package top;

import java.awt.BorderLayout;

public class Form1  extends JFrame {

    private JPanel contentPane;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Form1 frame = new Form1();
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Form1() {
//      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JLabel lblNewLabel = new JLabel("this Form1");
        contentPane.add(lblNewLabel, BorderLayout.WEST);

        textField = new JTextField();
        contentPane.add(textField, BorderLayout.CENTER);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("New button");
        contentPane.add(btnNewButton, BorderLayout.EAST);
    }

}

//3
package top;

import java.awt.BorderLayout;

public class Form2 extends JDialog {

    private final JPanel contentPanel = new JPanel();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            Form2 dialog = new Form2();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public Form2() {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        {
            JLabel lblThisForm = new JLabel("This Form2");
            contentPanel.add(lblThisForm);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }

}

Thanks again

tony5
  • 5
  • 1
  • 1
  • 4
  • 1
    Instead of multiple frames use `CardLayout` – Extreme Coders Jun 18 '13 at 06:26
  • 3
    Obligatory advice: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) – Howard Jun 18 '13 at 06:38
  • 1
    Use `JInternalFrame` instead. See [How to Use Internal Frames](http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html) – Bnrdo Jun 18 '13 at 06:46
  • Thanks to all your reply. CardLayout? can you post a simple sample. For JInternalFrame, I know it is for Netbean. Eclipse supported? I am new to java GUI, so need a simple way to do it. Why I need Menu application? I have many java programs. But I never know how to group them together in Java. So please tell me a simple way to call 2nd Form from menu/item. Thanks – tony5 Jun 18 '13 at 07:00

4 Answers4

2

You better use JDesktopPane + JInternalFrame for that purpose instead. Here's a quick sample.

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;

    import javax.swing.AbstractAction;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;

    public class JInternalFrameSample {

        private JPanel pnlMain;
        private JDesktopPane desk;

        public JInternalFrameSample(){
            pnlMain = new JPanel(new BorderLayout()){
                @Override public Dimension getPreferredSize(){
                    return new Dimension(600,600);
                }
            };
            desk = new JDesktopPane();

            JMenuBar bar = new JMenuBar();
            JMenu menu = new JMenu("Internal Frame");
            JMenuItem item = new JMenuItem();

            item.setAction(new AbstractAction("Create New") {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    JInternalFrame iFrame = new JInternalFrame("Created from Menu");
                    iFrame.setResizable(true);
                    iFrame.setClosable(true);
                    iFrame.setIconifiable(true);
                    iFrame.setSize(new Dimension(300, 300));
                    iFrame.setLocation(0, 0);

                    //iFrame.getContentPane().setLayout(new BorderLayout());
                    //iFrame.getContentPane().add( new YourCustomUI().getUI() );

                    iFrame.setVisible(true);
                    desk.add(iFrame);
                }
            });


            menu.add(item);
            bar.add(menu);

            pnlMain.add(bar, BorderLayout.PAGE_START);
            pnlMain.add(desk, BorderLayout.CENTER);
        }

        private JPanel getUI(){
            return pnlMain;
        }

        public static void main(String[] args){
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame("Demo");
                    frame.getContentPane().setLayout(new BorderLayout());
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.getContentPane().add(new JInternalFrameSample().getUI());
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }
    }

See also : How to Use Internal Frames

Bnrdo
  • 5,325
  • 3
  • 35
  • 63
  • thanks to onepotato. JDesktopPane + JInternalFrame is a new challange to me. before I go into, check again. JDesktopPane + JInternalFrame can be used in Eclipse 4.2? If I have many java GUI program, JDesktopPane + JInternalFrame can call each of them? Thanks again. – tony5 Jun 18 '13 at 08:02
  • Of course you can. Eclipse is just an IDE, a tool that helps programmer to develop in Java and other languages. Yes, you just have to add those GUI program to each internal frames. 1 internal frame = 1 of your GUI program. Internal frame behaves pretty much like a usual JFrame except for some [rules](http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html#rules) – Bnrdo Jun 18 '13 at 08:13
  • why i can only set only one check mark? but 3 of you all helpful ! – tony5 Jun 24 '13 at 01:17
  • If many answers help you, you upvote them by clicking the upward arrow in the top left side of that answer. Then choose 1 answer that helped you the most, then give it your check mark. – Bnrdo Jun 24 '13 at 01:24
1

If you do not like the JDesktopPane and JInternalFrame solution, just use your AppWin JFrame as is, and open modal JDialogs for the rest of the forms, instead of JFrames. Modal dialogs can float around the desktop and do not allow you to click your AppWin, until they are closed.

It is usually better to use just one main JFrame for an application, unless you have some wizard application that moves progressively from one JFrame to the other and back. Even with a wizard app, you can stick with one JFrame and update progressively just the ContentPane with JPanels.

Here is the AppWin JFrame:

public class AppWin extends javax.swing.JFrame {
    private Form1 form1;
    private Form1 form2;
    ...
    private FormN formN;

    public AppWin() {
        initComponents();
        form1 = new Form1(this, true);
        form2 = new Form2(this, true);
        ...
        formN = new FormN(this, true);
    }
    ...
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        form1.setVisible(true);
    }

And here is your Form1 JDialog:

public class Form1 extends javax.swing.JDialog {
    public Form1(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    ...
    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        setVisible(false);
    }      
Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
0

I only use NetBeans for GUI building because that's more convenient. In the following I can tell you how to achieve what you want to do but I can't tell you how to layout all the components because NetBeans do that for me.

So basically you want to 1. show secondFrame by clicking a menuitem and then close mainFrame, 2. show mainFrame after closing secondFrame, yes? Then, the key is to pass the reference of mainFrame to secondFrame, and write your own method of formClosing event of secondFrame. Something like this:

In the menuItem method in your mainframe:

private void menuItemActionPerformed(java.awt.event.ActionEvent evt) {
    //pass 'this' frame's (mainFrame) reference to the secondFrame
    SecondFrame newFrame = new SecondFrame(this);
    newFrame.setVisible(true); //show the secondFrame
    this.setVisible(false);  //hide this frame (mainFrame)
}

In your secondFrame:

public class SecondFrame extends javax.swing.JFrame {

private MainFrame mainFrame;

//define your own constructor that can use mainFrame's reference
public SecondFrame(MainFrame mainFrame) {
    initComponents();
    this.mainFrame = mainFrame;
}

private void initComponents(){
    //bind your own event for closing second frame
    setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent evt) {
            formWindowClosing(evt);
        }
    });

    /***********your stuff***************/
}

//show mainFrame when closing this frame and then dispose this frame
private void formWindowClosing(java.awt.event.WindowEvent evt) {
    mainFrame.setVisible(true);
    this.dispose();
}

}

The codes above is for disposing the secondFrame when closing it. If you just want to hide the secondFrame when closing for future use, then the codes will be slightly different. Let me know what you up to.

0

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
form1.setVisible(true); dispose(); }