Article View: aus.computers.java
Article #971Re: Please Help
From: moz
Date: Mon, 28 Feb 2000 00:00
Date: Mon, 28 Feb 2000 00:00
90 lines
3000 bytes
3000 bytes
fiveten@ptd.net wrote:
> Hi, first message here, i have a huge problem, i'v had this code for
> days and cant get the ItemListener part to work, could you all help me
> out a bit ??? i'm just learning the lang and cant find any help in the
> API, thanks a ton. (ps> i'm coding in AWT jdk1.1)
>
> thanks again,
> 510
>
> ps> if its not too much could you respond to email: fiveten@ptd.net
> since i can only get email at home, thanks
> ///////////////////////////////////////////////////////////////////////////////////////////////////////
>
Ok you seem to have the event handling code a bit mixed up. This is how it should look:
>
> import java.awt.event.*;
> import java.awt.*;
> import java.*;
> import java.applet.Applet;
>
> public class Radios extends Applet implements ItemListener
> {
>
> static String str1 = "String One";
> static String str2 = "String Two";
> static String na = "Previous Option N/A";
>
> TextField text = new TextField("Null");
> Checkbox radio1 = new Checkbox();
> Checkbox radio2 = new Checkbox();
> Checkbox box1 = new Checkbox("Toggle");
> Panel p1;
> // ItemListener I = new ItemListener();
>
> public Radios()
> {
> // Create New
>
> CheckboxGroup group = new CheckboxGroup();
> radio1 = new Checkbox(str1, group, false);
> radio2 = new Checkbox(str2, group, false);
>
> radio1.addItemListener(this);
> radio2.addItemListener(this);
> box1.addItemListener(this);
>
> //sets up the window and Layout
> p1 = new Panel();
> p1.setLayout(new GridLayout(8,0));
> p1.add(radio1);
> p1.add(radio2);
> p1.add(box1);
> p1.add(text);
> add(p1);
>
> }
>
> public void itemStateChanged(ItemEvent e){
> if (e.getLabel() == str1) //Some Swing pieces in here, gotta
> text.setText(str1); //convert them to awt simillar
> if (e.getLabel() == str2)
> text.setText(str2);
> if (box1.getState()==true) //true == on {
> radio2.setState(false); //disable radio2
> if (text.getText().equals(str2)) //If text ='d st2, now its N/A
> text.setText(na);
> }
> else
> {
> radio2.setState(true); //box off, enable radio2,
> if (text.getText().equals(na)) //if text was N/A, make it
> text.setText(str2); //Previous Option (Str2)
> }
>
> }
>
> }
I haven't tried to compile it, but that above code should work.
Moz
Technical Details
Message-ID:
<38B9A185.DF261B67@iinet.net.au>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<38B94ECA.25870E24@ptd.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,6d08219f0b3c18be,start X-Google-Attributes: gid10d6db,public From: moz <moz@iinet.net.au> Subject: Re: Please Help Date: 2000/02/28 Message-ID: <38B9A185.DF261B67@iinet.net.au> X-Deja-AN: 590603646 Content-Transfer-Encoding: 7bit References: <38B94ECA.25870E24@ptd.net> Content-Type: text/plain; charset=us-ascii X-Trace: news.iinet.net.au 951689275 30562 mozzy@203.62.155.111 MIME-Version: 1.0 Newsgroups: aus.computers.java