Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

Getting a reference to an object instantiated by doClick()

Thread view: 
Rhino - 14 Feb 2005 00:48 GMT
How do I select a particular tab of a tabbed pane in a dialog that I display
via doClick()?

My main GUI gives my user the option of creating a new Foo, which has a
variety of attributes. I need my user to confirm the values of all of those
attributes so I want to force them to go to a particular tab of my
Preferences dialog. Once they are there, they can change any attribute
values that they don't like and press 'Save'. Then they return to the main
GUI where they left off and the new Foo is there waiting for them.

I can launch the Preferences dialog programatically via doClick() -

   fileMenu.doClick();
   changePreferencesItem.doClick();

- but I don't see how to select a particular tab of my Preferences dialog,
which is basically a big JTabbedPane. I wanted to do this:

   setSelectedIndex(7);

but I get a compile error because setSelectedIndex refers to my main GUI
class, which is *not* a JTabbedPane. In other words, I need a reference to
the Preferences dialog preceding the method, something like this:

   myPreferencesDialog.setSelectedIndex(7);

I'm not sure how to get a reference to the Preferences dialog. I *could*
instantiate it but the second doClick() has *already* instantiated it so
doing so a second time seems redundant. However, I don't see how to get a
reference to the instance that doClick() created.

Can anyone help me out?

Signature

Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare

Alex Molochnikov - 14 Feb 2005 01:11 GMT
I am not sure what your problem is, since you obviously create a dialog and
therefore must have a reference to it. The doClick() method merely simulates
clicking on a button, which in turn generates an action event. The button
has an action listener attached to it, which is where the dialog could be
instantiated.

From the sound of it, it looks like your design is seriously flawed. Why do
you need to create the dialog twice, instead of reusing the one created the
first time?

Perhaps, posting some of your code could help.

Alex Molochnikov
Gestalt Corporation

> How do I select a particular tab of a tabbed pane in a dialog that I display
> via doClick()?
[quoted text clipped - 37 lines]
> make it so complicated that there are no obvious deficiencies." - C.A.R.
> Hoare
Rhino - 14 Feb 2005 01:35 GMT
Posting my code is something of a problem because of confidentiality
concerns. I could construct an example that illustrates the point without
giving away any secrets but that might take a while to put together. I'd
like to try to clarify my situation verbally first and see if someone can
see the solution.

I agree that my design is not ideal and I'm already thinking of ways to
improve it. However, I still want to pursue this approach just so that I
understand programatic operations, like doClick(), somewhat better, even if
I don't actually implement this approach in the program.

The two doClicks() that I am currently doing are on a JMenu and a JMenuItem
in the JMenuBar of my main GUI. Interrogating those menu items is only going
to give me a reference to the main GUI class, which is a JFrame, not the
Preferences dialog.

I'm really not clear *how* this works but the second doClick() instantiates
my Preferences dialog (actually, JDialog). I'd like to figure out how to get
a reference to that dialog. The API says that doClick() is a method from
AbstractButton and it returns void so I don't see how I can get a reference
to the dialog it launched, as opposed to the JFrame which contained the
control which had the doClick() done. I can SEE the dialog so I know it has
been instantiated but I can't figure out how to get a reference to it.
Surely if an object exists and I created it, there should be a way to get a
reference to it, right?

Rhino

> I am not sure what your problem is, since you obviously create a dialog and
> therefore must have a reference to it. The doClick() method merely simulates
[quoted text clipped - 56 lines]
> > make it so complicated that there are no obvious deficiencies." - C.A.R.
> > Hoare
Alex Molochnikov - 14 Feb 2005 06:54 GMT
I assume that you are the one who wrote the entire code, otherwise you have
to start digging into it. I certainly do not suggest that you should publish
your _entire_ code for the sake of someone analyzing it and finding the
problem. Not many readers will have time or desire for such an exercise.

Instead, I would suggest that you put together a very simplistic test case,
that would replicate the part of program that causes trouble, namely a
JFrame with JMenu in it (populated with the JMenuItems that instantiate the
dialog), and the skeleton code for the dialog. Try it and see if the the
disalog is still instantiated, even though you don't understand the
mechanics of it. If the dialog _does not_ get instantiated, start working
your way up, adding the functionality back to your code, bringing it
gradually to its original state, and see at what point it begins working
again.

If the dialog still gets created, and you are still at loss at what actually
instantiates it, post this stripped-down code.

Without seeing even the skeleton sample of the code, I can only suggest that
your JMenuItems must have an action listener assigned to them somewhere, and
the action listener creates the dialog. Look for the
actionPerformed(ActionEvent e) method in your code.

Alex.

> Posting my code is something of a problem because of confidentiality
> concerns. I could construct an example that illustrates the point without
[quoted text clipped - 93 lines]
> > > make it so complicated that there are no obvious deficiencies." - C.A.R.
> > > Hoare
Rhino - 14 Feb 2005 14:18 GMT
Thanks for your suggestions, Alex, but I've figured out my problem.

Something you said in your second reply got me thinking along the right
track and a minute with the debugger removed the fog: the path the program
takes is exactly the same as if the user clicked the menu options
themselves. I knew that conceptually before but now I've digested it and the
whole thing makes sense now. The simulated 'Change Preferences' click took
me into my logic for handling a 'Change Preferences' click in the GUI's
actionPerformed, just as it should. And there, of course, was my
instantiation of my Preferences dialog and a reference to that dialog.

There never was a problem except that I hadn't really digested the fact that
the doClick() caused me to go to the 'Change Preferences' logic. Somehow, I
had it in my head that some kind of strange magic was going on and the
Preferences dialog was getting created "under the covers" somehow. It was
silly but that's life; sometimes the penny doesn't drop quite as readily as
it should ;-)

Thanks for your suggestions in this matter!

By the way, I've figured out a better way to do what I want to do that
doesn't involve the dubious design that I first conceived. The doClick()
approach really was a shoddy approach, as you noted.

Rhino

> I assume that you are the one who wrote the entire code, otherwise you have
> to start digging into it. I certainly do not suggest that you should publish
[quoted text clipped - 136 lines]
> C.A.R.
> > > > Hoare


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.