how to reference textbox in a different jPanel?

R

Rpng123

Hello,

I have a jPanel (grid layout) jA1 which contains another jPanel jB1. jA1 also
contains a textbox, jtfResult. When an action is performed in jB1 I want to
set text in jtfResult which is in jA1. That is where my problem is. Don't
know how to reference jtfResult from jB1. I do have a standalone class (cls1)
which contains properties that get set from jB1 - mousePressed event. Could I
reference jtfResult from here? Do I need to do something in Frame1? jA1
already references cls1. In Frame1 I have jA1.setCls(new cls());

Any suggestions appreciated how to reference jtfResult in jA1 from jB1
(mousePressed Event).

Thanks,
Rich
 
R

Rpng123

Incase anyone care, I found a solution. Here is what I did

In jA1 I implemented PropertyChangeListener. I guess the PropertyChangeSupport
class keeps track of all the properties in a project. So if any property
changes, the PropertyChangeEvent fires. So I added this method of
PropertyChangeSupport in the jB1 bean:

public void propertyChange(PropertyChangeEvent evt){
jtfResult.setText((Integer)evt.getNewValue()).intValue() + "");
}

Now jtfResult updates continuously.
 
A

Andrew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rpng123 wrote:
| Hello,
|
| I have a jPanel (grid layout) jA1 which contains another jPanel jB1.
jA1 also
| contains a textbox, jtfResult. When an action is performed in jB1 I
want to
| set text in jtfResult which is in jA1. That is where my problem is.
Don't
| know how to reference jtfResult from jB1. I do have a standalone
class (cls1)
| which contains properties that get set from jB1 - mousePressed event.
Could I
| reference jtfResult from here? Do I need to do something in Frame1? jA1
| already references cls1. In Frame1 I have jA1.setCls(new cls());
|
| Any suggestions appreciated how to reference jtfResult in jA1 from jB1
| (mousePressed Event).
|
| Thanks,
| Rich
|
|


The way I get around this (and I'm not a swing expert) is to simply
declare the textfield as an attribute of the class. You instantiate the
textfield when you create the window, add it to whatever panel you like
and so long as you have a reference to the frame/panel you can reference
the textfield.
I'm not sure this is the best way, but I'm yet to read different,
perhaps someone who is an expert in swing can enlighten us to a better
way if possible. Otherwise go with this :)

- --
[A n d r e w]
- ------|------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/he6sx4nopx2ZsVURAjd3AKDUQDbN9CV+PVYffEA3Ar7ld1moWACaA1jp
SdH2eJbeU7jUnNaT2X+Xxl4=
=6ViI
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top