How to display textfield,checkbox, checkboxgroup, choice list and a list in an applet?

Hello,

I'm having some difficulty with displaying an applet that shows 2 textfields, a checkbox, 2 checkboxgroups, a choicelist and a list.  I have written the program below but can someone please advise me where I am going wrong as I'm having trouble with the last bit of the program where I have to insert the actionPerformed and itemStateChanged with the paint( ) method.  
-------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class FormDemo extends Applet implements ItemListener, ActionListener
{
String msg=" ";
Textfield name,pass;
CheckboxGroup male,female;
Checkbox oracle,java,c, basic;
Choice newspapers,friends,others;
List lumpsum,instalment,first, second;

public void init()
{
Label namep=new Label("Name: ", Label.RIGHT);
Label passp=new Label("Password", Label.RIGHT);
name=new TextField(12);
pass=new TextField(8);
pass.EchoChar('*');
add(namep);
add(name);
add(passp);
add(pass);

cbg=new CheckboxGroup();
male=new Checkbox("male", cbg, true);
female=new Checkbox("female", cbg, false);
add(male);
add(female);

oracle=new Checkbox("Oracle", null, true);
java=new Checkbox("Java");
c=new Checkbox("c++");
basic=new Checkbox("basic");
add(oracle);
add(java);
add(c++);
add(basic);

os=new Choice();
os.add("Newspapers");
os.add("Friends");
os.add("Others");
add(os);

os=new List(4,true);
os.add("lumpsum");
os.add("instalment");
os.add("first");
os.add("second");
add(os);

name.addActionListener(this);
pass.addActionListener(this);
male.addItemListner(this);
female.addItemListener(this);
oracle.addItemListener(this);
java.addItemListener(this);
c++.addItemListener(this);
basic.addItemListener(this);
os.addItemListener(this);
os.addItemListener(this);
}

--HERE I'M HAVING TROUBLE WITH THE DISPLAY OF THE ITEMS--

public void actionPerformed(ActionEvent ae), itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString("Name: "+name.getText(),6,60);
g.drawString("Password: "+pass.getText(),6,100);
}


msg="Gender";
msg+=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,6,100);



msg="Courses";
g.drawString(msg,6,80);
msg="Oracle: "+oracle.getState();
g.drawString(msg,6,100);
msg="Java: "+java.getState();
g.drawString(msg,6,120);
msg="C++: "+c.getState();
g.drawString(msg,6,140);
msg="Basic: "+basic.getState();
g.drawString(msg,6,160);




msg="Way of Source";
msg+=os.getSelectedItem();
g.drawString(msg,6,120);





int idx[];
msg="Payment";
idx=os.getSelectedIndexes();
for(int i=0;i<idx.length;i++)
msg+=os.getItem(idx[i])+" ";
g.drawString(msg,6,120);
}
}



nafisaAsked:
Who is Participating?
 
kamaljeetsCommented:
Try compiling this code,
the compiler is giving errors as u r defining the paint method twice.

Note : repaint() , internally calls the paint method.

hope this will do.

Kamal

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class FormDemo1 extends Applet
{
String msg=" ";
Textfield name,pass;
CheckboxGroup male,female;
Checkbox oracle,java,c, basic;
Choice newspapers,friends,others;
List lumpsum,instalment,first, second;

public void init()
{
Label namep=new Label("Name: ", Label.RIGHT);
Label passp=new Label("Password", Label.RIGHT);
name=new TextField(12);
pass=new TextField(8);
pass.EchoChar('*');
add(namep);
add(name);
add(passp);
add(pass);

cbg=new CheckboxGroup();
male=new Checkbox("male", cbg, true);
female=new Checkbox("female", cbg, false);
add(male);
add(female);

oracle=new Checkbox("Oracle", null, true);
java=new Checkbox("Java");
c=new Checkbox("c++");
basic=new Checkbox("basic");
add(oracle);
add(java);
add(c++);
add(basic);

os=new Choice();
os.add("Newspapers");
os.add("Friends");
os.add("Others");
add(os);

os=new List(4,true);
os.add("lumpsum");
os.add("instalment");
os.add("first");
os.add("second");
add(os);

name.addActionListener(this);
pass.addActionListener(this);
male.addItemListner(this);
female.addItemListener(this);
oracle.addItemListener(this);
java.addItemListener(this);
c++.addItemListener(this);
basic.addItemListener(this);
os.addItemListener(this);
os.addItemListener(this);
}



public void actionPerformed(ActionEvent ae)
{
repaint();
}

public void  itemStateChanged(ItemEvent ie)
  {
repaint();
}    

public void paint(Graphics g)
{

g.drawString("Name: "+name.getText(),6,60);
g.drawString("Password: "+pass.getText(),6,100);


msg="Gender";
msg+=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,6,100);


msg="Courses";
g.drawString(msg,6,80);
msg="Oracle: "+oracle.getState();
g.drawString(msg,6,100);
msg="Java: "+java.getState();
g.drawString(msg,6,120);
msg="C++: "+c.getState();
g.drawString(msg,6,140);
msg="Basic: "+basic.getState();
g.drawString(msg,6,160);

msg="Way of Source";
msg+=os.getSelectedItem();
g.drawString(msg,6,120);


int idx[];
msg="Payment";
idx=os.getSelectedIndexes();
for(int i=0;i<idx.length;i++)
msg+=os.getItem(idx[i])+" ";
g.drawString(msg,6,120);
}
}


0
 
shahnazaliCommented:
hi,
  write ur both methods separately .like

public void actionPerformed(ActionEvent ae)
   {
               repaint();
   }

public void  itemStateChanged(ItemEvent ie)
    {
               repaint();
     }


 bye.
Shaan
0
 
kamaljeetsCommented:
Hi!
 i am not able to understand how u r able to compile with the following code :


public void actionPerformed(ActionEvent ae), itemStateChanged(ItemEvent ie)
{
repaint();
}


isn't it should be like this

public void actionPerformed(ActionEvent ae)
{
do whatever u need to do here
}

public void itemStateChanged(ItemEvent ie)
{
and here also ,do whatever u need to do here
}


the next thing

where r u capturing the source of the event

like


public void actionPerformed(ActionEvent ae)
{
 String temp = ae.getSource(// here name of the source component)



have a look at the java docs to know which components cause action events or for which u need itemstatechanged.


labels do not respond to actionPerformed(buttons do),

for checkboxes   : itemStateChanged

for textfields(i do not remember, look in docs)


hope this will do -:)

kamal




0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
nafisaAuthor Commented:
hi,

I have tried using the two methods seperately -

public void actionPerformed(ActionEvent ae)
  {
              repaint();
  }

public void  itemStateChanged(ItemEvent ie)
   {
              repaint();
    }


But, I get an error (jdk1.2) which says "Duplicate method declaration: void paint(java.awt.Graphics) void paint(Graphics g).  Can you help me resolve this error please?

[please check my program below]:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class FormDemo1 extends Applet
{
String msg=" ";
Textfield name,pass;
CheckboxGroup male,female;
Checkbox oracle,java,c, basic;
Choice newspapers,friends,others;
List lumpsum,instalment,first, second;

public void init()
{
Label namep=new Label("Name: ", Label.RIGHT);
Label passp=new Label("Password", Label.RIGHT);
name=new TextField(12);
pass=new TextField(8);
pass.EchoChar('*');
add(namep);
add(name);
add(passp);
add(pass);

cbg=new CheckboxGroup();
male=new Checkbox("male", cbg, true);
female=new Checkbox("female", cbg, false);
add(male);
add(female);

oracle=new Checkbox("Oracle", null, true);
java=new Checkbox("Java");
c=new Checkbox("c++");
basic=new Checkbox("basic");
add(oracle);
add(java);
add(c++);
add(basic);

os=new Choice();
os.add("Newspapers");
os.add("Friends");
os.add("Others");
add(os);

os=new List(4,true);
os.add("lumpsum");
os.add("instalment");
os.add("first");
os.add("second");
add(os);

name.addActionListener(this);
pass.addActionListener(this);
male.addItemListner(this);
female.addItemListener(this);
oracle.addItemListener(this);
java.addItemListener(this);
c++.addItemListener(this);
basic.addItemListener(this);
os.addItemListener(this);
os.addItemListener(this);
}



public void actionPerformed(ActionEvent ae)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString("Name: "+name.getText(),6,60);
g.drawString("Password: "+pass.getText(),6,100);
}

public void  itemStateChanged(ItemEvent ie)
   {
repaint();
}    

public void paint(Graphics g)
{

msg="Gender";
msg+=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,6,100);


msg="Courses";
g.drawString(msg,6,80);
msg="Oracle: "+oracle.getState();
g.drawString(msg,6,100);
msg="Java: "+java.getState();
g.drawString(msg,6,120);
msg="C++: "+c.getState();
g.drawString(msg,6,140);
msg="Basic: "+basic.getState();
g.drawString(msg,6,160);

msg="Way of Source";
msg+=os.getSelectedItem();
g.drawString(msg,6,120);


int idx[];
msg="Payment";
idx=os.getSelectedIndexes();
for(int i=0;i<idx.length;i++)
msg+=os.getItem(idx[i])+" ";
g.drawString(msg,6,120);
}
}

0
 
nafisaAuthor Commented:
hi,

I have tried using the two methods seperately -

public void actionPerformed(ActionEvent ae)
  {
              repaint();
  }

public void  itemStateChanged(ItemEvent ie)
   {
              repaint();
    }


But, I get an error (jdk1.2) which says "Duplicate method declaration: void paint(java.awt.Graphics) void paint(Graphics g).  Can you help me resolve this error please?

[please check my program below]:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class FormDemo1 extends Applet
{
String msg=" ";
Textfield name,pass;
CheckboxGroup male,female;
Checkbox oracle,java,c, basic;
Choice newspapers,friends,others;
List lumpsum,instalment,first, second;

public void init()
{
Label namep=new Label("Name: ", Label.RIGHT);
Label passp=new Label("Password", Label.RIGHT);
name=new TextField(12);
pass=new TextField(8);
pass.EchoChar('*');
add(namep);
add(name);
add(passp);
add(pass);

cbg=new CheckboxGroup();
male=new Checkbox("male", cbg, true);
female=new Checkbox("female", cbg, false);
add(male);
add(female);

oracle=new Checkbox("Oracle", null, true);
java=new Checkbox("Java");
c=new Checkbox("c++");
basic=new Checkbox("basic");
add(oracle);
add(java);
add(c++);
add(basic);

os=new Choice();
os.add("Newspapers");
os.add("Friends");
os.add("Others");
add(os);

os=new List(4,true);
os.add("lumpsum");
os.add("instalment");
os.add("first");
os.add("second");
add(os);

name.addActionListener(this);
pass.addActionListener(this);
male.addItemListner(this);
female.addItemListener(this);
oracle.addItemListener(this);
java.addItemListener(this);
c++.addItemListener(this);
basic.addItemListener(this);
os.addItemListener(this);
os.addItemListener(this);
}



public void actionPerformed(ActionEvent ae)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString("Name: "+name.getText(),6,60);
g.drawString("Password: "+pass.getText(),6,100);
}

public void  itemStateChanged(ItemEvent ie)
   {
repaint();
}    

public void paint(Graphics g)
{

msg="Gender";
msg+=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,6,100);


msg="Courses";
g.drawString(msg,6,80);
msg="Oracle: "+oracle.getState();
g.drawString(msg,6,100);
msg="Java: "+java.getState();
g.drawString(msg,6,120);
msg="C++: "+c.getState();
g.drawString(msg,6,140);
msg="Basic: "+basic.getState();
g.drawString(msg,6,160);

msg="Way of Source";
msg+=os.getSelectedItem();
g.drawString(msg,6,120);


int idx[];
msg="Payment";
idx=os.getSelectedIndexes();
for(int i=0;i<idx.length;i++)
msg+=os.getItem(idx[i])+" ";
g.drawString(msg,6,120);
}
}

0
 
nafisaAuthor Commented:
hi,

I have tried using the two methods seperately -

public void actionPerformed(ActionEvent ae)
  {
              repaint();
  }

public void  itemStateChanged(ItemEvent ie)
   {
              repaint();
    }


But, I get an error (jdk1.2) which says "Duplicate method declaration: void paint(java.awt.Graphics) void paint(Graphics g).  Can you help me resolve this error please?

[please check my program below]:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class FormDemo1 extends Applet
{
String msg=" ";
Textfield name,pass;
CheckboxGroup male,female;
Checkbox oracle,java,c, basic;
Choice newspapers,friends,others;
List lumpsum,instalment,first, second;

public void init()
{
Label namep=new Label("Name: ", Label.RIGHT);
Label passp=new Label("Password", Label.RIGHT);
name=new TextField(12);
pass=new TextField(8);
pass.EchoChar('*');
add(namep);
add(name);
add(passp);
add(pass);

cbg=new CheckboxGroup();
male=new Checkbox("male", cbg, true);
female=new Checkbox("female", cbg, false);
add(male);
add(female);

oracle=new Checkbox("Oracle", null, true);
java=new Checkbox("Java");
c=new Checkbox("c++");
basic=new Checkbox("basic");
add(oracle);
add(java);
add(c++);
add(basic);

os=new Choice();
os.add("Newspapers");
os.add("Friends");
os.add("Others");
add(os);

os=new List(4,true);
os.add("lumpsum");
os.add("instalment");
os.add("first");
os.add("second");
add(os);

name.addActionListener(this);
pass.addActionListener(this);
male.addItemListner(this);
female.addItemListener(this);
oracle.addItemListener(this);
java.addItemListener(this);
c++.addItemListener(this);
basic.addItemListener(this);
os.addItemListener(this);
os.addItemListener(this);
}



public void actionPerformed(ActionEvent ae)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString("Name: "+name.getText(),6,60);
g.drawString("Password: "+pass.getText(),6,100);
}

public void  itemStateChanged(ItemEvent ie)
   {
repaint();
}    

public void paint(Graphics g)
{

msg="Gender";
msg+=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,6,100);


msg="Courses";
g.drawString(msg,6,80);
msg="Oracle: "+oracle.getState();
g.drawString(msg,6,100);
msg="Java: "+java.getState();
g.drawString(msg,6,120);
msg="C++: "+c.getState();
g.drawString(msg,6,140);
msg="Basic: "+basic.getState();
g.drawString(msg,6,160);

msg="Way of Source";
msg+=os.getSelectedItem();
g.drawString(msg,6,120);


int idx[];
msg="Payment";
idx=os.getSelectedIndexes();
for(int i=0;i<idx.length;i++)
msg+=os.getItem(idx[i])+" ";
g.drawString(msg,6,120);
}
}

0
 
kamaljeetsCommented:
I still do not understand when these methods  actionPerformed() and itemStateChanged() be called

it appears every time a checkbox is checked or unchecked , repaint will be called, maybe this is wht u want to do.


kamal
0
 
ImrMLCommented:
Nafisa,

What are you doing is really very difficlut to understand. Please look few things:
1. Where are you delaring CheckboxGroup 'cbg'? How you can instanctiating it?
2. How can you instantiate the 'male' object of CheckboxGroup by CheckBox?
3. Same Q. as above for 'female' object.
4. When you have declared Oracle as Checkbox but while instantiating it you are not adding to any group. Why null as below:
oracle=new Checkbox("Oracle", null, true)
5.When you have declared c as checkbox why adding add(c++)?
6. OOOOOOOps many
Anyway, let me know what exact;y you want i'll code for you. But as per your question "I'm having some difficulty with displaying an applet that shows 2 textfields, a checkbox, 2 checkboxgroups, a choicelist and a list."
Sorry Nafisa, you can't display CheckboxGroups?????

KH
ImrML
0
 
nafisaAuthor Commented:
Hi everyone,

Thanks for all the advice and I really appreciate it all.  I have now got the program above working (after lots of trials!) although I need to align all the things properly on the applet which I will do.  However I want your help on this:

I want my scrolling text "Welcome" to be placed on top in the applet and for all the textfields, checkboxes, etc. to be underneath it - my trouble is that I have the scrolling text program but don't know how to insert it in the FormDemo applet program?

Please can you check where I should insert the scrolling text program into the FormDemo applet program so that have the scrolling "Welcome" in the FormDemo applet.
[Here are my 2 programs].  

(1)FormDemo Program:-

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class BoxDemo extends Applet implements ItemListener,ActionListener
{

//checkbox
String msg=" ";
Checkbox Win98, winNT, solaris,mac;

//checkboxgroup
Checkbox one,two,three,four;
CheckboxGroup cbg;

//choicelist
Choice fruit;

//list
List vegetables;

//TextField

TextField name,pass;





public void init()
{

//checkbox
Win98=new Checkbox("Win98");

winNT=new Checkbox("winNT");

solaris=new Checkbox("solaris");

mac=new Checkbox("mac");

//checkboxgroup
cbg=new CheckboxGroup();
one=new Checkbox("one", cbg,true);

two=new Checkbox("two", cbg,false);
three=new Checkbox("three", cbg,false);
four=new Checkbox("four", cbg,false);

//choicelist
fruit=new Choice();
fruit.add("apple");
fruit.add("banana");
fruit.add("grapes");
fruit.add("melon");


//list
vegetables=new List(4,true);
vegetables.add("onion");
vegetables.add("potatoes");
vegetables.add("marrow");
vegetables.add("carrot");

//textfield
Label namep=new Label("Name: ", Label.RIGHT);
Label passp=new Label("Password: ", Label.RIGHT);
name=new TextField(12);
pass=new TextField(8);
pass.setEchoChar('*');













//checkbox
add(Win98);
add(winNT);
add(solaris);
add(mac);

//checkboxgroup
add(one);
add(two);
add(three);
add(four);

//choicelist
add(fruit);

//list
add(vegetables);

//textfield
add(namep);
add(name);
add(passp);
add(pass);












//checkbox
Win98.addItemListener(this);
winNT.addItemListener(this);
solaris.addItemListener(this);
mac.addItemListener(this);

//checkboxgroup
one.addItemListener(this);
two.addItemListener(this);
three.addItemListener(this);
four.addItemListener(this);

//choicelist
fruit.addItemListener(this);

//list
vegetables.addActionListener(this);

//textfield
name.addActionListener(this);
pass.addActionListener(this);


}


public void itemStateChanged(ItemEvent ie)
{
repaint();
}

public void actionPerformed(ActionEvent ae)
{
repaint();
}



public void paint(Graphics g)
{

//checkbox
msg="Current Status: ";
g.drawString(msg,100,200);
msg=" Win98: " +Win98.getState();
g.drawString(msg,6,100);
msg=" winNT: " +winNT.getState();
g.drawString(msg,6,120);
msg=" Solaris: "+solaris.getState();
g.drawString(msg,6,140);
msg=" mac: "+mac.getState();
g.drawString(msg,6,160);

//checkboxgroup
msg="Number selection: ";
msg+=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,0,180);

//choicelist
msg="Fruit Selection: ";
msg+=fruit.getSelectedItem();
g.drawString(msg,40,200);

//list
int idx[];
msg="Vegetable selection: ";
idx=vegetables.getSelectedIndexes();
for(int i=0; i<idx.length;i++)
msg+=vegetables.getItem(idx[i])+" ";
g.drawString(msg,60,220);

//textfield
g.drawString("Name: "+ name.getText(),80,240);
g.drawString("Selected text in name: " + name.getSelectedText(),70,260);
g.drawString("Password: "+ pass.getText(),80,280);
}
}


(2)Scroll Text Program:
import java.awt.*;
import java.applet.*;

public class Welcome extends Applet implements Runnable {
 
  Font f = new Font("Arial Black", Font.BOLD, 24);
  Color col = new Color(204,153,0);
  Color back = new Color(249,249,249);
  String wel = new String("WELCOME TO MY SITE");
  Thread runner;
  int x;
  int stringSize;
 
  public void init() {
     setForeground(col);
     setBackground(back);
      setFont(f);
      FontMetrics fm=getFontMetrics(f);
      stringSize=fm.stringWidth(wel);
  }

  public void start() {
     if (runner == null) {
        runner = new Thread(this);
        runner.start();
     }
  }

  public void stop() {
     if (runner != null) {
        runner = null;
     }
  }

  public void run() {
     Thread thisThread = Thread.currentThread();
     x = 10-stringSize;
     int cont = getSize().width;
     while (runner == thisThread) {
        x = x + 2;
        repaint();
        try {
           Thread.sleep(20);
        } catch (InterruptedException e) { }
        if (x > cont) x = -stringSize;
     }
  }

  public void paint(Graphics g) {
   
     g.drawString(wel, x, 25);
  }
}  


0
 
Asta CuTechnical consultant & graphic designCommented:
Somewhat off-topic, but important.

****************************** ALERT********************************
WindowsUpdate - Critical Update alert March 28, 2002 from Microsoft
http://www.microsoft.com/technet/treeview/default.asp?url=/technet/security/bulletin/ms02-015.asp
Synopsis:
Microsoft Security Bulletin MS02-015  
28 March 2002 Cumulative Patch for Internet Explorer
Originally posted: March 28, 2002
Summary
Who should read this bulletin: Customers using Microsoft® Internet Explorer
Impact of vulnerability: Two vulnerabilities, the most serious of which would allow script to run in the Local Computer Zone.
Maximum Severity Rating: Critical
Recommendation: Consumers using the affected version of IE should install the patch immediately.
Affected Software:
Microsoft Internet Explorer 5.01
Microsoft Internet Explorer 5.5
Microsoft Internet Explorer 6.0

Thought you'd appreciate knowing this.
":0)
Asta
0
 
CleanupPingCommented:
nafisa:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
sudhakar_koundinyaCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


[points to kamaljeets]


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.