When I load a "Test" applet, the functions somewhat dissapear?!
What do I mean by functions ladies and gentleman?
Well, I add ItemListeners to some checkboxes, and those checkboxes
are supposed to fire a process that will update some pricess for me,
depending on a specific category represented by the checkbox.
What happens is that if I use checkboxItem.getState(); function, it will
allways return the value false. I do not know why. The proper ActionEvent
is fired, and goes to the proper Object which called the event, but
it seems that the state of the checkbox is dead, as it shows "false" all the
time.
So, my plea to the mega programmers, or to the discerning, is "How do I
use Events within an Applet, with check boxes?"
I have included the code of the applet, in case this is necessary.
Thank you all so much for your attention.
Renato.
========================================================
The code is ready to run. The only thing needed done is to create an HTML with
the following
tags, and making sure that both the class file and the html are in the same
directory.
<HTML>
<BODY>
<HEADER > HELLO, APPLET BELLOW <p> </HEADER>
<applet code="GUIFrame.class" width=300 height=200></applet>
</BODY>
</HTML>
========================================================
// GUIFrame.java
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.applet.Applet;
public class GUIFrame extends Applet {
Panel inputpanel,checkpanel,outputpanel;
Label prompt;
TextField input;
CheckboxGroup category;
Checkbox wholesale;
Checkbox retail;
Checkbox customer;
Checkbox showcost;
Choice productlist;
TextArea output;
String theoutput;
String name;
boolean show;
String[] product;
double[] cp;
double sp;
int selected;
public GUIFrame()
{
//addWindowListener(new windowListener()); //not allowed in applet,
uncomment for non- applet load
/* anonymous inner class
addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent theEvent)
{
System.exit(0);
}
}
);
*/
setSize(350,200);
//setTitle("Price Quotes"); //not allowed in applet, uncomment for
non-applet load
init();
initEventHandling();
}
public void init()
{
prompt=new Label("Customer:");
input=new TextField(20);
input.setText("Default");
output=new TextArea("",10,25,0);
theoutput=new String();
category=new CheckboxGroup();
wholesale=new Checkbox("Wholesale",category,false);
retail=new Checkbox("Retail",category,false);
customer=new Checkbox("Client",category,false);
showcost=new Checkbox("Show Cost",false);
product=new String[4];
product[0]="CPU";
product[1]="CDROM";
product[2]="RAM";
product[3]="HDisk";
cp=new double[4];
cp[0]=4256.87;
cp[1]=742.93;
cp[2]=798.45;
cp[3]=1123.47;
sp=0;
productlist=new Choice();
for(int i=0;i<4;i++)
productlist.addItem(product[i]);
setLayout(new BorderLayout());
inputpanel=new Panel();
checkpanel=new Panel();
outputpanel=new Panel();
inputpanel.setLayout(new GridLayout(1,3));
checkpanel.setLayout(new GridLayout(6,1));
outputpanel.setLayout(new GridLayout(1,1));
inputpanel.add(prompt);
inputpanel.add(input);
checkpanel.add(wholesale);
checkpanel.add(retail);
checkpanel.add(customer);
checkpanel.add(showcost);
checkpanel.add(productlist);
outputpanel.add(output);
add(inputpanel,"North");
add(checkpanel,"West");
add(outputpanel,"Center");
productlist.select(2);
show=false;
initEventHandling();
validate();
updateDisplay();
}
public void updateDisplay()
{
theoutput="Customer : "+name+
"\nSelling Price: R "+sp;
if (true)
theoutput+="\nCost Price : R "+cp[selected];
output.setText(theoutput + " :-) ");
repaint();
System.out.println("update display:" + theoutput + "show: " + show + "\n");
}
public void initEventHandling()
{
System.out.println("Within EvenHandling");
input.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ System.out.println("input");
name=input.getText();
updateDisplay();
}
}
);
showcost.addItemListener
(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{ System.out.println("showcost");
show=showcost.getState();
updateDisplay();
}
}
);
productlist.addItemListener
(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{ System.out.println("productlist");
selected=productlist.getSelectedIndex();
updateDisplay();
}
}
);
wholesale.addItemListener
(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
System.out.println("Wholesale State: " + wholesale.getState());
if (wholesale.getState())
{
System.out.println("Wholesale");
sp=cp[selected]*1.2;
updateDisplay();
}
}
}
);
retail.addItemListener
(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
System.out.println("retail State: " + retail.getState());
if (retail.getState()==false)
{
System.out.println("retail");
sp=cp[selected]*1.5;
updateDisplay();
output.append("Hi there"); //not even this shows within the
APPLET!
}
}
}
);
customer.addItemListener
(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
System.out.println("Customer State: " + customer.getState());
if (customer.getState())
{
System.out.println("customer");
sp=cp[selected]*2;
updateDisplay();
}
}
}
);
}
public static void main(String[] args)
{ //Comment out if you need to run it without the applet.
// GUIFrame theGUIFrame=new GUIFrame();
//theGUIFrame.show();
}
}
class windowListener extends WindowAdapter
{
public void windowClosing(WindowEvent theEvent)
{
System.exit(0);
}
}