Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setText() not working

 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot pete.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic