20

I have a JFrame class and it was made in the design section on Netbeans. I am trying to make a log in button that takes closes the current frame and opens another, is there anyway I can do that?

I have tried:

JFrame frame = new JFrame(); 

But I want it to be editable in the design section!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ultrabyte
  • 227
  • 1
  • 4
  • 6

4 Answers4

24

Double Click the Login Button in the NETBEANS or add the Event Listener on Click Event (ActionListener)

btnLogin.addActionListener(new ActionListener() 
{
    public void actionPerformed(ActionEvent e) {
        this.setVisible(false);
        new FrmMain().setVisible(true); // Main Form to show after the Login Form..
    }
});
Gokul E
  • 1,356
  • 2
  • 13
  • 28
  • 5
    It's better to use `this.dispose()` instead making it invisible as it will be still running without any use. – Azad Jul 14 '13 at 07:46
  • Yea Thats correct. But i thought that while the Form is dispose it may return to Main Function without showing the FrmMain.. OOPS... I thought the Code will be Stopped then... – Gokul E Jul 14 '13 at 07:51
  • 1
    Great thought :) The code won't be stoped until executing all statements inside the actionPerformed method. – Azad Jul 14 '13 at 07:56
8
new SecondForm().setVisible(true);

You can either use setVisible(false) or dispose() method to disappear current form.

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
rajeesh
  • 616
  • 10
  • 20
6

This link works with me: video

The answer posted before didn't work for me until the second click

So what I did is Directly call:

        new NewForm().setVisible(true);

        this.dispose();//to close the current jframe
Chris Sim
  • 4,054
  • 4
  • 29
  • 36
1
JFrame.setVisible(true);

You can either use setVisible(false) or dispose() method to disappear current form.

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56