BadAssPanda
BadAssPanda

Reputation: 33

Java JFrame remove and repaint components not working

So I started to go into OOP and also started to learn about swing library but I have trouble. When I try to remove all of the JFrame components it doesnt work. What I want to do is when the user clicks a button I have to remove all the JFrame components and add new ones but it doesn't work despite that I used removeAll() repait(), revalidate() etc. Here is my code for the BankApp class:

import javax.swing.*;

public class BankApp{

public static void main(String[] args) {
    BankGUI object1;
    object1 = new BankGUI();
    object1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    object1.setSize(700, 500);
    object1.setLocationRelativeTo(null);
    object1.setResizable(false);
    object1.setVisible(true);

}

}

Here is the BankGUI:

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

public class BankGUI extends JFrame{

private JButton CreateAccount;
private JButton LoginAccount;
private JButton Exit;
private JButton AboutButton;
private JButton ExitButton;
private JLabel IntroText;


public BankGUI(){
    super("Banking App");
    createMainMenu();

}

public void createMainMenu(){
    add(Box.createRigidArea(new Dimension(0,40)));
    IntroText = new JLabel("Banking Application");
    IntroText.setMaximumSize(new Dimension(280,60));
    IntroText.setFont(new Font("Serif", Font.PLAIN, 34));
    IntroText.setAlignmentX(CENTER_ALIGNMENT);
    add(IntroText);

    add(Box.createRigidArea(new Dimension(0,40)));

    setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
    CreateAccount = new JButton("Register");
    CreateAccount.setMaximumSize(new Dimension(200,50));
    CreateAccount.setFont(new Font("Serif", Font.PLAIN, 24));
    CreateAccount.setAlignmentX(CENTER_ALIGNMENT);
    CreateAccount.setFocusable(false);
    add(CreateAccount);

    add(Box.createRigidArea(new Dimension(0,20)));

    LoginAccount = new JButton("Login");
    LoginAccount.setMaximumSize(new Dimension(200,50));
    LoginAccount.setFont(new Font("Serif", Font.PLAIN, 24));
    LoginAccount.setAlignmentX(CENTER_ALIGNMENT);
    LoginAccount.setFocusable(false);
    add(LoginAccount);

    add(Box.createRigidArea(new Dimension(0,20)));

    AboutButton = new JButton("About");
    AboutButton.setMaximumSize(new Dimension(200,50));
    AboutButton.setFont(new Font("Serif", Font.PLAIN, 24));
    AboutButton.setAlignmentX(CENTER_ALIGNMENT);
    AboutButton.setFocusable(false);
    add(AboutButton);

    add(Box.createRigidArea(new Dimension(0,20)));

    ExitButton = new JButton("Exit");
    ExitButton.setMaximumSize(new Dimension(200,50));
    ExitButton.setFont(new Font("Serif", Font.PLAIN, 24));
    ExitButton.setAlignmentX(CENTER_ALIGNMENT);
    ExitButton.setFocusable(false);
    add(ExitButton);

    ButtonListener actionListener = new ButtonListener(CreateAccount, LoginAccount, AboutButton, ExitButton);
    CreateAccount.addActionListener(actionListener);
    LoginAccount.addActionListener(actionListener);
    AboutButton.addActionListener(actionListener);
    ExitButton.addActionListener(actionListener);
}
}

And here is my ButtonListener Class:

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

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonListener implements ActionListener{
private JButton CreateAccount;
private JButton LoginAccount;
private JButton AboutButton;
private JButton ExitButton;


public ButtonListener(JButton button1,JButton button2,JButton button3,JButton button4){
    CreateAccount = button1;
    LoginAccount = button2;
    AboutButton = button3;
    ExitButton = button4;
}
@Override
public void actionPerformed(ActionEvent e) {
    BankGUI bankgui = new BankGUI();
    if(e.getSource() == CreateAccount){
        System.out.println("This prints out");
    }else if(e.getSource() == ExitButton){
        bankgui.getContentPane().removeAll();
        bankgui.removeAll();
        bankgui.validate();
        bankgui.repaint();
        System.out.println("Code reaches here but it doesnt clear the screen");
    }
}


}

When I try to do it nothing happens despite that I revalidate and repaint I'm not sure why.I just want to clear the JFrame. I'm 100% sure that the code reaches the methods but for some reason they don't work. Maybe its obvious mistake but I'm not seeing it. Any help would be appreciated.

Upvotes: 2

Views: 915

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347214

In your actionPerformed method, you are creating another instance of BankGUI, this is, in no way, connected to the instance which is been displayed to the user...

@Override
public void actionPerformed(ActionEvent e) {
    BankGUI bankgui = new BankGUI();
    //...
}

There are a few ways you "might" correct this, most of them aren't pretty.

You could pass a reference of BankGUI to the ButtonListener along with the buttons. I don't like this idea, as it provides the means for ButtonListener to start doing things to BankGUI it really has no responsibility for doing.

You could use SwingUtilities.getWindowAncestor, passing a reference of the button which was triggered, to get a reference to the window. This has the sam problem as the previous idea, but it also makes assumptions about the structure of the UI, which, ButtonListener shouldn't care about.

In both cases, this couples ButtonListener to BankGUI.

A better solution would be to provide some kind of "navigation manager". The ButtonListener would take a reference of it and when one of the buttons is actioned, would notify the "navigation manager", asking it to perform the actual task.

This de-couples the ButtonListener from the implementation of the UI.

This is all about concepts of "isolation of responsibility", "code reusability" and "model-view-controller"

Overall, a simpler and more practical solution would be to use a CardLayout, which will further de-couple the UI and make it easier to isolate functionality to their own classes/components and allow the primary UI to switch between them

import java.awt.CardLayout;
import static java.awt.Component.CENTER_ALIGNMENT;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JavaApplication101 {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                BankGUI object1;
                object1 = new BankGUI();
                object1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                object1.pack();
                object1.setLocationRelativeTo(null);
                object1.setVisible(true);
            }
        });
    }

    public interface BankNavigationController {

        public void setView(String command);
    }

    public static class CardLayoutBankNavigationController implements BankNavigationController {

        private Container parent;
        private CardLayout layout;

        public CardLayoutBankNavigationController(Container parent, CardLayout layout) {
            this.parent = parent;
            this.layout = layout;
        }

        @Override
        public void setView(String command) {
            layout.show(parent, command);
        }

    }

    public static class BankGUI extends JFrame {

        private CardLayoutBankNavigationController controller;

        public BankGUI() {
            super("Banking App");
            CardLayout layout = new CardLayout();
            setLayout(layout);
            controller = new CardLayoutBankNavigationController(getContentPane(), layout);

            add("Main Menu", createMainMenu());
            add("Register", otherView("Register"));
            add("Login", otherView("Login"));
            add("About", otherView("About"));
            add("Exit", otherView("Exit"));

            controller.setView("Main Menu");
        }

        public JPanel otherView(String named) {
            JPanel view = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            view.add(new JLabel(named), gbc);

            JButton mainMenu = new JButton("Main Menu");
            view.add(mainMenu, gbc);

            ButtonListener actionListener = new ButtonListener(controller);
            mainMenu.addActionListener(actionListener);

            return view;
        }

        public JPanel createMainMenu() {
            JPanel menu = new JPanel();
            menu.setLayout(new BoxLayout(menu, BoxLayout.PAGE_AXIS));

            menu.add(Box.createRigidArea(new Dimension(0, 40)));
            JLabel IntroText = new JLabel("Banking Application");
            IntroText.setMaximumSize(new Dimension(280, 60));
            IntroText.setFont(new Font("Serif", Font.PLAIN, 34));
            IntroText.setAlignmentX(CENTER_ALIGNMENT);
            menu.add(IntroText);

            menu.add(Box.createRigidArea(new Dimension(0, 40)));

            JButton CreateAccount = new JButton("Register");
            CreateAccount.setMaximumSize(new Dimension(200, 50));
            CreateAccount.setFont(new Font("Serif", Font.PLAIN, 24));
            CreateAccount.setAlignmentX(CENTER_ALIGNMENT);
            CreateAccount.setFocusable(false);
            menu.add(CreateAccount);

            menu.add(Box.createRigidArea(new Dimension(0, 20)));

            JButton LoginAccount = new JButton("Login");
            LoginAccount.setMaximumSize(new Dimension(200, 50));
            LoginAccount.setFont(new Font("Serif", Font.PLAIN, 24));
            LoginAccount.setAlignmentX(CENTER_ALIGNMENT);
            LoginAccount.setFocusable(false);
            menu.add(LoginAccount);

            menu.add(Box.createRigidArea(new Dimension(0, 20)));

            JButton AboutButton = new JButton("About");
            AboutButton.setMaximumSize(new Dimension(200, 50));
            AboutButton.setFont(new Font("Serif", Font.PLAIN, 24));
            AboutButton.setAlignmentX(CENTER_ALIGNMENT);
            AboutButton.setFocusable(false);
            menu.add(AboutButton);

            menu.add(Box.createRigidArea(new Dimension(0, 20)));

            JButton ExitButton = new JButton("Exit");
            ExitButton.setMaximumSize(new Dimension(200, 50));
            ExitButton.setFont(new Font("Serif", Font.PLAIN, 24));
            ExitButton.setAlignmentX(CENTER_ALIGNMENT);
            ExitButton.setFocusable(false);
            menu.add(ExitButton);

            ButtonListener actionListener = new ButtonListener(controller);
            CreateAccount.addActionListener(actionListener);
            LoginAccount.addActionListener(actionListener);
            AboutButton.addActionListener(actionListener);
            ExitButton.addActionListener(actionListener);

            return menu;
        }
    }

    public static class ButtonListener implements ActionListener {

        private BankNavigationController controller;

        public ButtonListener(BankNavigationController controller) {
            this.controller = controller;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            controller.setView(e.getActionCommand());
        }

    }

}

Upvotes: 2

Related Questions