It's not a secret anymore!
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 Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Update/Set TextField from other class" Watch "Update/Set TextField from other class" New topic

Update/Set TextField from other class

Radek Sta

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 (

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

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

Joined: Oct 16, 2003
Posts: 1101

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

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

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.
I agree. Here's the link:
subject: Update/Set TextField from other class
It's not a secret anymore!