0

I have a login and a register panel and a window to hold all the panels. What I am trying to do is to go to the login panel when the register button is pressed. But it does not work. The method that I use would work if the login panel is inside the window but when it is in its own class it doesn't. Can someone please let me know how to do this? Here is my code for the Window

package userInterface;

import java.awt.*;
import java.awt.EventQueue;

import java.sql.*;

import javax.swing.JFrame;
import javax.swing.JPanel;

import company.CompanySys;

import javax.swing.*;
import java.awt.CardLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;

public class FrontEnd {
    
    /**
     * Variables
     */
    
    // System
    CompanySys sys = CompanySys.getSystem();
    
    private static FrontEnd  front = new FrontEnd();
    
    private JFrame frame;
    private final JLayeredPane layeredPane = new JLayeredPane();
    public final RegisterPanel registerPan = new RegisterPanel();
    public final LoginPanel loginPan = new LoginPanel();
    

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FrontEnd window = new FrontEnd();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    private FrontEnd() {
        initialize();
    }

    
    
    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        
        
        this.setFrame();
        this.setLayout();
        this.addPanels();
    
        
    }
    
    
    

    /**
     * Setting up the main frame 
     */
    private void setFrame() {
        
        frame = new JFrame();
        frame.setBounds(100, 100, 900, 700);    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        layeredPane.setBounds(0, 0, 900, 700);
        frame.getContentPane().add(layeredPane);
        layeredPane.setLayout(new CardLayout(0, 0));
    }
    
    /**
     * Setting Layout
     */
    
    public void setLayout() {
//      frame.getContentPane().add(layeredPane, "name_2578740971832500");
//      layeredPane.setLayout(new CardLayout(0, 0));
        
    }
    
    
    public void SetSizeandLocation() {
        
        this.registerPan.setBounds(10, 10, 900, 700);
        layeredPane.setLayer(loginPan, 0);
        this.loginPan.setBounds(10, 10, 900, 700);
    }
    
    /**
     * Set visibility of the components
     */
    private void setVisibility() {

//      this.registerPan.setVisible(true);
//      this.loginPan.setVisible(true);
        
    }
    
    /**
     * Adding panels to the window
     */
    private void addPanels() {
        
        layeredPane.add(registerPan);
        layeredPane.add(loginPan);
    }
    
    public static FrontEnd getFront() {
        
        return front;
    }
    
    /**
     * Switching Panels
     * @param panel
     */
    public void switchPanels(JPanel panel) {
        layeredPane.removeAll();
        layeredPane.add(panel);
        layeredPane.repaint();
        layeredPane.revalidate();
        
    }
    
    public LoginPanel getLoginPan() {
        return this.loginPan;
    }
    
    
}

Here is the code for Register Panel

package userInterface;
import company.*;

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

public class RegisterPanel extends JPanel implements ActionListener{
    
    /**
     * Variables
     */
    // System
    CompanySys sys = CompanySys.getSystem();
    FrontEnd front = FrontEnd.getFront();
    
    // Input fields
    private JTextField usernameTextField = new JTextField();
    private JPasswordField passwordField1 = new JPasswordField();
    private JPasswordField passwordField2 = new JPasswordField();
    
    
    // Labels
    private JLabel usernameLabel = new JLabel("Username");
    private JLabel password1Label = new JLabel("Password");
    private JLabel password2Label = new JLabel("Password");
    
    
    
    // Buttons
    private JButton registerButton = new JButton("REGISTER");
    private JButton cancelButton = new JButton("CANCEL");
    private JButton haveAccount = new JButton("Already have an account? Login");
    private JButton clearButton = new JButton("CLEAR");
    
    // Others
    private JCheckBox showPasswordCheckBox = new JCheckBox("Show Password");
    

    /**
     * Create the panel.
     */
    public RegisterPanel() {
        setLayout(null);
        this.addComponents();
        this.setSizeandLocations();     
        this.addActionEvent();
    }
    
    /**
     * Methods
     */
    
    // Set size and location of the components
    public void setSizeandLocations() {
        
        // Labels
        usernameLabel.setBounds(312, 106, 64, 14);
        password1Label.setBounds(324, 175, 64, 14);
        password2Label.setBounds(312, 237, 64, 14);
        
        // Input Fields
        usernameTextField.setBounds(493, 103, 96, 20);
        passwordField1.setBounds(493, 173, 97, 17);
        passwordField2.setBounds(494, 234, 96, 20);
        
        passwordField1.setEchoChar('*');
        passwordField2.setEchoChar('*');
        
        //Buttons
        registerButton.setBounds(337, 301, 89, 23);
        cancelButton.setBounds(479, 301, 89, 23);
        haveAccount.setBounds(366, 363, 196, 23);
        clearButton.setBounds(606, 301, 89, 23);
        
        // Others
        showPasswordCheckBox.setBounds(493, 261, 141, 23);
        
    }
    
    
    // Add all components to the panel
    public void addComponents() {
        
        // Add Labels 
        add(usernameLabel);
        add(password1Label);
        add(password2Label);
        
        // Add input fields
        add(usernameTextField);
        add(passwordField1);
        add(passwordField2);
        
        // Add Buttons
        add(registerButton);    
        add(cancelButton);      
        add(haveAccount);
        add(clearButton);
        
        // Others
        add(showPasswordCheckBox);
        
    }
    
    
    /**
     * Adding Action listener
     */
    public void addActionEvent() {
        
        this.registerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                front.switchPanels(front.getLoginPan());
            }
        });
        this.cancelButton.addActionListener(this);
        this.haveAccount.addActionListener(this);
        this.clearButton.addActionListener(this);
        this.showPasswordCheckBox.addActionListener(this);
        
    }
    
    /**
     * Main method of the panel 
     * @param args
     */
    public static void main(String args[]) {
        
        RegisterPanel frame = new RegisterPanel();
        frame.setVisible(true);
        frame.setBounds(10, 10, 900, 500);      
    }
    
    /**
     * Adding the functionality to the components 
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        
        String usr = this.usernameTextField.getText();
        String pass1 = String.valueOf(this.passwordField1.getPassword());
        String pass2 = String.valueOf(this.passwordField2.getPassword());
        
//      if (e.getSource() == this.registerButton) {
//          if (sys.register(usr, pass1, pass2)) {
//          //  JOptionPane.showConfirmDialog(null, "Success", getName(), JOptionPane.DEFAULT_OPTION);
//              
//              front.switchPanels(front.getLoginPan());
//          }
//          else {
//              JOptionPane.showConfirmDialog(null, "fail", getName(), JOptionPane.DEFAULT_OPTION);
//          }
//      }
        if (e.getSource() == this.cancelButton) {
            
        }
        else if (e.getSource() == this.haveAccount) {
            
        }
        else if (e.getSource() == this.showPasswordCheckBox) {
            
            if(this.showPasswordCheckBox.isSelected()) {
                this.passwordField1.setEchoChar((char) 0);
                this.passwordField2.setEchoChar((char) 0);
            }
            else {
                this.passwordField1.setEchoChar('*');
                this.passwordField2.setEchoChar('*');
            }
            
        }
        else if (e.getSource() == this.clearButton) {
            this.passwordField1.setText("");
            this.passwordField2.setText("");
            this.usernameTextField.setText("");
        }
    
    
    }
    

    
    

}

Here is code for login panel

package userInterface;
import company.CompanySys;

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

public class LoginPanel extends JPanel implements ActionListener{
    
    /**
     * Variables
     */
    // System
    CompanySys sys = CompanySys.getSystem();
    // Fields
    private JTextField userNameTextField = new JTextField();;
    private JPasswordField passwordField = new JPasswordField();;
    
    // Labels
    JLabel userNameLabel = new JLabel("Username");
    JLabel passwordLabel = new JLabel("Password");
    
    // Buttons
    JButton cancelButton = new JButton("CANCEL");
    JButton loginButton = new JButton("LOGIN");
    JButton noAccountButton = new JButton("Don't have an acount? register");
    JButton clearButton = new JButton("CLEAR");
    
    // Others
    JCheckBox showPassCheckBox = new JCheckBox("Show Password");

    
    /**
     *  Constructor
     */
    public LoginPanel() {
        setLayout(null);
        this.setSizeAndLocation();
        this.addComponent();
        this.addAction();
    }
    
    /**
     * Setting size and location of the components 
     */
    public void setSizeAndLocation() {
        
        // Labels
        userNameLabel.setBounds(97, 52, 69, 14);
        passwordLabel.setBounds(97, 88, 48, 14);
        
        // TextFields
        passwordField.setBounds(197, 85, 103, 20);
        userNameTextField.setBounds(197, 49, 96, 20);
        
        // Buttons
        clearButton.setBounds(325, 146, 89, 23);
        noAccountButton.setBounds(85, 196, 208, 23);
        cancelButton.setBounds(209, 146, 89, 23);
        loginButton.setBounds(85, 146, 89, 23);
        
        // Others
        showPassCheckBox.setBounds(197, 116, 130, 23);
    }
    
    
    /**
     * Adding components to the Panel
     */
    public void addComponent() {
        
        // Labels
        this.add(userNameLabel);
        this.add(passwordLabel);
                
        // TextFields
        this.add(userNameTextField);
        this.add(passwordField);
                
        // Buttons
        this.add(loginButton);
        this.add(cancelButton);
        this.add(noAccountButton);
        this.add(clearButton);
                
        // Others
        this.add(showPassCheckBox);

    }
    
    /**
     * Adding ActionListener to the interactive components
     */
    public void addAction() {
        
        // Buttons
        this.clearButton.addActionListener(this);
        this.cancelButton.addActionListener(this);
        this.loginButton.addActionListener(this);
        this.noAccountButton.addActionListener(this);
        
        
        // Others
        this.showPassCheckBox.addActionListener(this);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        
    }
    
    public static void main(String args[]) {
        
        LoginPanel frame = new LoginPanel();
        frame.setVisible(true);
        frame.setBounds(10, 10, 900, 700);      
    }

}

I have tried other ways too but it doesnt work!

  • Have you tried using a card layout? https://www.google.com/amp/s/www.geeksforgeeks.org/java-awt-cardlayout-class/amp/. This allows for easy switching between panels. – LuckyBandit74 Nov 26 '21 at 16:59
  • the frame is set to absoluteLayout and LayeredPane is set to CardLayout(), do you mean that? or do you mean something else? – Somaiyah Sultani Nov 26 '21 at 19:35
  • Oh now I see that you have a card layout. However, why are you not utilizing the card layouts methods? Refer to the link I sent, a card layout can show a certain panel and hide all other panels very easily. Right now, you have your own method called switchPanels() which is extra work. I encourage you to implement the card layout to its full capability. Look at the examples used in the link – LuckyBandit74 Nov 26 '21 at 19:39
  • So I tried doing that too but still nothing happens! I tried next(), last() and first() as well as show(), but no change idk what I'm doing wrong! – Somaiyah Sultani Nov 26 '21 at 23:12
  • 1
    Alright so my last suggestion would be to open up a new project and just create a simple card layout demo that switches between 2 panels using a JButton. This should be doable and, even though small, can help you understand how the card layout works. Then, you go into this project and see the differences between the 2 and hopefully find any mistakes. If it still does not work, ask a new question asking “why does the card layout not work here” and put your latest attempt, hope it helps. – LuckyBandit74 Nov 26 '21 at 23:14
  • @LuckyBandit74 *"open up a new project and just create a simple card layout demo that switches between 2 panels using a JButton"* Good idea. Cut the problem down to the most basic form. Doing so might solve the problem & demonstrate to you how to use a card layout in the actual GUI. But if not, [edit] to add that [mre] and thereby raise the chance of getting help. – Andrew Thompson Nov 26 '21 at 23:28
  • So I tried that and it seems when the button is in the main window class it can send you to any panel including the ones that were implemented in their own class, but for some reason it does not work when I press the button in let say register panel even though everything else looks good and there is no runtime issue or anything else. So for now I am implementing all the panels in the same class which will be way way too long (because I might have to add up to 20 panels) cause I'm running out of time. Hopefully I can figure it out later. – Somaiyah Sultani Nov 27 '21 at 05:24
  • *"So I tried that.."* Tried what? Who are you replying to? Tip: Add @LuckyBandit74 (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Nov 27 '21 at 05:31
  • Start with a simple working example and modify it to your needs: [1](https://stackoverflow.com/a/46013230/3992939) , [2](https://stackoverflow.com/a/61121540/3992939) – c0der Nov 27 '21 at 09:01

0 Answers0