Re: enter event (and source)

From: Roderik (ng_at_nl100.cjb.net)
Date: 11/27/03


Date: Thu, 27 Nov 2003 15:51:32 +0100

On Thu, 27 Nov 2003 13:54:54 GMT, Christophe Vanfleteren
<c.v4nfl3t3r3n@pandora.be> wrote:

>Roderik wrote:
>
>> Hi,
>>
>> I am building an applet with some textfields. When the user hits enter
>> in a textfield an event should occur. I don't know how I can determine
>> from which textfield the ENTER is coming.
>> What is an easy way to execute some code when a user hits enter and to
>> determine from within which textfield it was pressed.
>>
>> Thanks in advance,
>>
>> Roderik
>>
>> --
>> code:
>>
>> // Java
>> // Exceptie-afhandeling
>>
>> // Roderik Emmerink
>> // Marinus van Heuvelen
>>
>> import java.applet.*;
>> import java.awt.*;
>> import java.lang.String;
>>
>> public class OpdrachtA extends Applet
>> {
>> public TextField txtfldObj1 = new TextField();
>> public TextField txtfldObj2 = new TextField();
>> public TextField txtfldObj3 = new TextField();
>>
>> MijnClass MijnObject1 = new MijnClass();
>> MijnClass MijnObject2 = new MijnClass();
>> MijnClass MijnObject3 = new MijnClass();
>>
>> Font f;
>>
>> public void init() {
>> setLayout(null);
>> setSize(400,400);
>>
>> f = new Font("Helvetica",Font.ITALIC, 20);
>> setBackground(new Color(0x99,0x99,0xCC));
>>
>> txtfldObj1.setBounds(140,100,100,20);
>> add(txtfldObj1);
>>
>> txtfldObj2.setBounds(140,130,100,20);
>> add(txtfldObj2);
>>
>> txtfldObj3.setBounds(140,160,100,20);
>> txtfldObj3.setBackground(new Color(0xCC,0xCC,0xFF));
>> add(txtfldObj3);
>>
>> Button btnBerekenSom = new Button("Bereken som");
>> btnBerekenSom.setBounds(140,190,100,20);
>> add(btnBerekenSom);
>>
>> repaint();
>> }
>>
>> static void berekenSom() throws WaardeExceptie {
>> // moi
>> }
>>
>> public void paint(Graphics g) {
>>
>> g.setFont(f);
>> g.setFont(new Font("Helvetica", Font.PLAIN, 10));
>> g.setFont(new Font("Helvetica", Font.PLAIN, 12));
>> g.drawString("Object 1 - x:", 20, 115);
>> g.drawString("Object 2 - x:", 20, 145);
>> g.drawString("Object 3 - x:", 20, 175);
>> }
>>
>> // method to handle key - down events
>> public boolean keyDown(Event e, int key) {
>> // user presses enter key
>> if (key == Event.ENTER) {
>> System.out.println(e.getSource); //? <---------------
>> }
>>
>> }
>
>Use an if to see what textfield was the source:
>
>if (key == Event.ENTER) {
> Object source = e.getSource;
> if(source == txtfldObj1){
> //do something
> }else if(source == txtfldObj2) {
> //do something else
> }
> ...
>}

I get:
"OpdrachtA.java": Error #: 300 : variable getSource not found in class
java.awt.Event at line 68, column 25

At the rule:
Object source = e.getSource;

Any idea, how I can solve this?

regards,

Roderik