# 6
I've decided to add some code for an example. This code is for the three classes I have described but I didn't put any code into the button yet that would close the current panel and load a new one in its place. Please let me know if you need more clarification.
Code for MainFrame:
package MultiLoaderView;
import javax.swing.*;
import java.awt.*;
/**
*
* @author Blear
*/
public class MainFrame extends javax.swing.JFrame {
/** Creates new form MainFrame */
public MainFrame() {
initComponents();
LoadNewPanel(mySecondClass);
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
LoaderPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
LoaderPanel.setBackground(new java.awt.Color(255, 255, 204));
javax.swing.GroupLayout LoaderPanelLayout = new javax.swing.GroupLayout(LoaderPanel);
LoaderPanel.setLayout(LoaderPanelLayout);
LoaderPanelLayout.setHorizontalGroup(
LoaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 792, Short.MAX_VALUE)
);
LoaderPanelLayout.setVerticalGroup(
LoaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 479, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(LoaderPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(LoaderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(87, Short.MAX_VALUE))
);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-800)/2, (screenSize.height-600)/2, 800, 600);
}// </editor-fold>
public void LoadNewPanel(Component panel2Load)
{
javax.swing.GroupLayout LoaderPanelLayout = new javax.swing.GroupLayout(LoaderPanel);
LoaderPanel.setLayout(LoaderPanelLayout);
LoaderPanelLayout.setHorizontalGroup(
LoaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 792, Short.MAX_VALUE)
.addGroup(LoaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(LoaderPanelLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(panel2Load, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
);
LoaderPanelLayout.setVerticalGroup(
LoaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 479, Short.MAX_VALUE)
.addGroup(LoaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(LoaderPanelLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(panel2Load, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel LoaderPanel;
// End of variables declaration
//Declare instance of SecondClass and ThirdClass
SecondClass mySecondClass = new SecondClass();
ThirdClass myThirdClass = new ThirdClass();
}
Code for SecondClass (second panel)
package MultiLoaderView;
/**
*
* @author Blear
*/
public class SecondClass extends javax.swing.JPanel {
/** Creates new form SecondClass */
public SecondClass() {
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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
NextPanelButton = new javax.swing.JButton();
DescriptionLabel = new javax.swing.JLabel();
setBackground(new java.awt.Color(204, 255, 204));
NextPanelButton.setText("Next Panel");
NextPanelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NextPanelButtonActionPerformed(evt);
}
});
DescriptionLabel.setText("Click to close this panel and open the other");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(93, 93, 93)
.addComponent(DescriptionLabel)
.addContainerGap(101, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(NextPanelButton)
.addGap(0, 0, Short.MAX_VALUE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(103, 103, 103)
.addComponent(DescriptionLabel)
.addContainerGap(183, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(NextPanelButton)
.addGap(0, 0, Short.MAX_VALUE)))
);
}// </editor-fold>
private void NextPanelButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JLabel DescriptionLabel;
private javax.swing.JButton NextPanelButton;
// End of variables declaration
}
Code for ThirdClass:
package MultiLoaderView;
/**
*
* @author Blear
*/
public class ThirdClass extends javax.swing.JPanel {
/** Creates new form ThirdClass */
public ThirdClass() {
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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setBackground(new java.awt.Color(204, 0, 51));
jLabel1.setText("This is the third class/panel in the project");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(99, 99, 99)
.addComponent(jLabel1)
.addContainerGap(105, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(jLabel1)
.addContainerGap(226, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
Bleara at 2007-7-12 19:34:04 >

# 12
An ineligant solution:
interface Fooable
{
void BtnPushed(Object foo); // The use of Object here is bad form
}
import javax.swing.*;
import java.awt.*;
import java.util.EventListener;
class MainFrame extends javax.swing.JFrame implements Fooable
{
public MainFrame() {
initComponents();
LoadNewPanel(mySecondClass);
}
protected void secondClassBtnPushed()
{
mySecondClass.setVisible(false);
LoadNewPanel(myThirdClass);
this.invalidate();
this.repaint();
}
private void initComponents() {
// no change here
}
public void LoadNewPanel(Component panel2Load)
{
// no change here
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel LoaderPanel;
// End of variables declaration
//Declare instance of SecondClass and ThirdClass
SecondClass mySecondClass = new SecondClass(this); //!!
ThirdClass myThirdClass = new ThirdClass(this); //!!
public void BtnPushed(Object foo) // The use of Object here is bad form
{
if (foo.equals(mySecondClass))
{
mySecondClass.setVisible(false);
LoadNewPanel(myThirdClass);
this.invalidate();
this.repaint();
}
else if (foo.equals(myThirdClass))
{
}
}
}
class SecondClass extends javax.swing.JPanel
{
Fooable mf; // !!
public SecondClass(Fooable mf) {
this.mf = mf; // !!
initComponents();
}
private void initComponents()
{
// no change here
}// </editor-fold>
private void NextPanelButtonActionPerformed(java.awt.event.ActionEvent evt)
{
mf.BtnPushed(this);
}
// Variables declaration - do not modify
private javax.swing.JLabel DescriptionLabel;
private javax.swing.JButton NextPanelButton;
// End of variables declaration
}
class ThirdClass extends javax.swing.JPanel
{
Fooable mf;
public ThirdClass(Fooable mf)
{
this.mf = mf;
initComponents();
}
private void initComponents()
{
// no change here
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}