0

i'm at a standstill developing a java application. I have a series of JFrames that open each other, and once I open the next, the frame before shall close. Now, all works fine, I used

setVisible(false);

to "close" the frame, but I have some problems with a specific form I generated: The form has some panels inside, and the panels each have a button which opens the next form. Now, I wonder how can I apply setVisible(false) to the form that contains those panels.

ChooseForm is the name of the form that contains the panels; And I have this event handling the button click of each panel's button

private void btn_scegliMouseClicked(java.awt.event.MouseEvent evt) {                                        
    Database.getAbitazioneByCodice(label_codice.getText());
    MainForm.showFrame();

} 

Thanks in advance for the help.

DaviCode
  • 1
  • 4
  • 1
    You may use `SwingUtilities.getWindowAncestor` : https://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#getWindowAncestor(java.awt.Component) – Arnaud May 29 '17 at 15:19
  • 1
    *"I have a series of JFrames .."* 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 29 '17 at 18:42
  • [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – MadProgrammer May 29 '17 at 20:49

1 Answers1

0

There are many ways to do this. A Simple way you can do like this:

class YourFrame extends JFrame implements ActionListener {

    private YourPanel panel;

    public YourFrame() {
        //...
        panel.addButtonActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        //show other Frame and hide this Frame
    }
}
class YourPanel extends JPanel {
    private JButton yourButton;

    public void addButtonActionListener(ActionListener listener) {
        yourButton.addActionListener(listener);
    }
}
Esc Điệp
  • 346
  • 2
  • 8