Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Update/Set TextField from other class

 
Radek Sta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i found this site while searching for solution on google. I've found topic with question very similar to mine, however it didn't help me (http://www.coderanch.com/t/416900/java/java/set-TextField-value-another-class).

Ok, so I've got three classes:

1 (Display)- with TextField, that extends JPanel
2 (Cancel) - with Button, that extends JPanel
3 - with final GUI, which extends JFrame

I run my program from "third" class, i see TextField and button. After I click Button i want my TextField to be updated. I made this ActionListener


It invokes method setResult, which I defined in first - Display class









Console shows me "0.0" as I wanted, but value in TextField doesn't change. I'm completely out of ideas how to solve it. I tried to run action listener like



but it didn't help neither.

I hope that Someone will help me with this. I've tried to solve this for 3 hours :S




 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
possibly duplicate declarations:

1) local variable added to the panel (the one you see on the screen)
2) class variable - this is the one referenced by the listener,
and is not on the screen.

 
Radek Sta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but I completely out of ideas how I can solve it. I know that I should do reference between class with action listener and class which extends JFrame. Or maybe I am wrong?

This is the class with action listener




This one creates TextField i want to update




And the last one, the one that, which connects two panels and builds GUI




I know I can do this all in one class, because it's very simple calculator, but now I'm just curious how to solve this problem and get It work.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radek Sta wrote:



Why are you creating the Display instance everytime? This has no relation with the Display instance that is actually displayed in the GUI. Like you have mentioned, you should pass the reference of the correct Display instance to the Cancel class and then use it inside the ActionListener...
 
Radek Sta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've created new instance in Cancel class, because I've needed to invoke setResult method, which is in Display class. Thank You for indicating this mistake, when I'll be back from work I'll try different approach to pass this arguments. I hope I'll find solution.
 
Radek Sta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I add two methods to Cancel Class:



also modified ActionListener a little bit



and Calculator GUI, where I've added one line



Now, there is only one Display instance and everything's working fine. I'm posting it, because maybe someone will have same problem and find this topic usefull.

Once again - thank You guys for help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic