I have been looking for this for days now and I haven't found a solution so I just made my account.
I want to figure out how should I open another JPanel from the current JPanel in my JFrame. I have made three classes till now in the view package: mainView (JFrame), loginAs (JPanel), loginPanel (JPanel). What I want to do is, I want to display loginAs Panel as my first panel where user selects whether he is an Admin or Staff and then after clicking the desired option, it should open the loginPanel where they can log in. I'm stuck here as I don't know how I can give back the information to mainView that the user has selected Admin or Staff and that mainView should now move to the next panel that is loginPanel.
Here is the coding I did:
mainView
package view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import java.awt.Color;
public class mainView extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainView frame = new mainView();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public mainView() {
setTitle("COVID'19 Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 550, 410);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new CardLayout(0, 0));
JPanel p1 = new loginAs();
contentPane.add(p1, "t1");
JPanel p2 = new loginPanel();
contentPane.add(p2, "t2");
JPanel p3 = new JPanel();
contentPane.add(p3, "t3");
JPanel p4 = new JPanel();
contentPane.add(p4, "t4");
}
}
loginAs
package view;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import view.mainView;
public class loginAs extends JPanel {
/**
* Create the panel.
*/
public loginAs() {
setBackground(Color.BLACK);
setLayout(null);
JLabel lblNewLabel = new JLabel("COVID'19 Management System");
lblNewLabel.setForeground(Color.WHITE);
lblNewLabel.setFont(new Font("Calibri", Font.BOLD, 18));
lblNewLabel.setBounds(140, 27, 262, 28);
add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("LOGIN AS");
lblNewLabel_1.setForeground(Color.WHITE);
lblNewLabel_1.setFont(new Font("Calibri", Font.BOLD, 16));
lblNewLabel_1.setBounds(35, 87, 97, 28);
add(lblNewLabel_1);
JButton btnAdmin = new JButton("ADMIN");
btnAdmin.setForeground(Color.BLACK);
btnAdmin.setBackground(Color.WHITE);
btnAdmin.setBounds(35, 134, 97, 39);
add(btnAdmin);
btnAdmin.setBorderPainted(false);
btnAdmin.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
btnAdmin.setBackground(Color.GREEN);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
btnAdmin.setBackground(Color.WHITE);
}
public void mouseClicked(java.awt.event.MouseEvent e)
{
}
});
JButton btnStaff = new JButton("STAFF");
btnStaff.setForeground(Color.BLACK);
btnStaff.setBackground(Color.WHITE);
btnStaff.setBounds(35, 200, 97, 39);
add(btnStaff);
btnStaff.setBorderPainted(false);
btnStaff.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
btnStaff.setBackground(Color.GREEN);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
btnStaff.setBackground(Color.WHITE);
}
public void mouseClicked(java.awt.event.MouseEvent e)
{
}
});
}
}
loginPanel
package view;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
public class loginPanel extends JPanel {
private JTextField txt_uname;
private JTextField txt_pass;
/**
* Create the panel.
*/
public loginPanel() {
setBackground(Color.BLACK);
setLayout(null);
JLabel lblNewLabel = new JLabel("COVID'19 Management System");
lblNewLabel.setForeground(Color.WHITE);
lblNewLabel.setFont(new Font("Calibri", Font.BOLD, 18));
lblNewLabel.setBounds(140, 27, 262, 28);
add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("LOGIN PAGE");
lblNewLabel_1.setForeground(Color.WHITE);
lblNewLabel_1.setFont(new Font("Calibri", Font.BOLD, 16));
lblNewLabel_1.setBounds(35, 87, 97, 28);
add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("Username");
lblNewLabel_2.setFont(new Font("Calibri", Font.BOLD, 14));
lblNewLabel_2.setForeground(Color.WHITE);
lblNewLabel_2.setBounds(35, 142, 74, 14);
add(lblNewLabel_2);
txt_uname = new JTextField();
txt_uname.setBounds(123, 137, 147, 20);
add(txt_uname);
txt_uname.setColumns(10);
JLabel lblNewLabel_2_1 = new JLabel("Password");
lblNewLabel_2_1.setForeground(Color.WHITE);
lblNewLabel_2_1.setFont(new Font("Calibri", Font.BOLD, 14));
lblNewLabel_2_1.setBounds(35, 188, 74, 14);
add(lblNewLabel_2_1);
txt_pass = new JTextField();
txt_pass.setColumns(10);
txt_pass.setBounds(123, 183, 147, 20);
add(txt_pass);
JButton btn_login = new JButton("Login");
btn_login.setBounds(123, 229, 89, 23);
add(btn_login);
}
}
Here is the visual representation: