AWT program

Problems while trying this code in which , when button is pressed the content of the textfeild should be appended. to the list provided it does'nt already exist.

import java.awt.*;

import java.awt.event.*;

/*

<applet code="TextFeild1" width=500 height=200>

</applet>

*/

public class TextFeild1 extends java.applet.Applet implements ActionListener

{

public void init()

{

Button b= new Button("tFeild");

TextField t= new TextField(20);

List l= new List();

add(b);

add(t);

add(l);

//ButList b1= new ButList();

// b.addActionListener(b1);

// public void actionPerformed(ActionEvent ae)

// {

// System.out.println("button clicked");

// }

//b.addActionListener(ae);

// }

class ButList implements ActionListener

{

public void actionPerformed(ActionEvent ae)

{

System.out.println("button clicked");

}

}

}

}

Errors in code:

TextFeild1 is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener

public class TextFeild1 extends java.applet.Applet implements ActionListener

^

1 error

Tool completed with exit code 1

I will appreciate your suggestions regarding the same.

Thanks,

Nishant

[1428 byte] By [nish_atowna] at [2007-11-26 19:45:31]
# 1

TextFeild1 is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener

public class TextFeild1 extends java.applet.Applet implements ActionListener

^

1 error

This is saying that since the TextFeild1 class implements the ActionListener interface it is required to implement each and every method defined by that interface. There is only one method defined in the ActionListener interface: actionPerformed. The compiler is saying that it is unable to find this method signature in the TextFeild1 class.

import java.awt.*;

import java.awt.event.*;

/*

<applet code="TF1" width=500 height=200>

</applet>

*/

public class TF1 extends java.applet.Applet implements ActionListener

{

public void init()

{

// top section

Button topButton = new Button("top button");

topButton.addActionListener(this);

Panel topPanel = new Panel();

topPanel.add(topButton);

// center section

List list = new List();

list.add("Saturn");

list.add("Uranus");

list.add("Neptune");

list.add("Pluto");

TextField textField = new TextField("Hello world", 20);

Panel center = new Panel(new BorderLayout());

center.add(list);

center.add(textField, "South");

// south section

Button bottomButton = new Button("bottom button");

bottomButton.addActionListener(new ButtonListener());

Panel bottomPanel = new Panel();

bottomPanel.add(bottomButton);

// assemble

System.out.println("default layout manager for Applet = " +

getLayout().getClass().getName());

setLayout(new BorderLayout());

add(topPanel, "North");

add(center);

add(bottomPanel, "South");

}

/**

* If the enclosing class (TF1) implements the ActionListener

* interface then this class must provide a method with this

* exact signature. Any interface that a class implements must

* have all of its methods implemented in/by the class. So

* this method is the class implementation of the ActionListener

* interface.

public void actionPerformed(ActionEvent ae)

{

Button button = (Button)ae.getSource();

String ac = button.getActionCommand();

System.out.println("button clicked = " + ac);

}

/**

* Nested class that implements ActionListener.

* This nested class is required to implement the actionPerformed

* method which is defined in the ActionListener interface.

*/

class ButtonListener implements ActionListener

{

public void actionPerformed(ActionEvent ae)

{

Button button = (Button)ae.getSource();

String ac = button.getActionCommand();

System.out.println("button clicked = " + ac);

}

}

}

crwooda at 2007-7-9 22:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 2
Instead of a TextField use a JTextArea and then use append( ) or insert( ) method to append to the JTextArea
qUesT_foR_knOwLeDgea at 2007-7-9 22:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 3

AWT Program objective:

1}program with list , textfeild and button.

2}when the button is pressed , the content of the textfeild is appended to the list provided it exists

3}if the textfeild is empty place the cursor back into textfeild

i am unable to acheive the 3rd objective of this code.

Can anyone help me in using the this kind of cursor movement.

thanks

nish

import java.awt.*;

import java.awt.event.*;

import java.awt.TextField;

import java.awt.Cursor;

/*

<applet code="TextFeild3" width=500 height=200>

</applet>

*/

public class TextFeild3 extends java.applet.Applet //implements

//ActionListener

{List lst;

String A;

TextField t;

public void init()

{

Button b= new Button("Append TextFeild Button");

t= new TextField("Hello World");

// t= new TextField(" ");

lst = new List(2, true);

lst.add("Mercury");

lst.add("Venus");

add(b);

add(t);

add(lst);

ButList b1= new ButList(this);

b.addActionListener(b1);

t.addTextListener(b1);

lst.addActionListener(b1);

}

public void repaint()

{

A=t.getText();

if(!(A.equals(" ")))

{

lst.add(A,-1); // this thing is not working when I am trying to prevent it from entering spaces ie new 20 sth

}

else

{

t.getDefaultCursor();

}

}

}

class ButList implements ActionListener, TextListener

{

TextFeild3 t;

ButList(TextFeild3 t1)

{

t=t1;

}

public void actionPerformed(ActionEvent ae)

{

t.repaint();

}

public void textValueChanged(TextEvent e)

{

System.out.println("entered text");

}

}

nish_atowna at 2007-7-9 22:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 4

Button yourButton = new Button("GO");

yourButton.addActionListener(/* ... */);

TextField yourTextField = new TextField();

public void actionPerformed(ActionEvent ae) {

String s = yourTextField.getText();

if (s.equals(""))

yourTextField.requestFocus();

}

abillconsla at 2007-7-9 22:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 5
Help or no help?
abillconsla at 2007-7-9 22:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 6
?
abillconsla at 2007-7-9 22:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 7
Thanks ,It is really useful suggestionNish
nish_atowna at 2007-7-9 22:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 8

i have a similar kind of error in event handling

i am posting the error msg with the code below

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*

<applet code =" MouseEvents " width=300 height=40>

</applet>

*/

public class MouseEvents extends Applet

implements MouseListener, MouseMotionListener

{

String msg = " ";

int mouseX = 0, mouseY = 0;

public void init()

{

addMouseListener(this);

addMouseMotionListener(this);

}

public void mouseClicked(MouseEvent me)

{

mouseX = 0;

mouseY = 10;

msg = "MouseEntered ";

repaint();

}

/*public void mouseExited(MouseEvent me)

{

mouseX = 0;

mouseY = 10;

msg = "Mouse Exited";

repaint();

}*/

public void mousePressed ( MouseEvent me)

{

mouseX = me.getX();

mouseY = me.getY();

msg = " Down " ;

repaint ();

}

public void mouseReleased ( MouseEvent me)

{

mouseX = me.getX();

mouseY = me.getY();

msg = "UP";

repaint();

}

public void mouseDragged(MouseEvent me)

{

mouseX = me.getX();

mouseY = me.getY();

msg = " * " ;

showStatus ( "Dragging mouse at " + mouseX + " , " + mouseY);

repaint();

}

public void mouseMoved(MouseEvent me)

{

showStatus("Moving mouse at " + me.getX() + " , " +me.getY());

}

public void paint(Graphics g)

{

g.drawString(msg,mouseX,mouseY);

}

}

MouseEvent is not abstract and does not override abstract

method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener

public class MouseEvent extends Applet

^

2 errors

paramjeeta at 2007-7-9 22:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 9

You commented–out one of the required method implementations for the MouseListener

interface, viz, mouseExited. Since your class has implemented the MouseListener interface

you are required to provide an implementation of each of its methods. The implementation

of any of the methods can be empty, ie, no code in the method body (curley braces). But

the method signature must appear in the class body.

To fix your error comment–out the code in the method body and leave the method

declaration:

public void mouseEntered(MouseEvent e) { }

public void mouseExited(MouseEvent me)

{

/*

mouseX = 0;

mouseY = 10;

msg = "Mouse Exited";

repaint();

*/

}

This is oaky now:

// <applet code ="ME" width=300 height=40></applet>

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class ME extends Applet

implements MouseListener, MouseMotionListener

{

String msg = " ";

int mouseX = 0, mouseY = 0;

public void init()

{

addMouseListener(this);

addMouseMotionListener(this);

}

public void mouseClicked(MouseEvent me)

{

mouseX = 0;

mouseY = 10;

msg = "MouseClicked";

repaint();

}

public void mouseEntered(MouseEvent e) { }

public void mouseExited(MouseEvent me)

{

/*

mouseX = 0;

mouseY = 10;

msg = "Mouse Exited";

repaint();

*/

}

public void mousePressed ( MouseEvent me)

{

mouseX = me.getX();

mouseY = me.getY();

msg = " Down " ;

repaint ();

}

public void mouseReleased ( MouseEvent me)

{

mouseX = me.getX();

mouseY = me.getY();

msg = "UP";

repaint();

}

public void mouseDragged(MouseEvent me)

{

mouseX = me.getX();

mouseY = me.getY();

msg = " * " ;

showStatus ( "Dragging mouse at " + mouseX + " , " + mouseY);

repaint();

}

public void mouseMoved(MouseEvent me)

{

showStatus("Moving mouse at " + me.getX() + " , " +me.getY());

}

public void paint(Graphics g)

{

g.drawString(msg,mouseX,mouseY);

}

}

crwooda at 2007-7-9 22:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 10

An easy way to do it:

import java.awt.*;

import java.awt.event.*;

/*

<applet code="TextField1" width=500 height=200>

</applet>

*/

public class TextField1 extends java.applet.Applet

{

TextField t;

List l

public void init()

{

Button b= new Button("tFeild");

l = new List();

t = new TextField(20);

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String s = t.getText();

l.add(s);//If this is how to append to list, I don't know. I've never done it before

}

});

add(b);

add(t);

add(l);

}

}

Consideratea at 2007-7-9 22:30:34 > top of Java-index,Desktop,Core GUI APIs...
# 11
Sure thing.
abillconsla at 2007-7-9 22:30:34 > top of Java-index,Desktop,Core GUI APIs...
# 12

Hi,

I modified your program. See the code.

If you have any queries please let me know

import java.awt.*;

import java.awt.event.*;

/*

<applet code="TextFeild1" width=500 height=200>

</applet>

*/

public class TextFeild1 extends java.applet.Applet implements ActionListener

{

Button b;

TextField t;

List l;

public void init()

{

b= new Button("tFeild");

t= new TextField(20);

l= new List();

add(b);

add(t);

add(l);

ButList b1= new ButList();

b.addActionListener(b1);

}

public void actionPerformed(ActionEvent ae)

{

System.out.println("button clicked");

}

class ButList implements ActionListener

{

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==b)

{

if(!t.getText().trim().equalsIgnoreCase(""))

l.add(t.getText());

}

}

}

}

Thanks & Regards,

Santhosh Reddy Mandadi

santhosh-mcaa at 2007-7-9 22:30:34 > top of Java-index,Desktop,Core GUI APIs...
# 13
hi!when ever you are posting Don't forget to use the [url= http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.regardsAniruddha
Aniruddha-Herea at 2007-7-9 22:30:34 > top of Java-index,Desktop,Core GUI APIs...