0

Good afternoon!

I have this code:

private static class ClickListener implements ActionListener {

    public ClickListener() {

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JFrame frame = new JFrame();
        JLabel label = new JLabel("Opção Indisponivel");
        JPanel panel = new JPanel();
        frame.add(label, BorderLayout.CENTER);
        frame.setSize(300, 400);
        JButton button = new JButton("Voltar");
        button.addActionListener(new CloseWindowListener());
        panel.add(button);
        frame.add(panel, BorderLayout.SOUTH);
        frame.setVisible(true);
    }
}

private static class CloseWindowListener implements ActionListener {

    public CloseWindowListener() {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        setVisible(false);
    }
}

What I want to do is when i click on the button "voltar" (which is in another window, not on the "main" one as you can see) it closes the windows but not the app itselft. The setVisible line gives me an error about that it cannot be referenced by a static context which I understand because I need the reference of the frame. How do I solve this?

EDIT: Changed JFrame to JDialog but still no sucess. Both windows are shutdown.

Thanks in advance, Diogo Santos

Diogo Santos
  • 780
  • 4
  • 17
  • 1
    Your GUI should only have one main application window -- one JFrame. Any other sub or dependent windows should be JDialogs, not JFrames, and closing these can't close the application. – Hovercraft Full Of Eels May 07 '16 at 15:15
  • Also please have a look at [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) – Hovercraft Full Of Eels May 07 '16 at 15:15
  • 1
    @HovercraftFullOfEels, This question was marked as a duplicate of: http://stackoverflow.com/questions/4268749/how-to-close-a-jframe-without-closing-the-main-program. However I don't think it is a duplicate, so I reopened the question. This question is about `The setVisible line gives me an error about that it cannot be referenced by a static context which I understand because I need the reference of the frame. How do I solve this?` – camickr May 07 '16 at 16:23
  • @camickr: thanks for the correction. – Hovercraft Full Of Eels May 07 '16 at 17:33

1 Answers1

1

The setVisible line gives me an error about that it cannot be referenced by a static context which I understand because I need the reference of the frame. How do I solve this?

You can access the component that generated the event. Then you can find the window the component belongs to. This will give you generic code to hide any window:

//setVisible(false);
JButton button = (JButton)e.getSource();
Window window = SwingUtilities.windowForComponent(button);
window.setVisible(false);

You can also check out Closing an Application. The ExitAction can be added to your button. Now when you click the button it will be like clicking the "x" (close) button of the window. That is whatever default close operation your specify for the window will be invoked.

camickr
  • 321,443
  • 19
  • 166
  • 288