Results 1 to 5 of 5
  1. #1
    themburu is offline Member
    Join Date
    Mar 2008
    Posts
    20
    Rep Power
    0

    Default Seriously need help on my listeners!!

    Hi I have a applet code which I'm trying to add listeners but its really disturbing me.

    What coulb be wrong with it, plus also how do I add listeers for the checkboxes which grouped and also the Choice list?

    When I compile the code it gives me this errors
    1.myRequest is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
    2. addComponentListener(java.awt.event.ComponentListe ner) in java.awt.Component cannot be applied to (myRequest)

    I'm using JCreator 4.50 pro and jdk1.6.0_02

    Here's ma code
    Attached Files Attached Files

  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    It would be easier if you show the code.
    1. I guess you are not implementing actionPerformed(java.awt.event.ActionEvent) in your myRequest class that extends java.awt.event.ActionListener. An you have to do that if you pan to instantiate myRequest. By the way use capitals for your class names: MyRequest.

    2. addComponentListener(java.awt.event.ComponentListe ner) in java.awt.Component cannot be applied to (myRequest) means myRequest is not a ComponentListener and I guess that is true since it extends ActionListener.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  3. #3
    themburu is offline Member
    Join Date
    Mar 2008
    Posts
    20
    Rep Power
    0

    Default myRequest code

    The code is attached below my post thread or should I paste it here

    import java.awt.*; //import the whole awt package
    import javax.swing.JApplet; //import class JApplet
    import java.awt.Graphics; //import class Graphics
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.*;


    public class myRequest extends java.applet.Applet implements ActionListener{

    //Create chekboxes which determine the kind of interaction the user wants
    CheckboxGroup theGroup = new CheckboxGroup();
    Checkbox c1 = new Checkbox("Distances", theGroup, true);
    Checkbox c2 = new Checkbox("Health Facilities", theGroup, false);
    Checkbox c3 = new Checkbox("Hotels & Accomondation", theGroup, false);
    Checkbox c4 = new Checkbox("Government Offices", theGroup, false);
    Checkbox c5 = new Checkbox("Supermarkets", theGroup, false);
    Checkbox c6 = new Checkbox("Banks", theGroup, false);


    //Create Choices so as to have a selection
    Choice menus = new Choice();
    Choice menus2 = new Choice();

    //Create labels to indicate what happpens at each place
    Label whereFrom = new Label("Where do you want to know?");
    Label whereTo = new Label("Select the nearest point to where you are!");
    //Label jeddy = new Label("Go to room D310");

    //A button to send the selected request
    Button report = new Button("Get Direction");


    //Just to format font if I use g.drawString instead of the Label text
    Font mm = new Font("Arial", Font.BOLD, 15);

    //Add action listener
    public void ActionPerformed(ActionEvent evt){
    if (evt.getActionCommand().equals("Distances")){
    setC1();
    }
    if (evt.getActionCommand().equals("Health Facilities")){
    setC2();
    }
    if (evt.getActionCommand().equals("Hotels & Accomondation")){
    setC3();
    }
    if (evt.getActionCommand().equals("Government Offices")){
    setC4();
    }
    if (evt.getActionCommand().equals("Supermarkets")){
    setC5();
    }
    if (evt.getActionCommand().equals("Banks")){
    setC6();
    }


    repaint();
    }

    public void setC1(){
    whereFrom.setText("Where do you want to know?");
    whereTo.setText("Select the nearest point to where you are!");
    report.setLabel("Get Direction"); }
    public void setC2(){
    whereFrom.setText("Which hospital are you interested in?");
    whereTo.setText("Select the nearest point to where you are!");
    report.setLabel("Take me there"); }
    public void setC3(){
    whereFrom.setText("What Star hotel?");
    whereTo.setText("Select the nearest point to where you are!");
    report.setLabel("Give me a list"); }
    public void setC4(){
    whereFrom.setText("What Office are you interested in?");
    whereTo.setText("Select the nearest point to where you are!");
    report.setLabel("Show the office"); }
    public void setC5(){
    whereFrom.setText("Select the supermarket you are looking for?");
    whereTo.setText("Select the nearest point to where you are!");
    report.setLabel("Get direction and prices"); }
    public void setC6(){
    whereFrom.setText("Which bank do you want?");
    whereTo.setText("Select the nearest point to where you are!");
    report.setLabel("Get bank and ATM's"); }

    /** Initialization method that will be called after the applet is loaded
    * into the browser.
    */

    public void init() {

    c1.addItemListener(this);
    c2.addItemListener(this);
    c3.addItemListener(this);
    c4.addItemListener(this);
    c5.addItemListener(this);
    c6.addItemListener(this);


    menus.add("Select One");
    menus.add("City Hall");
    menus.add("Hilton");
    menus.add("Nation Center");
    menus.add("National Archives");
    menus.add("680 Hotel");
    menus.add("Electricity House");
    menus.add("Times tower");
    menus.addItem("Harambee House");
    menus.addItem("Anniversary towers");
    menus.addItem("Viewpark towers");
    menus.addItem("Serena hotel");
    menus.addItem("Nakumatt Lifestyle");
    menus.addItem("I & M House");
    menus.addItem("Barclays Head Office");


    menus2.add("Select One");
    menus2.add("City Hall");
    menus2.add("Hilton");
    menus2.add("Nation Center");
    menus2.add("National Archives");
    menus2.add("680 Hotel");
    menus2.add("Electricity House");
    menus2.add("Times tower");
    menus2.addItem("Harambee House");
    menus2.addItem("Anniversary towers");
    menus2.addItem("Viewpark towers");
    menus2.addItem("Serena hotel");
    menus2.addItem("Nakumatt Lifestyle");
    menus2.addItem("I & M House");
    menus2.addItem("Barclays Head Office");

    add(whereFrom);
    add(whereTo);
    add(menus);
    add(menus2);
    add(report);
    add(c1);
    add(c6);
    add(c3);
    add(c4);
    add(c5);
    add(c2);
    // TODO start asynchronous download of heavy resources
    }

    public void paint(Graphics g) {
    //Set positions for the Checkboxes

    c1.setLocation(13,25);
    c2.setLocation(120,25);
    c3.setLocation(120,65);
    c4.setLocation(120,45);
    c5.setLocation(13,65);
    c6.setLocation(13,45);
    g.drawRect(5,3,275,100);

    /**This part represents where I've used g.drawstring */
    g.setFont(mm);
    g.setColor(Color.BLUE);
    g.drawString("What Help would you like on ?" , 8,19);
    //g.drawString("Where do you want to know?" ,10 ,25);
    //g.drawRect(5,10,180,25);
    // g.drawString("Select the nearest point to where you are!",3, 130);
    //g.drawRect(1,115,240,25);


    /**This part I've used Labels */
    whereFrom.setLocation(10,115);
    whereTo.setLocation(6, 180);

    /*These are the Choices */
    menus.setLocation(15,140);
    menus2.setLocation(15,210);

    report.setLocation(180,250);

    //g.drawString("Rm D310",15, 300);
    //jeddy.setLocation(78,300);
    }
    }

  4. #4
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    You should put your code between UBB code markers.
    The error are caused by:

    1. public void ActionPerformed(ActionEvent evt){
    Java is case sensitive. The name should be: actionPerformed
    2. in the original code you were trying to add an action listener as a component listener.

    What do you try to solve?
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  5. #5
    themburu is offline Member
    Join Date
    Mar 2008
    Posts
    20
    Rep Power
    0

    Default good work n still more problems!

    Now thanks for your help I was able to create action listener for the checkbox now here's the other challenge
    1. Can I have a different String arrays so that as the checkboxes change the text in the labels I can also use this String array to change what appears in the choice list.
    2. The problem reduces to: How can I make a choice use a String array just as the way you do it in a JComboBox?

Similar Threads

  1. Multiple listeners per event
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-03-2008, 10:06 AM
  2. Action Event and Listeners
    By lost1 in forum New To Java
    Replies: 3
    Last Post: 11-14-2007, 04:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •