This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Update/Set TextField from other class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Update/Set TextField from other class" Watch "Update/Set TextField from other class" New topic
Author

Update/Set TextField from other class

Radek Sta
Greenhorn

Joined: Mar 27, 2012
Posts: 6
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Mar 27, 2012
Posts: 6
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

Joined: Oct 16, 2003
Posts: 1075
    
  10

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...


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Radek Sta
Greenhorn

Joined: Mar 27, 2012
Posts: 6
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

Joined: Mar 27, 2012
Posts: 6
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.
 
 
subject: Update/Set TextField from other class
 
Similar Threads
Basic swing application; class extending JPanel won't update graphics
Critique my beginner basic GUI calculation program and need some clarifications
Applets alignment. cache & POST
setActionCommand() in Applet
Spaces into a JtextField