aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes setText() not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "setText() not working" Watch "setText() not working" New topic
Author

setText() not working

Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
In my application, i have made one JButton, named "Clear", i have placed its handler named ClearHandler in other file with ClearHandler.java. The functionality of the handler is, whenever button is pressed, it should clear all the textfields. For which, i have used following code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class ClearHandler extends GUIApps implements ActionListener{
public void actionPerformed(ActionEvent e){
String s="";
idfield.setText(s);
descfield.setText(s);
ratefield.setText(s);
quantfield.setText(s);
unitfield.setText(s);
System.out.print("Event:="+e.getActionCommand());
}
}


i am not able to get the functionality. after pressing button, i am getting on console, "Event: Clear", but no textfield is getting cleared. Please provide solution.


Regards,
Vishnu
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Please provide solution

please provide code:
1) adding actionListener to the button, or
2) how mentioned textfields are 'seen' by ClearHandler
Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
Michael Dunn wrote:> Please provide solution

please provide code:
1) adding actionListener to the button, or
2) how mentioned textfields are 'seen' by ClearHandler


Code for ClearHandler is i already pasted in previous quote. The text fields are not getting cleared. Whatever value i am putting into them, it should be cleared after pressing the button. But it is not happening.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Michael provides a big hint when he asks how the textfields are "seen" by this class, and per your code it appears that this class in fact has no way of seeing or getting a reference to the JTextFields. Because of this, I am surprised that this class even compiles.

A suggestion:
1) Give your GUI class, the one that holds the JTextFields, a public method, say clearTextFields(), and inside this method do just that, set all the JTextField texts to "".
2) Give your ClearHandler class a GUI variable and in the ClearHandler constructor, pass a reference to the main GUI.
3) In the ClearHandler's actionPerformed method call the clearTextFields() method off of the GUI.

edit: OK, I see why your code compiles and what you're doing wrong. Your handler class extend from the gui class, and yes by doing this it will "see" the fields of the gui, but you must understand that these JTextFields that handler sees are handler's own non-displayed JTextFields and have nothing to do with the ones that are held by the GUIApps object that is displayed, that the handler class inherits completely different objects. You don't use inheritance for this for as you're seeing it simply doesn't work and it's wrong because the Handler class does not fulfill the "is-a" requirement for inheritance: in other words, it isn't inherently a more specialsed type of the GUIApps class and never will be. Instead, pass a reference from the actual GUI to your class as I suggest above and demonstrate below:

e.g.,
Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
Thanks a lot pete.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setText() not working