i want use CardLayout in my JFrame form to swap JPanel this is the code of JFrame form:
public class Login extends javax.swing.JFrame {
/**
* Creates new form Login
*/
public Login() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
First JPanel to load when JFrame launch is:
public class LoginPanel extends javax.swing.JPanel {
private Font original;
/**
* Creates new form LoginPanel
*/
public LoginPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Email = new javax.swing.JLabel();
emailField = new javax.swing.JTextField();
login = new javax.swing.JButton();
password = new javax.swing.JLabel();
passwordField = new javax.swing.JPasswordField();
register = new javax.swing.JLabel();
background = new javax.swing.JLabel();
setLayout(null);
Email.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
Email.setText("Email address");
add(Email);
Email.setBounds(155, 115, 90, 14);
emailField.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
emailFieldMouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
emailFieldMouseExited(evt);
}
});
add(emailField);
emailField.setBounds(115, 135, 170, 20);
login.setText("Login");
add(login);
login.setBounds(156, 240, 90, 23);
password.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
password.setText("Password");
add(password);
password.setBounds(155, 170, 90, 14);
add(passwordField);
passwordField.setBounds(115, 191, 170, 20);
register.setForeground(new java.awt.Color(0, 0, 255));
register.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
register.setText("Don't have an account? Register!");
register.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
registerMouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
registerMouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
registerMouseExited(evt);
}
});
add(register);
register.setBounds(115, 220, 170, 14);
background.setIcon(new javax.swing.ImageIcon("C:\\Users\\Matteo\\Desktop\\Netbeans Project\\Social_Network\\background.png")); // NOI18N
background.setText("jLabel1");
add(background);
background.setBounds(0, 0, 400, 300);
}// </editor-fold>
private void registerMouseClicked(java.awt.event.MouseEvent evt) {
}
private void registerMouseEntered(java.awt.event.MouseEvent evt) {
original = register.getFont();
Map attributes = original.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
register.setFont(original.deriveFont(attributes));
Cursor cursor = Cursor.getDefaultCursor();
cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
setCursor(cursor);
}
private void registerMouseExited(java.awt.event.MouseEvent evt) {
register.setFont(original);
Cursor cursor = Cursor.getDefaultCursor();
setCursor(cursor);
}
private void emailFieldMouseEntered(java.awt.event.MouseEvent evt) {
Cursor cursor = Cursor.getDefaultCursor();
cursor = Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR);
setCursor(cursor);
}
private void emailFieldMouseExited(java.awt.event.MouseEvent evt) {
Cursor cursor = Cursor.getDefaultCursor();
setCursor(cursor);
}
// Variables declaration - do not modify
private javax.swing.JLabel Email;
private javax.swing.JLabel background;
private javax.swing.JTextField emailField;
private javax.swing.JButton login;
private javax.swing.JLabel password;
private javax.swing.JPasswordField passwordField;
private javax.swing.JLabel register;
// End of variables declaration
}
Second JPanel to swap when button clicked:
public class RegisterPanel extends javax.swing.JPanel {
/**
* Creates new form RegisterPanel
*/
public RegisterPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
register = new javax.swing.JButton();
cancel = new javax.swing.JButton();
Name = new javax.swing.JLabel();
LastName = new javax.swing.JLabel();
Email = new javax.swing.JLabel();
Password = new javax.swing.JLabel();
icon = new javax.swing.JLabel();
jPasswordField2 = new javax.swing.JPasswordField();
repeatPassword = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
setToolTipText("");
setPreferredSize(new java.awt.Dimension(400, 300));
setLayout(null);
add(jTextField1);
jTextField1.setBounds(110, 60, 240, 20);
add(jTextField2);
jTextField2.setBounds(110, 100, 240, 20);
add(jTextField4);
jTextField4.setBounds(110, 140, 240, 20);
add(jPasswordField1);
jPasswordField1.setBounds(110, 180, 240, 20);
register.setIcon(new javax.swing.ImageIcon("C:\\Users\\Matteo\\Desktop\\Netbeans Project\\Social_Network\\cancel.png")); // NOI18N
register.setText("Cancel");
register.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
registerMouseClicked(evt);
}
});
add(register);
register.setBounds(240, 260, 90, 25);
cancel.setIcon(new javax.swing.ImageIcon("C:\\Users\\Matteo\\Desktop\\Netbeans Project\\Social_Network\\confirm.png")); // NOI18N
cancel.setText("Register");
add(cancel);
cancel.setBounds(130, 260, 93, 25);
Name.setText("Name");
add(Name);
Name.setBounds(20, 60, 27, 14);
LastName.setText("Last name");
add(LastName);
LastName.setBounds(20, 100, 60, 14);
Email.setText("Email address");
add(Email);
Email.setBounds(20, 140, 70, 14);
Password.setText("Password");
add(Password);
Password.setBounds(20, 180, 50, 14);
icon.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
icon.setIcon(new javax.swing.ImageIcon("C:\\Users\\Matteo\\Desktop\\Netbeans Project\\Social_Network\\addUser.png")); // NOI18N
icon.setText(" Registration user");
add(icon);
icon.setBounds(20, 10, 180, 40);
add(jPasswordField2);
jPasswordField2.setBounds(110, 220, 240, 20);
repeatPassword.setText("Repeat password");
add(repeatPassword);
repeatPassword.setBounds(20, 220, 90, 14);
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Matteo\\Desktop\\Netbeans Project\\Social_Network\\background.png")); // NOI18N
jLabel1.setText("jLabel1");
add(jLabel1);
jLabel1.setBounds(0, 0, 400, 300);
}// </editor-fold>
private void registerMouseClicked(java.awt.event.MouseEvent evt) {
}
// Variables declaration - do not modify
private javax.swing.JLabel Email;
private javax.swing.JLabel LastName;
private javax.swing.JLabel Name;
private javax.swing.JLabel Password;
private javax.swing.JButton cancel;
private javax.swing.JLabel icon;
private javax.swing.JLabel jLabel1;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JPasswordField jPasswordField2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField4;
private javax.swing.JButton register;
private javax.swing.JLabel repeatPassword;
// End of variables declaration
}
How i need to write to swap JPanel when i click button?
You need to access the CardLayout
instance and use the show(...)
of the layout, add pass it the name of the panel you want to show. This is all clearly stated in How to Use CardLayout.
Also see How to Use CardLayout with Netbeans GUI Builder for
Also see this answer for
UPDATE
If you want to call the CardLayout
from inside your panels, you can create an interface to implement with a method to show. Have the class with the CardLayout implement it, and pass the instance of the frame to the panel Something like
public interface Switchable {
void show(String card);
}
public class Login extends JFrame implements Switchable {
LoginPanel panel = new LoginPanel(this);
@Override
public void show(String card) {
CardLayout layout = (CardLayout)getLayout();
layout.show(card);
}
}
public class LoginPanel extends JPanel {
private Switchable switcher;
public LoginPanel(Switchable switcher) {
this.switcher = switcher;
}
private void buttonActionPerformed(ActionEvent e) {
switcher.show(...);
}
}