File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes set TextField value from another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "set TextField value from another class" Watch "set TextField value from another class" New topic
Author

set TextField value from another class

Ramona Andreea
Greenhorn

Joined: Nov 22, 2008
Posts: 14
Hy,

I have a big problem. I have an application with many buttons and windows. On one of this windows (which extends Dialog class) a have a textField. I want to set it's value from another class B. If in class B I write tf.setText("test") and on the next line I write System.out.println(tf.getText) "test" is displayed, but in the textField the value doesn't change.

I need some help.

Thanks,
Ramona
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
Welcome to JavaRanch

Please check that the "tf" reference in class B points to the same object as the TextField in the Dialog.
Ramona Andreea
Greenhorn

Joined: Nov 22, 2008
Posts: 14
Thanks,

I don't think that this is the problem. In class B I wrote:

dw.latdegree.setText("test");
System.out.println(dw.latdegree.getText());

And in the Dialog class the textField is initializeed like this:

latdegree=new TextField("");
latdegree.setForeground(Color.black);
latdegree.setFont(f);
latdegree.requestFocus();
add(latdegree);
latdegree.setBounds(75,135,26,20);
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
I presume latdegree is a field? Why have you given it public access rather than using a setLatdegreeText(String text) method?
Ramona Andreea
Greenhorn

Joined: Nov 22, 2008
Posts: 14
I put this method in dialog class:

public void setLatdegreeText(String text){
latdegree.setText(text);
}

And this code in class B (which is in other package):

dw.setLatdegreeText("test");

But in the textField doesn't appear value "test".

The idee of my program is to read some values from a file and display them in textFields in other window (extends dialog).
[ November 22, 2008: Message edited by: Ramona Andreea ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
Try adding a line to that method, after the setText() call.

System.out.println("TextField text changed to " + text);//test

Also try adding a repaint(); call again after setText().
Ramona Andreea
Greenhorn

Joined: Nov 22, 2008
Posts: 14
I have already done this. The System.out.println prints the new value but the textField doesn't.

I don't know what i'm doing wrong.

Thanks,
Ramona Andreea
Greenhorn

Joined: Nov 22, 2008
Posts: 14
I solved the problem. It was another instance of the DataWindow - when I push the button which opens that window. So I was modifying one window and displaying another.


Thanks
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.

Pete

java-forum post
[ November 22, 2008: Message edited by: pete stein ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
Thank you for noticing, Pete, and I am intrigued to see one suggestion virtually identical to one of mine.

At least you said sorry, Ramona: the suggestions in this FAQ should be applied to both websites.
Ramona Andreea
Greenhorn

Joined: Nov 22, 2008
Posts: 14
Sorry to all of you. It will never happen again.
Thanks for the help and sorry.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
Pleased to be able to help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: set TextField value from another class