• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Update/Set TextField from other class

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 (https://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




 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic