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

