1

I am writing a program where there is a JFrame with a JPanel with a login gui. After logging in successfully, a method is called that clears the JFrame. The issue I am having is that instead of clearing the JFrame, the stuff in the JPanel is still visibly there and it is just frozen.

Method that creates login gui:

public void logingui() {

    JPanel loginpanel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 2;
    JLabel gamename = new JLabel("InvestGame By Ama291");
    gamename.setFont(new Font("Arial", 1, 22));
    loginpanel.add(gamename, c);
    c.ipady = 20;
    c.gridx = 0;
    c.gridy = 1;
    loginpanel.add(new JPanel(), c);
    c.ipady = 10;
    c.gridwidth = 1;
    c.gridx = 0;
    c.gridy = 2;        
    loginpanel.add(new JLabel("Username: "), c);
    c.gridx = 1;
    final JTextField userfield = new JTextField(10);
    loginpanel.add(userfield, c);
    c.gridx = 0;
    c.gridy = 3;
    loginpanel.add(new JPanel(), c);
    c.gridx = 0;
    c.gridy = 4;
    loginpanel.add(new JLabel("Password: "), c);
    c.gridx = 1;
    final JPasswordField passfield = new JPasswordField(10);
    loginpanel.add(passfield, c);
    c.gridx = 0;
    c.gridy = 5;
    c.ipady = 20;
    loginpanel.add(new JPanel(), c);
    c.ipady = 10;
    c.gridx = 0;
    c.gridy = 6;
    c.gridwidth = 1;
    JButton createacc = new JButton("Create Account");
    loginpanel.add(createacc, c);
    c.gridx = 1;
    JButton login = new JButton("Log In");
    loginpanel.add(login, c);
    add(loginpanel);
}

Method to clear JFrame:

public void gamegui() {

    JPanel gamepanel = new JPanel();
    removeAll();
    invalidate();
    validate();
    repaint();
    add(gamepanel);
}

Does anyone know how I can successfully remove the JPanel from the JFrame without the buttons and stuff from the JPanel frozen inside of the window? This is the issue I'm having.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
ama291
  • 70
  • 1
  • 10
  • Please consider CardLayout, for this sort of a problem, as already stated. For [example](http://stackoverflow.com/a/9349137/1057230) :-) – nIcE cOw Aug 18 '13 at 17:24
  • Going to use CardLayout for something else in my program, thanks for showing me how to use it. – ama291 Aug 19 '13 at 05:17

3 Answers3

2

For a login it is probably better to use a popup JDialog. Then when the dialog closes you just display the panel on the frame.

The other option is to use a Card Layout. The you can swap panels as required.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I'm going to try to use a JDialog, it'll make things much easier. I don't have much experience with swing and didn't even know it existed. Thanks a lot for the suggestion! – ama291 Aug 18 '13 at 05:15
  • Ok, so I actually fixed my problem by doing setVisible(false); before the removal of the old JPanel and the addition of the new JPanel. Then I set the visibility to true after this was done and it works fine now. However, I decided that I am going to use a JDialog because it basically will have the same function and seems easier to me. – ama291 Aug 18 '13 at 05:26
1

When removing components from a JFrame, the component will be removed but it will visually still be there. Therefore, to hide it or refresh the JFrame, use the .setVisible(false) method.

0

if you ll just add

this.setLayout(your layout)

//your layout=the layout which you are using. i.e border,card etc..

between the invalidate() and validate() methods in your code in gamegui().

I hope it ll work . I havent tried but hope it ll work.

Mukesh Kumar Singh
  • 623
  • 1
  • 10
  • 18