Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2006

Tip: Looking for answers? Try searching our database.

Event Handling

Thread view: 
raver - 04 Feb 2006 19:32 GMT
HI, Can anybody help me out here, i am building a chatwindow with three
buttons 'send,clear,quit choice list, textarea & textfield....

I've got three classes ChatWindow, QuitChat, SendAndClearHandler

QuitChat class works O.K., i can close window + use quit button

Why can't i get my two other buttons two work

Tony...
----------------------------------------------------------
import java.awt.*;
import java.awt.event.*;

public class SendAndClearHandler implements ActionListener
{
   private TextArea ta2;
   private TextField tf2;

   public SendAndClearHandler(TextArea ta2, TextField tf2){
    this.ta2 = new TextArea();
    this.tf2 = new TextField();
   }

   public void actionPerformed(ActionEvent a){
       if(a.getActionCommand() == "Send" || a.getSource() == tf2)
       {
        ta2.setText(ta2.getText() + tf2.getText() + "\n");
       }
       else if(a.getActionCommand() == "Clear"){ta2.setText("");
       {
        tf2.setText("");
       }
   }
}
}
----------------------------------------------------------
import java.awt.*;
import java.awt.Event;

public class ChatWindow extends Frame
{
   private Button clear;
   private Button send;
   private Button quit;
   private TextField tf1;
   private Choice combo;
private TextArea ta1;

   public ChatWindow(String title)
   {
          super(title);
          /*
       * (non-Javadoc)
       *
       * pan1, 1st panel
       */
          Panel pan1 = new Panel();
          /*
       * (non-Javadoc)
       *
       * pan2, 2nd panel
       */
          Panel pan2 = new Panel();
       /*
       * (non-Javadoc)
       *
       * This code is setting panel 2, to borderlayout manager
       */
       pan2.setLayout(new BorderLayout ());
          /*
       * (non-Javadoc)
       *
       * This line of code is setting panel 1, to Gridlayout manager
       */
          pan1.setLayout(new GridLayout (3,1));
       /*
       * (non-Javadoc)
       *
       * This line of code is declairing main txt field
       */
          tf1 = new TextField(80);
       /*
       * (non-Javadoc)
       *
       * This line of code is declairing choice list
       */
          combo = new Choice();
       /*
       * (non-Javadoc)
       *
       * This code is adding three buttons to 1st panel
       */
          clear = new Button ("Clear");
          pan1.add(clear,
                                     BorderLayout.WEST);
          send = new Button ("Send");
          pan1.add(send,
                                     BorderLayout.WEST);
          quit = new Button ("quit");
          pan1.add(quit,
                                     BorderLayout.WEST);
       /*
       * (non-Javadoc)
       *
       * This line of code is adding choice list to 2nd panel
       */
          pan2.add(combo, BorderLayout.WEST);
       /*
       * (non-Javadoc)
       *
       * This code is adding choice box options
       */
          combo.addItem("brb");
          combo.addItem("lol");
          combo.addItem("cul8r");
       /*
       * (non-Javadoc)
       *
       * This code is adding a text field to 2nd panel
       */
          pan2.add(tf1, BorderLayout.CENTER);
       /*
       * (non-Javadoc)
       *
       * This line of code is declairing text area
       */
          ta1 = new TextArea();
          this.add(pan1, BorderLayout.WEST);
          this.add(ta1, BorderLayout.CENTER);
       this.add(pan2, BorderLayout.SOUTH);

       /*
        * (non-Javadoc)
        *
        *
        * Creating an instance of QuitChat
        */
        QuitChat qc = new QuitChat();
        /*
         * (non-Javadoc)
         *
         * This line of code is adding Frame to ActionListener, by calling
         * it this
         */
        this.addWindowListener(qc);
        /*
         * This line of code is adding quit to ActionListener
         */
        quit.addActionListener(qc);
  /*
  * (non-Javadoc)
  *
  * This line is creating an instance of SendAndClearHandler,
  * with two arguments TextArea & Textfield
  */
        SendAndClearHandler sach = new SendAndClearHandler(this.ta1,
this.tf1);
  /*
  * (non-Javadoc)
  *
  * This line of code is adding Textfield to ActionListener,
  * & then using our SendAndClearHandler
  */
  this.tf1.addActionListener(sach);
  /*
  * (non-Javadoc)
  *
  * This line of code is adding send to ActionListener
  * & then using our SendAndClearHandler
  */
        send.addActionListener(sach);
  /*
  * (non-Javadoc)
  *
  * This line of code is adding clear to ActionListener
  * & then using our SendAndClearHandler
  */
  clear.addActionListener(sach);

   }
   /*
* (non-Javadoc)
*
* Declairing main
*/
public static void main(String [] args){
       ChatWindow cw = new ChatWindow("Hello, ChatWindows");
       cw.setSize(500,400);
       cw.setVisible(true);
   }
}
im - 04 Feb 2006 20:12 GMT
Part of your problem is that the event listener creates its own
TextArea and TextField objects:

>     public SendAndClearHandler(TextArea ta2, TextField tf2){
>      this.ta2 = new TextArea();
>      this.tf2 = new TextField();
>     }

Something like this may work better:

this.ta2 = ta2;
this.tf2 = tf2;

Imre
Tony_P - 06 Feb 2006 11:53 GMT
yes you was right thank you again!!!!

Tony_P
> Part of your problem is that the event listener creates its own
> TextArea and TextField objects:
[quoted text clipped - 10 lines]
>
> Imre
Tony_P - 06 Feb 2006 17:52 GMT
HI,Can anybody tell me why i can't seem to get my 'Choice' list to display
ever

Tony_P
-------------------------------------------------------
import java.awt.TextArea;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class ChoiceHandler implements ItemListener {
   private TextArea ta3;

   public ChoiceHandler(TextArea ta3){
    this.ta3 = ta3;
   }

   public void itemStateChanged(ItemEvent e){
    if(e.getItem() == "brb" || e.getSource() == ta3){
     ta3.setText(ta3.getSelectedText());
    }
    else if(e.getItem() == "lol" || e.getSource() == ta3){
     ta3.setText(ta3.getSelectedText());
    }
    if(e.getItem() == "cul8r" || e.getSource() == ta3){
     ta3.setText(ta3.getSelectedText());
    }
   }
}
-------------------------------------------------------
import java.awt.*;
import java.awt.Event;
/*
* (non-Javadoc)
*
* MODULE: "6_2"
* This program prints: "Event Handling"
* Filename: "ChatWindow.java"
* @author: "Tony Prowse"
* Enroll No: 6611204966
* @e-mail: "tony.prowse@blueyonder.co.uk"
*/

public class ChatWindow extends Frame
{
   private Button clear;
   private Button send;
   private Button quit;
   private TextField tf1;
   private Choice combo;
private TextArea ta1;

   public ChatWindow(String title)
   {
          super(title);
          /*
       * (non-Javadoc)
       *
       * pan1, 1st panel
       */
          Panel pan1 = new Panel();
          /*
       * (non-Javadoc)
       *
       * pan2, 2nd panel
       */
          Panel pan2 = new Panel();
       /*
       * (non-Javadoc)
       *
       * This code is setting panel 2, to borderlayout manager
       */
       pan2.setLayout(new BorderLayout ());
          /*
       * (non-Javadoc)
       *
       * This line of code is setting panel 1, to Gridlayout manager
       */
          pan1.setLayout(new GridLayout (3,1));
       /*
       * (non-Javadoc)
       *
       * This line of code is declairing main txt field
       */
          tf1 = new TextField(80);
       /*
       * (non-Javadoc)
       *
       * This line of code is declairing choice list
       */
          combo = new Choice();
       /*
       * (non-Javadoc)
       *
       * This code is adding three buttons to 1st panel
       */
          clear = new Button ("Clear");
          pan1.add(clear,
                                     BorderLayout.WEST);
          send = new Button ("Send");
          pan1.add(send,
                                     BorderLayout.WEST);
          quit = new Button ("quit");
          pan1.add(quit,
                                     BorderLayout.WEST);

       /*
       * (non-Javadoc)
       *
       * This line of code is adding choice list to 2nd panel
       */
          pan2.add(combo, BorderLayout.WEST);
       /*
       * (non-Javadoc)
       *
       * This code is adding choice box options
       */
          combo.addItem("brb");
          combo.addItem("lol");
          combo.addItem("cul8r");
       /*
       * (non-Javadoc)
       *
       * This code is adding a text field to 2nd panel
       */
          pan2.add(tf1, BorderLayout.CENTER);
       /*
       * (non-Javadoc)
       *
       * This line of code is declairing text area
       */
          ta1 = new TextArea();
          this.add(pan1, BorderLayout.WEST);
          this.add(ta1, BorderLayout.CENTER);
       this.add(pan2, BorderLayout.SOUTH);

       /*
        * (non-Javadoc)
        *
        * Creating an instance QuitChat
        */
        QuitChat qc = new QuitChat();
        /*
         * (non-Javadoc)
         *
         * This line of code is adding Frame to ActionListener, by calling
         * it this
         */
        this.addWindowListener(qc);
        /*
         * This line of code is adding quit to ActionListener
         */
        quit.addActionListener(qc);
        /*
         * (non-Javadoc)
         *
         * Creating an instance of SendAndClearHandler
         * With two arguments TextArea & TextField
         */
        SendAndClearHandler sch = new SendAndClearHandler(this.ta1,
this.tf1);
        /*
         * (non-Javadoc)
         *
         * Creating an instance of ChoiceHandler
         * With one argument TextArea
         */
        ChoiceHandler ch = new ChoiceHandler(this.ta1);
        /*
         * (non-Javadoc)
         *
         * This line of code is adding TextField to a listener,
         * & then adding handler
         */
        this.tf1.addActionListener(sch);

        this.combo.addItemListener(ch);
        /*
         * (non-Javadoc)
         *
         * This line of code is adding send to a listener,
         * & then adding handler
         */
        send.addActionListener(sch);
        /*
         * (non-Javadoc)
         *
         * This line of code is adding clear to a listener,
         * & then adding handler
         */
        clear.addActionListener(sch);
        /*
         * (non-Javadoc)
         *
         * Local variable
         */
  Button enter = send;
  /*
         * (non-Javadoc)
         *
         * This line of code is adding enter to a listener,
         * & then adding handler
         */
        enter.addActionListener(sch);
        Choice brb = this.combo;
  brb.addItemListener(ch);
  Choice lol = this.combo;
  lol.addItemListener(ch);
  Choice cul8r = this.combo;
  cul8r.addItemListener(ch);
   }
   /*
* (non-Javadoc)
*
* Declairing main
*/
public static void main(String [] args){
       ChatWindow cw = new ChatWindow("Hello, ChatWindows");
       cw.setSize(500,400);
       cw.setVisible(true);
   }
import java.awt.*;
import java.awt.Event;

public class ChatWindow extends Frame
{
   private Button clear;
   private Button send;
   private Button quit;
   private TextField tf1;
   private Choice combo;
private TextArea ta1;

   public ChatWindow(String title)
   {
          super(title);
          /*
       * (non-Javadoc)
       *
       * pan1, 1st panel
       */
          Panel pan1 = new Panel();
          /*
       * (non-Javadoc)
       *
       * pan2, 2nd panel
       */
          Panel pan2 = new Panel();
       /*
       * (non-Javadoc)
       *
       * This code is setting panel 2, to borderlayout manager
       */
       pan2.setLayout(new BorderLayout ());
          /*
       * (non-Javadoc)
       *
       * This line of code is setting panel 1, to Gridlayout manager
       */
          pan1.setLayout(new GridLayout (3,1));
       /*
       * (non-Javadoc)
       *
       * This line of code is declairing main txt field
       */
          tf1 = new TextField(80);
       /*
       * (non-Javadoc)
       *
       * This line of code is declairing choice list
       */
          combo = new Choice();
       /*
       * (non-Javadoc)
       *
       * This code is adding three buttons to 1st panel
       */
          clear = new Button ("Clear");
          pan1.add(clear,
                                     BorderLayout.WEST);
          send = new Button ("Send");
          pan1.add(send,
                                     BorderLayout.WEST);
          quit = new Button ("quit");
          pan1.add(quit,
                                     BorderLayout.WEST);

       /*
       * (non-Javadoc)
       *
       * This line of code is adding choice list to 2nd panel
       */
          pan2.add(combo, BorderLayout.WEST);
       /*
       * (non-Javadoc)
       *
       * This code is adding choice box options
       */
          combo.addItem("brb");
          combo.addItem("lol");
          combo.addItem("cul8r");
       /*
       * (non-Javadoc)
       *
       * This code is adding a text field to 2nd panel
       */
          pan2.add(tf1, BorderLayout.CENTER);
       /*
       * (non-Javadoc)
       *
       * This line of code is declairing text area
       */
          ta1 = new TextArea();
          this.add(pan1, BorderLayout.WEST);
          this.add(ta1, BorderLayout.CENTER);
       this.add(pan2, BorderLayout.SOUTH);

       /*
        * (non-Javadoc)
        *
        * LAB 6_2
        *
        * Creating an instance QuitChat
        */
        QuitChat qc = new QuitChat();
        /*
         * (non-Javadoc)
         *
         * This line of code is adding Frame to ActionListener, by calling
         * it this
         */
        this.addWindowListener(qc);
        /*
         * This line of code is adding quit to ActionListener
         */
        quit.addActionListener(qc);
        /*
         * (non-Javadoc)
         *
         * Creating an instance of SendAndClearHandler
         * With two arguments TextArea & TextField
         */
        SendAndClearHandler sch = new SendAndClearHandler(this.ta1,
this.tf1);
        /*
         * (non-Javadoc)
         *
         * Creating an instance of ChoiceHandler
         * With one argument TextArea
         */
        ChoiceHandler ch = new ChoiceHandler(this.ta1);
        /*
         * (non-Javadoc)
         *
         * This line of code is adding TextField to a listener,
         * & then adding handler
         */
        this.tf1.addActionListener(sch);

        this.combo.addItemListener(ch);
        /*
         * (non-Javadoc)
         *
         * This line of code is adding send to a listener,
         * & then adding handler
         */
        send.addActionListener(sch);
        /*
         * (non-Javadoc)
         *
         * This line of code is adding clear to a listener,
         * & then adding handler
         */
        clear.addActionListener(sch);
        /*
         * (non-Javadoc)
         *
         * Local variable
         */
  Button enter = send;
  /*
         * (non-Javadoc)
         *
         * This line of code is adding enter to a listener,
         * & then adding handler
         */
        enter.addActionListener(sch);
      Choice brb = this.combo;
      brb.addItemListener(ch);
     Choice lol = this.combo;
     lol.addItemListener(ch);
     Choice cul8r = this.combo;
     cul8r.addItemListener(ch);
   }
   /*
* (non-Javadoc)
*
* Declairing main
*/
public static void main(String [] args){
       ChatWindow cw = new ChatWindow("Hello, ChatWindows");
       cw.setSize(500,400);
       cw.setVisible(true);
   }
}

> yes you was right thank you again!!!!
>
[quoted text clipped - 13 lines]
>>
>> Imre
Roedy Green - 07 Feb 2006 03:57 GMT
On Mon, 06 Feb 2006 17:52:35 GMT, "Tony_P"
<tony.prowse@blueyonder.co.uk> wrote, quoted or indirectly quoted
someone who said :

>  combo = new Choice();

I think you have a naming problem.  I see four different things you
are all calling combo.

Choice does not support combos, only single choices.  So rename your
variables and make them meaningful and unique and I think you problem
will away.

not just "choice" but "colourChoice" or "provinceChoice" or whatever
it is you are selecting.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 04 Feb 2006 20:44 GMT
On Sat, 04 Feb 2006 19:32:27 GMT, "raver"
<tony.prowse@blueyonder.co.uk> wrote, quoted or indirectly quoted
someone who said :

>public class SendAndClearHandler implements ActionListener
>{
[quoted text clipped - 7 lines]
>
>    public void actionPerformed(ActionEvent a){

you are missing your addActionListener lines. See
http://mindprod.com/jgloss/textarea.html for sample code.  Just
implementing the interface is not sufficient to hook it up the way it
was in the JDK 1.0 event model.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.