Article View: borland.public.jbuilder.java.swing
Article #13006Multi-panel Swing Applet
From: Yong Chee Keong
Date: Tue, 06 Oct 1998 00:00
Date: Tue, 06 Oct 1998 00:00
364 lines
8387 bytes
8387 bytes
Hi there,
I am trying to have a swing based applet that have multiple panels.
In my sample, the first panel have a button that will call the parent
JApplet
to switch to the second panel.
I also want to be able to switch to the corresponding panel based on
what
the user select from the menu.
When I run the application under appletviewer, I managed to change
from the first panel to the second panel.
But after that, it doesn't work any more.
Enclosed along is the full source code of my application in 3 java
files.
Any help is highly appreciated.
Thanks,
Yong Chee Keong
/* ***************************
Source code for SampleApplet
*****************************
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.sun.java.swing.*;
//import com.sun.java.swing.UIManager;
public class SampleApplet extends JApplet implements ActionListener {
boolean isStandalone = false;
panel1 first;
panel2 second;
Frame browserFrame;
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JPanel activepane;
//Labels to identify the text fields
public SampleApplet() {
}
//Initialize the applet
public void init() {
Component comp = this;
while (!(comp instanceof Frame) )
comp = comp.getParent();
browserFrame = (Frame) comp;
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
menuBar = new JMenuBar();
setJMenuBar(menuBar);
menu = new JMenu("A Menu");
menuBar.add(menu);
menuItem = new JMenuItem("Item One");
menuItem.addActionListener(this);
menu.add(menuItem);
menuItem = new JMenuItem("Item Two");
menuItem.addActionListener(this);
menu.add(menuItem);
menu = new JMenu("Another Menu");
menuBar.add(menu);
first = new panel1(this);
second = new panel2(this);
activepane = first;
this.getContentPane().add(activepane);
browserFrame.pack();
this.setSize(400,300);
}
//Start the applet
public void start() {
}
//Stop the applet
public void stop() {
}
//Destroy the applet
public void destroy() {
}
public void actionPerformed (ActionEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
String s = source.getText();
System.out.println("Action Event is " + s);
if (s == "Item One") {
switchpanel("first");
}
else if (s == "Item Two") {
switchpanel("second");
}
}
public void switchpanel(String args) {
this.getContentPane().remove(activepane);
System.out.println("Args is " + args);
if (args == "second") {
activepane = second;
}
else if (args == "first") {
activepane = first;
}
this.getContentPane().add(activepane);
browserFrame.pack();
this.setSize(400,300);
}
}
/* **********************
Source code for panel2
************************
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.sun.java.swing.*;
//import com.sun.java.swing.UIManager;
public class panel1 extends JPanel {
//Labels to identify the text fields
private JLabel amountLabel;
private JLabel rateLabel;
private JLabel numPeriodsLabel;
private JLabel paymentLabel;
private JLabel dateLabel;
private JLabel icLabel;
//Strings for the labels
private static String amountString = "Loan Amount: ";
private static String rateString = "APR: ";
private static String numPeriodsString = "Years: ";
private static String paymentString = "Monthly Payment: ";
private static String dateString = "Date: ";
private static String icString = "I.C. No: ";
SampleApplet theapplet = null;
JButton bc = new JButton("Next");
//Construct the applet
public panel1() {
}
public panel1(SampleApplet applet) {
theapplet = applet;
init();
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setLayout(new GridLayout(0,2));
amountLabel = new JLabel(amountString);
rateLabel = new JLabel(rateString);
numPeriodsLabel = new JLabel(numPeriodsString);
paymentLabel = new JLabel(paymentString);
dateLabel = new JLabel(dateString);
icLabel = new JLabel(icString);
this.add(amountLabel);
this.add(rateLabel);
this.add(numPeriodsLabel);
this.add(dateLabel);
this.add(icLabel);
bc.addActionListener(new bc_adapter(this));
this.add(bc);
}
//Start the applet
public void start() {
}
//Stop the applet
public void stop() {
}
//Destroy the applet
public void destroy() {
}
void bc_actionPerformed(ActionEvent e) {
if (theapplet != null)
theapplet.switchpanel("second");
}
class bc_adapter implements java.awt.event.ActionListener {
panel1 adaptee;
bc_adapter(panel1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.bc_actionPerformed(e);
}
}
}
/* **********************
Source code for panel2
************************
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.sun.java.swing.*;
//import com.sun.java.swing.UIManager;
public class panel2 extends JPanel {
//Labels to identify the text fields
private JLabel amountLabel;
private JLabel rateLabel;
private JLabel numPeriodsLabel;
private JLabel paymentLabel;
private JLabel dateLabel;
private JLabel icLabel;
//Strings for the labels
private static String amountString = "Name: ";
private static String rateString = "Computer Model: ";
private static String numPeriodsString = "Salary: ";
private static String paymentString = "Status: ";
private static String dateString = "Position: ";
private static String icString = "Info: ";
SampleApplet theapplet = null;
JButton bc = new JButton("Back");
//Construct the applet
public panel2() {
}
public panel2(SampleApplet applet) {
theapplet = applet;
init();
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setLayout(new GridLayout(0,2));
amountLabel = new JLabel(amountString);
rateLabel = new JLabel(rateString);
numPeriodsLabel = new JLabel(numPeriodsString);
paymentLabel = new JLabel(paymentString);
dateLabel = new JLabel(dateString);
icLabel = new JLabel(icString);
this.add(amountLabel);
this.add(rateLabel);
this.add(numPeriodsLabel);
this.add(dateLabel);
this.add(icLabel);
bc.addActionListener(new bc_adapter(this));
this.add(bc);
}
//Start the applet
public void start() {
}
//Stop the applet
public void stop() {
}
//Destroy the applet
public void destroy() {
}
void bc_actionPerformed(ActionEvent e) {
if (theapplet != null)
theapplet.switchpanel("first");
}
class bc_adapter implements java.awt.event.ActionListener {
panel2 adaptee;
bc_adapter(panel2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.bc_actionPerformed(e);
}
}
}
Technical Details
Message-ID:
<3619D83C.E69B910@gsi.demc.com.my>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,4064b71d56346a37,start X-Google-Attributes: gid111d45,public From: Yong Chee Keong <yong@gsi.demc.com.my> Subject: Multi-panel Swing Applet Date: 1998/10/06 Message-ID: <3619D83C.E69B910@gsi.demc.com.my> X-Deja-AN: 398203131 Content-Transfer-Encoding: 7bit Organization: Another Netscape Collabra Server User Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing