0

If I want that on clicking a certain button the current frame should close and a new frame with new content should appear, how can I do that? For example, I have a login button, which on clicking leads to a new frame and closing the old frame.

public void actionPerformed(ActionEvent evt) {
 setVisible(false);//for closing the old frame

Now how can I add a new frame?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

You should use as you did, this.setvisible(false) or this.dispose(); depending on your requirements.

Theres a few posts that cover it but see here for specifics.

public void actionPerformed(ActionEvent evt) {
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
frame.pack();

//added new frame, set it to visible
frame.setVisible(true);
 //hide old one with setVisible or dispose()
this.setVisible(false); //or you'd be better using  this.dispose();
}
Community
  • 1
  • 1
D3181
  • 2,037
  • 5
  • 19
  • 44