Search code examples
javaswingjbuttonjlabeljtextfield

How to make a button show other buttons and options.


I am trying to create program where you can press one of the buttons on the first page and it will open more buttons and labels and such. But I'm not sure how to make it so that when the button is pressed it will show those other buttons and labels.

The code is

public class Japp extends JApplet implements ActionListener{

    private JPanel panel1= new JPanel();
    private JPanel panel2= new JPanel();

    JPanel container = new JPanel();
    //container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));

    JButton reg = new JButton ("Register");
    JButton log = new JButton ("Login");

    JLabel regusrlbl = new JLabel ("Enter Username");
    JLabel regpwdlbl = new JLabel ("Enter Password");
    JLabel regpwdconlbl = new JLabel ("Confirm Username");
    JTextField regusr = new JTextField("");
    JTextField regpwd = new JTextField("");
    JTextField regpwdcon = new JTextField("");
    JButton create = new JButton("Create");

    JLabel logusrlbl = new JLabel ("Enter Username");
    JLabel logpwdlbl = new JLabel ("Enter Password");
    JTextField logusr = new JTextField("");
    JTextField logpwd = new JTextField("");
    JButton login = new JButton("Login");

    public void init(){
    setLayout(null); 
    add(reg).setBounds(50,100,100,30);
    add(log).setBounds(150,100,100,30);

    }
    @SuppressWarnings("deprecation")

    public void actionPerformed(ActionEvent e) {
        JButton source = (JButton)e.getSource();
        if (source.getLabel()=="reg"){

        // TODO Auto-generated method stub
            remove(reg);
            remove(log);
            add(regusrlbl).setBounds(20,20, 150,50);
            add(regusr).setBounds(20,50,100,30);
            add(regpwdlbl).setBounds(200,20,150,50);
            add(regpwd).setBounds(200,50,100,30);
            add(regpwdconlbl).setBounds(195,80,150,30);
            add(regpwdcon).setBounds(200,110,100,30);
            add(create).setBounds(110,140,100,30);
        // setup all the context...
        }   
        else if(source.getLabel()=="log"){
            remove(reg);
            remove(log);
            add(logusrlbl).setBounds(20,20, 150,50);
            add(logusr).setBounds(20,50,100,30);
            add(logpwdlbl).setBounds(200,20,150,50);
            add(logpwd).setBounds(200,50,100,30);
            add(login).setBounds(110,140,100,30);
        }
    }   
}

Solution

    1. Avoid null layouts. You don't control how fonts are rendered on different system so unless you intent to only run you application on the machine it was developed for, avoid null layouts. Learn to use and understand how different layout managers can be used to achieve what you want.
    2. Use a CardLayout, this is what it was designed to do, allow you to easily change groups of visible components.

    This will require you to break down you application into separate components/panels but will allow you focus on the individual needs of each section independently

    Update with example

    enter image description hereenter image description hereenter image description here

    import java.awt.CardLayout;
    import java.awt.Container;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JSeparator;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestCardLayout100 {
    
        public static void main(String[] args) {
            new TestCardLayout100();
        }
    
        public TestCardLayout100() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    LoginPane loginPane = new LoginPane();
                    RegisterPane registerPane = new RegisterPane();
    
                    JPanel startPane = new JPanel(new GridBagLayout());
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridx = 0;
                    gbc.gridy = 0;
                    startPane.add(loginPane, gbc);
                    gbc.gridx = 2;
                    startPane.add(registerPane, gbc);
                    gbc.gridx = 1;
                    gbc.fill = GridBagConstraints.VERTICAL;
                    startPane.add(new JSeparator(JSeparator.VERTICAL), gbc);
    
                    JPanel loggedInPane = new JPanel(new GridBagLayout());
                    loggedInPane.add(new JLabel("Logged In..."));
    
                    JPanel registeredPane = new JPanel(new GridBagLayout());
                    registeredPane.add(new JLabel("Registered..."));
    
                    final JFrame frame = new JFrame("Testing");
                    final CardLayout cardLayout = new CardLayout();
    
                    loginPane.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            cardLayout.show(frame.getContentPane(), "loggedIn");
                        }
                    });
                    registerPane.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            cardLayout.show(frame.getContentPane(), "registered");
                        }
                    });
    
                    Container contentPane = frame.getContentPane();
                    contentPane.setLayout(cardLayout);
                    contentPane.add(startPane, "startPane");
                    contentPane.add(loggedInPane, "loggedIn");
                    contentPane.add(registeredPane, "registered");
    
                    cardLayout.show(contentPane, "startPane");
    
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class LoginPane extends JPanel {
    
            private final JLabel logusrlbl = new JLabel("Enter Username");
            private final JLabel logpwdlbl = new JLabel("Enter Password");
            private final JTextField logusr = new JTextField(10);
            private final JTextField logpwd = new JPasswordField(10);
            private final JButton login = new JButton("Login");
    
            public LoginPane() {
                setLayout(new GridBagLayout());
    
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                add(new JLabel("Login"), gbc);
    
                gbc.gridy++;
                gbc.gridwidth = 1;
                add(logusrlbl, gbc);
                gbc.gridy++;
                add(logpwdlbl, gbc);
    
                gbc.gridx++;
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.gridy = 1;
    
                add(logusr, gbc);
                gbc.gridy++;
                add(logpwd, gbc);
    
                gbc.gridx = 0;
                gbc.gridy++;
                gbc.anchor = GridBagConstraints.EAST;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.fill = GridBagConstraints.NONE;
                add(login, gbc);
    
            }
    
            public void addActionListener(ActionListener listener) {
                login.addActionListener(listener);
            }
    
        }
    
        public class RegisterPane extends JPanel {
    
            private final JLabel logusrlbl = new JLabel("Enter Username");
            private final JLabel logpwdlbl = new JLabel("Enter Password");
            private final JTextField logusr = new JTextField(10);
            private final JTextField logpwd = new JPasswordField(10);
            private final JButton register = new JButton("Register");
    
            public RegisterPane() {
                setLayout(new GridBagLayout());
    
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                add(new JLabel("Register"), gbc);
    
                gbc.gridy++;
                gbc.gridwidth = 1;
                add(logusrlbl, gbc);
                gbc.gridy++;
                add(logpwdlbl, gbc);
    
                gbc.gridx++;
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.gridy = 1;
    
                add(logusr, gbc);
                gbc.gridy++;
                add(logpwd, gbc);
    
                gbc.gridx = 0;
                gbc.gridy++;
                gbc.anchor = GridBagConstraints.EAST;
                gbc.fill = GridBagConstraints.NONE;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                add(register, gbc);
    
            }
    
            public void addActionListener(ActionListener listener) {
                register.addActionListener(listener);
            }
    
        }
    
    }