• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

set TextField value from another class

 
Ramona Andreea
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume latdegree is a field? Why have you given it public access rather than using a setLatdegreeText(String text) method?
 
Ramona Andreea
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to all of you. It will never happen again.
Thanks for the help and sorry.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pleased to be able to help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic