0

As opposed to making a JFrame object, I extended JFrame and created a Panel. I want the panel I created to close when the user clicks certain buttons, but the panel still stays open, even when I put an action listener with the dispose() method. I know my code is a mess right now but if you could please take a look and see what I'm doing wrong.

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


public class StartUp extends JFrame{

    private JButton createAccount, login;
    private static final int WIDTH = 500;
    private static final int HEIGHT = 300;

    public StartUp(){

        createView();

        setTitle("MovieBase Login");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setMinimumSize(new Dimension(400, 200));
        setLocationRelativeTo(null);
        setResizable(false);

    }

    private void createView(){

        JPanel panel = new JPanel(new GridBagLayout());
        getContentPane().add(panel);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(10,10,10,10);

        constraints.gridx = 0;
        constraints.gridy = 1;

        JLabel label = new JLabel("Welcome to MovieBase, your personal Movie Database!");
        panel.add(label, constraints);

        constraints.gridx = 0;
        constraints.gridy = 2;
        createAccount = new JButton("Create Account");

        panel.add(createAccount, constraints);

        createAccount.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                new CreateAccount();
            }

        });


        createAccount.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
               dispose();
            }
        });


        constraints.gridx = 0;
        constraints.gridy = 3;

        login = new JButton("Login");
        panel.add(login, constraints);

        login.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                new Login();
            }

        });

        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {

            }
        });

    }


    public static void main(String[] args)
    {
       SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                try{
                    StartUp window = new StartUp();
                    window.setVisible(true);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}  

Seperate Class

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

public class CreateAccount extends JFrame{

    private JLabel user, pass; //label next to username and password field
    private JTextField username; //enter username
    private JPasswordField password; //enter password
    private JButton create;
    private String u, p;
    private static final int WIDTH = 500;
    private static final int HEIGHT = 300;

    public CreateAccount(){

        createView();

        setTitle("Create Account");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setMinimumSize(new Dimension(WIDTH, HEIGHT));
        setLocationRelativeTo(null);
        setResizable(false);


    }

    public void setUser(String usn){
        u = usn;
    }

    public String getUser(){
        return u;
    }

    public void setPass(String psd){
        p = psd;
    }

    public String getPass(){
        return p;
    }


    private void createView(){

        setVisible(true);
        JPanel panelMain = new JPanel(null);
        getContentPane().add(panelMain);

        user = new JLabel("Username: ");
        panelMain.add(user);
        user.setBounds(50, 30, 120, 25);

        username = new JTextField();
        panelMain.add(username);
        username.setBounds(140, 30, 150, 25);


        pass = new JLabel("Password: ");
        panelMain.add(pass);
        pass.setBounds(50, 100, 120, 25);

        password = new JPasswordField();
        panelMain.add(password);
        password.setBounds(140, 100, 150, 25);

        create = new JButton("Create");
        panelMain.add(create);
        create.setBounds(170, 180, 100,25);


        create.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String usn = username.getText();
                String psd = password.getText();
                setUser(usn);
                setPass(psd);
            }


        });

        create.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                new Login2().setVisible(true);
            }

        });

        create.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               dispose();
            }
        });



    }

}
ruakh
  • 175,680
  • 26
  • 273
  • 307
Snaxia
  • 9
  • 1
  • Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). In this case, we'd add both the 'open' panel and another entirely blank panel. On 'close', flip the view to the blank panel. – Andrew Thompson Dec 11 '18 at 00:51

1 Answers1

1

There is no dispose method on JPanel - I see you calling it twice, both times on your extensions of JFrame.

More specifically - methods in Java are always called on some object with the pattern object.method(arguments) - Java provides a shortcut notation - when the object is not specified, this is implied. When you call dispose(), you're not explicitly providing any object it's being called on, and this in that context is your extensions of JFrame.

If you just want to hide the panel, you can call setVisible(false)

Krease
  • 15,805
  • 8
  • 54
  • 86
  • do you know where in the program I would call setVisible(false) ? – Snaxia Dec 11 '18 at 00:39
  • You'd call it in the place you want to hide your panel. More specifically, you would call `mainPanel.setVisible(false)` or `panel.setVisible(false)` (or whichever is the name of your JPanel object). Side note - I'm confused why all of your buttons have multiple `ActionListener` attached (ie: your `create` button has 3 listeners - this is really the equivalent of having one listener with all the same code in it - is that what you intend?) – Krease Dec 11 '18 at 00:44
  • Oh okay thank you. And yes I changed that, haha, I thought I needed separate action listeners but I realized I didn't. – Snaxia Dec 11 '18 at 00:46