2

So, I'm creating a sort menu and I have a Log on screen.When the user logs on it loads another menu. I have it load the second menu but i don't know how to wipe the log in button and the textfields etc. how would i go about doing this?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends  JFrame{

    public void fixtureList()   //Here is where all of the fixture/timetable elements are based.
    {
        JButton editButton;
        JButton createButton;
        JButton logoutButton;

        editButton = new JButton("Edit");
        editButton.setBounds(10, 10, 80, 30);
        add(editButton);

        createButton = new JButton(("Create"));
        createButton.setBounds(145, 10, 80, 30);
        add(createButton);

        logoutButton = new JButton(("Log Out"));
        logoutButton.setBounds(10, 350, 80, 30);
        add(logoutButton);
        logoutButton.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                loginPanel();
                System.out.println("Loading the login screen");
                repaint();
            }

        });

    }

    public void loginPanel()    //This is where the screen containing all of the login elements is based.
    {
        setLayout(null);

        JButton loginButton;
        JTextField usernameField;
        JPasswordField passwordField;


        loginButton = new JButton("Login");
        loginButton.setBounds(80, 250, 70, 30);
        add(loginButton);
        usernameField = new JTextField(15);
        usernameField.setBounds(60, 110, 130, 30);
        add(usernameField);
        passwordField = new JPasswordField(15);
        passwordField.setBounds(60, 150, 130, 30);
        add(passwordField);


        loginButton.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                fixtureList();
                System.out.println("Loading the fixtures screen");
                repaint();
            }

        });

    }

    public static void main(String[] args)
    {

        Main window = new Main();

        window.setTitle("PE Fixtures v1.0");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(250, 430);
        window.loginPanel();
        window.setResizable(false);
        window.getContentPane().setBackground(new Color(53, 56, 64));
        window.setVisible(true);



    }
}
            });

2 Answers2

1

I tried CardLayout earlier and wasn't too happy with it.

I urge you to revisit CardLayout, described here and illustrated here. It's designed for exactly the effect you describe.

As an an aid to understanding, compare this more primitive approach that removes components and uses revalidate() to mark components as needing to be laid out again.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

While CardLayout is a cleaner approach, you can also just lift the widgets to instance variables so the click handlers can change their visibility. I've modified your example here. However using Swing without layout managers is leading with your chin. Some machine somewhere is going to produce a dorked up (technical term) rendering of the widgets.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SwingTest extends JFrame {

    JButton loginButton;
    JTextField usernameField;
    JPasswordField passwordField;
    JButton editButton;
    JButton createButton;
    JButton logoutButton;

    private void setLoginFieldVisibility(boolean val) {
        loginButton.setVisible(val);
        usernameField.setVisible(val);
        passwordField.setVisible(val);
        invalidate();
    }

    private void setFixtureFieldVisibility(boolean val) {
        editButton.setVisible(val);
        createButton.setVisible(val);
        logoutButton.setVisible(val);
        invalidate();
    }

    private void initialize() {
        setLayout(null);
        loginButton = new JButton("Login");
        loginButton.setBounds(80, 250, 70, 30);
        add(loginButton);
        usernameField = new JTextField(15);
        usernameField.setBounds(60, 110, 130, 30);
        add(usernameField);
        passwordField = new JPasswordField(15);
        passwordField.setBounds(60, 150, 130, 30);
        add(passwordField);        
        editButton = new JButton("Edit");
        editButton.setBounds(10, 10, 80, 30);
        add(editButton);
        createButton = new JButton(("Create"));
        createButton.setBounds(145, 10, 80, 30);
        add(createButton);
        logoutButton = new JButton(("Log Out"));
        logoutButton.setBounds(10, 350, 80, 30);
        add(logoutButton);

        setFixtureFieldVisibility(false);

        loginButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setLoginFieldVisibility(false);
                setFixtureFieldVisibility(true);
            }
        });

        logoutButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setLoginFieldVisibility(true);
                setFixtureFieldVisibility(false);
            }
        });
    }

    public static void main(String[] args) {

        SwingTest window = new SwingTest();

        window.setTitle("PE Fixtures v1.0");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(250, 430);
        window.initialize();
        window.setResizable(false);
        window.getContentPane().setBackground(new Color(53, 56, 64));
        window.setVisible(true);
    }
}
Gene
  • 46,253
  • 4
  • 58
  • 96
  • Thanks, may i ask what "val" does? Also was i anywhere near having it work before you helped me? –  Feb 16 '14 at 09:38
  • `val` is a method parameter that in this case holds the Boolean visibility value of widgets. This question indicates you need to go back and learn more about basic Java programming before Swing programming will make much sense. Your starting code was typical of folks who are trying to cut-and-paste from Internet examples to get to a working solution. Nothing wrong with cut-and-paste, but the basic language understanding must be there before it will work. – Gene Feb 16 '14 at 15:17
  • Oh ok. By the way it wasn't cut and pasted. just felt logical(Obviously wasn't xD). Last time I touched Java was a couple of years ago this will explain my issues. Thanks. –  Feb 16 '14 at 15:28