display a new jpanel in an applet
God applets just boggle my mind, but anyways. I've created a class which extends JApplet and calls a class which extends JPanel. I have a button on this JPanel class and when it is clicked, I want it to display a different JPanel.
I'm not sure how to do this.
Here's my JApplet class
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* NewJApplet.java
*
* Created on Oct 16, 2009, 6:52:11 PM
*/
package mainPackage;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
/**
*
* @author Administrator
*/
public class NewJApplet extends javax.swing.JApplet {
/** Initializes the applet NewJApplet */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
Language lan = new Language();
setContentPane(lan);
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** This method is called from within the init() method 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() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 298, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 297, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}
and it calls this JPanel class
Code:
/*
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Language.java
*
* Created on Oct 17, 2009, 2:40:54 PM
*/
package mainPackage;
/**
*
* @author Administrator
*/
public class Language extends javax.swing.JPanel {
/** Creates new form Language */
public Language() {
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() {
jLabel1 = new javax.swing.JLabel();
englishButton = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel1.setText("Please select a language");
englishButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
englishButton.setText("English");
englishButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
englishButtonActionPerformed(evt);
}
});
jButton2.setText("jButton2");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(englishButton)
.addComponent(jButton2))
.addContainerGap(225, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(englishButton)
.addGap(58, 58, 58)
.addComponent(jButton2)
.addContainerGap(148, Short.MAX_VALUE))
);
}// </editor-fold>
private void englishButtonActionPerformed(java.awt.event.ActionEvent evt) {
EnterSocial social = new EnterSocial();
setContentPane(social);
}
// Variables declaration - do not modify
private javax.swing.JButton englishButton;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
and when the englishButton is clicked, I want it to display this JPanel class
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* EnterSocial.java
*
* Created on Oct 17, 2009, 2:29:39 PM
*/
package mainPackage;
/**
*
* @author Administrator
*/
public class EnterSocial extends javax.swing.JPanel {
/** Creates new form EnterSocial */
public EnterSocial() {
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() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel1.setText("jLabel1");
jTextField1.setText("jTextField1");
jButton1.setText("jButton1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)))
.addContainerGap(240, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addContainerGap(234, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
In the second piece of code, where it says setContentPane(social); gives an error. Anybody know how to do this?