• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

setText() not working

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot pete.
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic