nix
nix

Reputation: 165

Closing JFrame with Button

I am currently using Eclipse's drag and Drop feature, I have one application window which comes with JFrame by default and is able to setVisible(false); but my other frames/panel/window I have created with JPanel and with extending JFrame.

Because of extend I am unable to setVisible(false or true); it has no effect at all on the window it still remains true.

My code :

private JPanel contentPane;

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

public LibrarianMenu() {
    setTitle("Librarian");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 385, 230);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    .
    .
    . so on

Here I am trying to execute my button:

 btnLogout.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    LibrarianMenu frame = new LibrarianMenu();
                    frame.setVisible(false);
                }
            });

Any solutions for it?

Upvotes: 1

Views: 888

Answers (2)

HasS
HasS

Reputation: 201

This is happening because every time you press the button you create a new instance of that frame. Here is your code updated :

static LibrarianMenu frame ;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame = new LibrarianMenu();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

And the logout button event should be like this :

btnLogout.addActionListener(
        new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
            }
        });

Upvotes: 2

serebit
serebit

Reputation: 380

Because you're creating the frame inside the Runnable, its scope is limited to that of the runnable. Try declaring the variable outside of the runnable, then initializing it within the runnable, like so:

private JPanel contentPane;
private LibrarianMenu frame;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame = new LibrarianMenu();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Then setvisible to false without declaring a new instance of LibrarianMenu:

btnLogout.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
           frame.setVisible(false);
     }
});

Upvotes: 3

Related Questions