0

I am trying to Enable my mainJframe after disabling it to use another jFrame. i disable it after : actionPerformed with : frame.setEnabled(false); the 2nd jFrame works and closes just fine, but I cant get my mainJframe enabeled again.

public class Main {

    JFrame frame;
    public static void main(String[] args) {...}
    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 13));
        frame.setBounds(50, 10, 1050, 650);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new CardLayout(0, 0));

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

        JMenu menu1 = new JMenu("Something");
        menuBar.add(menu1);

        JMenuItem menuItem1 = new JMenuItem("go to frame 2");
        mntmAddStudent.setFont(new Font("Segoe UI Semilight", Font.PLAIN, 12));
        menu1.add(menuItem1);
        menuItem1.addActionListener(new ActionListener() {          
            public void actionPerformed(ActionEvent arg0) {

                frame.setEnabled(false); /* ???diable it here or in OtherClass??? */    

                OtherClass otherClass = new OtherClass ();
                otherClass .setVisible(true);

            }
        });

    }
}

public class OtherClass extends JFrame {

    private JFrame otherClass;
    public static void main(String[] args) {...}

    public OtherClass() {
        otherClass = new JFrame();
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {

             public void windowClosing(WindowEvent e) {
                    System.out.println("closing...");
                    JFrame frame = (JFrame)e.getSource();

                    int result = JOptionPane.showConfirmDialog(frame,"Are you sure you to close this frame?","Exit Application",JOptionPane.YES_NO_OPTION);

                    if (result == JOptionPane.YES_OPTION){
                ***What do I do here to enable main frame again?***      
                        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                        }
               });
    }
}   

I also have multiple layouts in main and too many jFrames to change them to jDialogs now. I am using eclipse Kepler. What is the problem?

Vicki K
  • 23
  • 8

1 Answers1

1

Basically what you "seem" to have done is create a new instance Main and enabled that, instead of enabling the instance you previously disable.

Start by getting rid of Main m = new Main(), but unless we have more code to go by, it's impossible to suggest how you would reference the parent frame.

A better solution might be to use a modal JDialog or JOptionPane as the second window, which would prevent the user from interacting with the first frame until it was closed.

Take a look at How to Make Dialogs for more details

An actual runnable example that demonstrates your problem would involve less guess work and better responses

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366