2

I have this code.. here when i input number "6" in the textfield, text should be displayed in the textarea..but after that if i input any other number i want the textarea contents to be clear. But when I execute my code, the old contents of the textarea remain even when i input a different number. Please help!

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="front" width=500 height=500></applet> */
public class front extends Applet implements ActionListener {
  String msg="";
  TextArea text,text1;
  TextField txt;
  Button load, enter;

  public void init() {
    enter=new Button("Enter");
    load=new Button("Load");
    txt=new TextField(5);
    text=new TextArea(10,15);

    add(load);
    add(text);

    add(txt);
    add(enter);

    load.addActionListener(this);
    txt.addActionListener(this);
    enter.addActionListener(this);
  }

  public void actionPerformed(ActionEvent ae)
  {
    String str = ae.getActionCommand();
    if(str.equals("Load")) {
      msg = "You pressed Load";
    } else {
      if(txt.getText().toString().equals ("6")) {
        msg="Set the text for 6";
        text.setText("Text");
      } else {
        msg="Invalid number";
        text.setText("");
      }
    }
    repaint();
  }

  public void paint(Graphics g) {
    g.drawString(msg,350,250);
  }
}
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
praveena
  • 41
  • 1
  • 1
  • 9
  • 1
    I'm running this example on my computer and the TextArea clears if I type anything other than "6" is the textfield (if I type "6" it sets the textarea to "Text") – Jason Sperske Apr 09 '13 at 17:44
  • Do you mean you want the TextField to be cleared? – Jason Sperske Apr 09 '13 at 17:45
  • TextArea itseld..It does work..but it doesnt work all the time .. especially when there are two text areas.. is there a problem with my software or something? – praveena Apr 09 '13 at 18:00

3 Answers3

2

write your actionPerformed() method as follows

    public void actionPerformed(ActionEvent ae)
  {
    String str = ae.getActionCommand();
    if(str.equals("Load")) {
      msg = "You pressed Load";
    } else {
      if(txt.getText().toString().equals ("6")) 
         {
        **text.setText("");**
        msg="Set the text for 6";
        text.setText("Text");
         } 
         else {
        msg="Invalid number";
        text.setText("");
      }
    }
    repaint();
  }

the mistake was that you were not clearing the text field after writing to it! now it is cleared by using text.setText(""); in if condition

hope this solves your problem!

Nomesh Gajare
  • 855
  • 2
  • 12
  • 28
  • No it doesnt :( it remains as "Text" itself..the thing is only the msg "Invalid msg" gets displayed but the textfield isnt cleared :( – praveena Apr 09 '13 at 17:53
0

You should call super.paint(g) within paint(Graphics g) method:

public void paint(Graphics g) {
    super.paint(g);
    g.drawString(msg,350,250);
  }
Vishal K
  • 12,976
  • 2
  • 27
  • 38
0

Now, text.setText(""); will not do anything and it will same as //text.setText("");

So Better Approach is to take help of ASCII code,

For Null Character ASCII value is 0, in unicode we can write it as '\u0000'

And Finally this Statement will surely work: text.setText(""+'\u0000');

Note: their is no method is textArea class to clear the area... So you have to do this on your own.

Sukhbir
  • 553
  • 8
  • 23