I have made two files shown in its entirety to run the program, just copy and paste it into a project and then run it. Preferabbly it will be done in Netbeans.
LoginPage.java
which is the "main" page. When the application starts the user sees this.RegisterPage.java
which is a different page, so another card.The LoginPage.java
is what you see when the application first starts. This is the entire code for it:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package com.mycompany.dcoms_mre;
import java.awt.CardLayout;
/**
*
* @author star
*/
public class LoginPage extends javax.swing.JFrame {
/**
* Creates new form LoginRegisterScreen
*/
public LoginPage() {
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
// <editor-fold defaultstate="collapsed" desc="Generated
// <editor-fold defaultstate="collapsed" desc="Generated
// <editor-fold defaultstate="collapsed" desc="Generated
// <editor-fold defaultstate="collapsed" desc="Generated
// Code">//GEN-BEGIN:initComponents
private void initComponents() {
mainPanel = new javax.swing.JPanel();
LoginPanel = new javax.swing.JPanel();
buttonPanel = new javax.swing.JPanel();
registerButton = new javax.swing.JButton();
RegisterPanel = new com.mycompany.dcoms_mre.RegisterPage();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMaximumSize(new java.awt.Dimension(500, 500));
setName("mainWindow"); // NOI18N
getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));
mainPanel.setLayout(new java.awt.CardLayout());
LoginPanel.setMaximumSize(new java.awt.Dimension(500, 500));
LoginPanel.setLayout(new java.awt.GridLayout(3, 0));
buttonPanel.setBorder(new javax.swing.border.MatteBorder(null));
buttonPanel.setLayout(new java.awt.GridLayout(1, 0));
registerButton.setText("Register");
registerButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
registerButtonActionPerformed(evt);
}
});
buttonPanel.add(registerButton);
LoginPanel.add(buttonPanel);
mainPanel.add(LoginPanel, "login panel");
mainPanel.add(RegisterPanel, "register panel");
getContentPane().add(mainPanel);
pack();
}// </editor-fold>//GEN-END:initComponents
private void registerButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_registerButtonActionPerformed
// TODO add your handling code here:
CardLayout card = (CardLayout) mainPanel.getLayout();
card.show(mainPanel, "register panel");
}// GEN-LAST:event_registerButtonActionPerformed
/**
* @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(LoginPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(LoginPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(LoginPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(LoginPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
// </editor-fold>
// </editor-fold>
// </editor-fold>
// </editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LoginPage().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel LoginPanel;
private com.mycompany.dcoms_mre.RegisterPage RegisterPanel;
private javax.swing.JPanel buttonPanel;
public javax.swing.JPanel mainPanel;
private javax.swing.JButton registerButton;
// End of variables declaration//GEN-END:variables
}
By clicking on the "register" button, the page changes and it shows a different page on RegisterPage.java
this is also the full code:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JPanel.java to edit this template
*/
package com.mycompany.dcoms_mre;
import java.awt.CardLayout;
import javax.swing.JPanel;
/**
*
* @author star
*/
public class RegisterPage extends javax.swing.JPanel {
JPanel cardPanel;
CardLayout cardLayout;
/**
* Creates new form RegisterPage
*/
public RegisterPage() {
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">//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
backButton = new javax.swing.JButton();
setLayout(new java.awt.GridBagLayout());
backButton.setText("Back");
backButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
backButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 6;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
add(backButton, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
// TODO add your handling code here:
// cardLayout.show(cardPanel, "login panel");
}//GEN-LAST:event_backButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton backButton;
// End of variables declaration//GEN-END:variables
}
I can go from LoginPage
-> RegisterPage
but not the other way around. The way I'm trying right now is to
LoginPage
card and the panel which holds all the cards available in RegisterPage
and I'll do this by making the RegisterPage
constructor take it in as an argument. private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
// TODO add your handling code here:
cardLayout.show(cardPanel, "login panel");
}//GEN-LAST:event_backButtonActionPerformed
So, I'd need to use the CardLayout
object that was created in LoginPage.java
and pass it into RegisterPage
's constructor and also pass in the LoginPanel
which is the UI or a card. Problem is I can't do any of that.
This entire section is uneditable. I can make minor edits but not the ones I want through this dialogue.
This is part of the "initComponent" function and is way too big to put into a screenshot but the entire thing cannot be edited.
I also ran across this answer but this is the important part:
This is not an issue if you simply use a separate JPanel as the 'card' container plus you get the added benefit of using the JFrame BorderLayout should you wish to add navigation buttons, say in the BorderLayout.SOUTH location.
I already have a separate JPanel as the card container. As you can see here.
As suggested by @camickr I managed to get a solution, but I did things a little differently.
Container mainPanel = this.getParent();
CardLayout card = (CardLayout) mainPanel.getLayout();
card.show(mainPanel, "login panel");