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
  • 166,747
  • 40
  • 210
  • 420
Ultrabyte
  • 227
  • 1
  • 4
  • 6

4 Answers4

23

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,276
  • 2
  • 12
  • 27
  • 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,000
  • 23
  • 38
  • 54
rajeesh
  • 586
  • 9
  • 18
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
  • 3,792
  • 3
  • 27
  • 32
1
JFrame.setVisible(true);

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

Enamul Hassan
  • 5,000
  • 23
  • 38
  • 54