Adding ActionListener, Where am I wrong?

Hello,

Please see the two java files that are working fine, but I need to add actionlisteners to RectangleButton.java file, Could any one please help me? Getting text from textfield of superclass into the a string in the sub-class is my problem.

1st File

import java.awt.*;

import java.awt.event.*;

import java.lang.*;

import java.util.*;

import java.applet.*;

public class RectangleButton extends Applet //implements ActionListener

{

TextField tf1,tf2,tf3,tf4;

Label l1,l2,l3,l4;

//Button submit;

public void init()

{

//setLayout(new BorderLayout());

//setLayout(null);

tf1 = new TextField(20);

tf2 = new TextField(20);

tf3 = new TextField(20);

tf4 = new TextField(20);

//submit = new Button("Submit");

add(l1 = new Label("UserName:"));

add(l2 = new Label("Password:"));

add(l3 = new Label("Name:"));

add(l4 = new Label("Age:"));

l1.setBounds(10,20,65,10);

l2.setBounds(10,50,65,10);

l3.setBounds(10,80,65,10);

l4.setBounds(10,110,65,18);

tf1.setBounds(90,15,170,25);

tf2.setBounds(90,45,170,25);

tf3.setBounds(90,75,170,25);

tf4.setBounds(90,105,170,25);

add(tf1);

add(tf2);

add(tf3);

add(tf4);

//add(submit);

RectangleButton2 button1 = new RectangleButton2("Submit");

add(button1);

SimpleActionListener listener = new SimpleActionListener();

button1.addActionListener(listener);

//submit.addActionListener(listener);

}

}

class SimpleActionListener implements ActionListener

{

//TextField tf1;

RectangleButton rectbutton = new RectangleButton();

public SimpleActionListener(){ }

public void actionPerformed(ActionEvent ae)

{

/*if(ae.getActionCommand().equalsIgnoreCase("button1"))

{

String st1 = rectbutton.tf1.getText();

//System.out.println("Clicked:" +ae.getActionCommand());

System.out.println(st1);

}*/

System.out.println("Clicked:" +ae.getActionCommand());

if(ae.getSource() == ("Submit"))

{

String st1 = rectbutton.tf1.getText();

System.out.println("Name = :"+st1);

}

}

}

2nd File

import java.awt.*;

import java.lang.*;

import java.awt.event.*;

import java.util.*;

import java.applet.*;

public class RectangleButton2 extends Component

{

String string;

int capWidth=20;

boolean pressed = false;

ActionListener actionListener;

Color interior = Color.blue;

Color highlight1 = Color.red;

Color highlight2= Color.green;

TextField tf1;

public RectangleButton2()

{

this("");

}

public RectangleButton2(String string)

{

this.string = string;

//enableEvent(AWTEvent.MOUSE_EVENT_MASK);

}

public String getLabel()

{

return string;

}

public void setLabel(String string)

{

this.string = string;

invalidate();

repaint();

}

public void paint(Graphics g)

{

int width= getSize().width - 1;

int height = getSize().height - 1;

interior = getBackground();

g.setColor(interior);

g.drawRect(width - capWidth,0,capWidth,height);

g.fillRect(capWidth/2,0,width-capWidth,width);

g.setColor(highlight1);

g.drawLine(capWidth/2,0,width-capWidth/2,0);

//g.drawLine(0,0,width-capWidth/2,0);

g.setColor(highlight1);

g.drawLine(capWidth/2,height,width-capWidth/2,height);

//g.drawLine(capWidth,height,width-capWidth,height);

g.setColor(highlight1);

g.drawArc(0,0,capWidth,height,90,180-40);

g.drawArc(0,0,capWidth,height,270-40,40);

g.drawArc(width-capWidth,0,capWidth,height,90-40,40);

g.drawArc(width-capWidth,0,capWidth,height,270,180-40);

g.draw3DRect(capWidth,0,capWidth*2,height,true);

Font f = getFont();

if(f != null)

{

FontMetrics fm = getFontMetrics(getFont());

g.setColor(getForeground());

g.drawString(string,

width/2 - fm.stringWidth(string)/2,

height/2 + fm.getHeight()/2 - fm.getMaxDescent());

}

}

public Dimension getPreferredSize()

{

Font f = getFont();

if(f != null)

{

FontMetrics fm = getFontMetrics(getFont());

return new Dimension(fm.stringWidth(string) + capWidth*2,

fm.getHeight() + 10);

}

else

{

return new Dimension(100, 50);

}

}

public Dimension getMinimumSize()

{

return new Dimension(100, 50);

}

public void addActionListener(ActionListener listener)

{

actionListener = AWTEventMulticaster.add(actionListener, listener);

enableEvents(AWTEvent.MOUSE_EVENT_MASK);

}

public void removeActionListener(ActionListener listener)

{

actionListener = AWTEventMulticaster.remove(actionListener, listener);

}

public void processMouseEvent(MouseEvent e)

{

Graphics g;

switch(e.getID())

{

case MouseEvent.MOUSE_PRESSED:

pressed = true;

repaint();

break;

case MouseEvent.MOUSE_RELEASED:

if(actionListener != null)

{

actionListener.actionPerformed(new ActionEvent

(

this, ActionEvent.ACTION_PERFORMED, string));

}

if(pressed == true)

{

pressed = false;

repaint();

}

break;

case MouseEvent.MOUSE_ENTERED:

break;

case MouseEvent.MOUSE_EXITED:

if(pressed == true)

{

pressed = false;

repaint();

}

break;

}

super.processMouseEvent(e);

}

}

Waiting for reply.

Regards

Uma

umajava@yahoo.com

[5920 byte] By [reddyumamaheswar] at [2007-9-26 1:15:22]
# 1

>

> RectangleButton2 button1 = new

> w RectangleButton2("Submit");

> add(button1);

> SimpleActionListener listener = new

> w SimpleActionListener();

> button1.addActionListener(listener);

This is correct.

>

> class SimpleActionListener implements ActionListener

> {

> //TextField tf1;

> RectangleButton rectbutton = new RectangleButton();

This is wrong. You are creating another instance of RectangleButton.

> public void actionPerformed(ActionEvent ae)

> {

> /*if(ae.getActionCommand().equalsIgnoreCase("button1"

> )

getActionCommand returns the string you previously passed to setActionCommand, but you have not calld that (call it where you create the button).

> if(ae.getSource() == ("Submit"))

Get source returns the object (in this case an instance of RectangleButton2) that was clicked on. You have confused getActionCommand and getSource.

I didn't read the second class.

I think you should read some tutorial on action listeners.

Here is one (it is for swing but it is the same in AWT): http://java.sun.com/docs/books/tutorial/uiswing/events/index.html

Here is the API documentation for ActionEvent so you can read about getActionCommand and getSource: http://java.sun.com/j2se/1.3/docs/api/java/awt/event/ActionEvent.html

DanielN at 2007-6-29 0:41:42 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
No better example to understand how listeners work: http://www.javaworld.com/javaworld/jw-09-1998/jw-09-techniques.htmlHope this helps.
Eric.Vautier at 2007-6-29 0:41:42 > top of Java-index,Archived Forums,New To Java Technology Archive...