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 / April 2005

Tip: Looking for answers? Try searching our database.

Create new object the MenuItem is pressed

Thread view: 
Java - 20 Apr 2005 14:39 GMT
Hello,

I have develop an application, but I am having som problems what
eventshandling when a MenuItem in a Frame is pressed.
/*
menuFileUpdate.addActionListener(new Update());
*/

In this code the Update-object is created when the main-program i
initalized. This i a problem - I would like that the objekt is created at
"pressing time". Do anyone god any suggestion to how this i done?

The update-objekt i an class implementes Runnable. This class is af
singleton with a static instance. What I do right how is to test if the
static object i null, and if is I create a new one, and start the thread.
How ugly is it to set the static instance= null when the job is done - and
thereby create thread-objects if the getInstance() is called?

Do it give any proformace to run the Frame in a thread calling invalidate,
and let the Update run in a the too?

Thanks,

Brian
Piet - 21 Apr 2005 12:27 GMT
Hi Brian
> I have develop an application, but I am having som problems what
> eventshandling when a MenuItem in a Frame is pressed.
[quoted text clipped - 5 lines]
> initalized. This i a problem - I would like that the objekt is created at
> "pressing time". Do anyone god any suggestion to how this i done?
It is clearly NOT possible to create the ActionListener in the moment
when the action it is supposed to listen to is performed.

> The update-objekt i an class implementes Runnable.
Does it also implements ActionListener? Can you compile your code?
In general, you simply have to place everything you want to happen in
the actionPerformed-method of your actionListener.
HTH Piet
Brian - 21 Apr 2005 18:34 GMT
> Hi Brian
>> I have develop an application, but I am having som problems what
[quoted text clipped - 14 lines]
> the actionPerformed-method of your actionListener.
> HTH Piet

Sorry - offcause Update also implements the ActionListener, and got the
metode actionPerformed.

But the problem is that i the main-Frame klasse where i add the
actionListener i write:

menuFileUpdate.addActionListener(new Update());

This means that the Update-object is create when the main-Frame is
initialized - and not when the button with the text "Update" is pressed. Is
it possiable til change that to something smarter?

Brian
Piet - 22 Apr 2005 08:42 GMT
> >> I have develop an application, but I am having som problems what
> >> eventshandling when a MenuItem in a Frame is pressed.
[quoted text clipped - 9 lines]
> >>
> >> The update-objekt i an class implementes Runnable.
1. Can you check your keyboard? It looks to me that in the paragraph
above, we have (among other things that do not facilitate
understanding) several times a simple "i" when an "is" would be more
appropriate IMHO.
2. Without some code, it is very difficult to give you any more
detailed hint. But from what I think I have understood so far, you
have a class that is both an ActionListener and a ThreadStarter (so to
speak) which has an instance of itself as static member. This "static
instance" is basically a thread. You want to separate the
actionListening from the threadStarting functionality.
> But the problem is that i the main-Frame klasse where i add the
> actionListener i write:
[quoted text clipped - 3 lines]
> This means that the Update-object is create when the main-Frame is
> initialized - and not when the button with the text "Update" is pressed.
What means "Update" object? Are you talking about your instance of
"Update" that is usd as an actionListener or do you mean the "static
instance" (i. e. the thread that is apparently to be started when the
button is pressed)?
In general, I can see two possible solutions:
a) Shift the generation of your "static instance"-thread to the
actionPerformed-method. I believe that is what you could be called
"generate the Update-object when the button is pressed."
b) Rename your ActionListener from "Update" to "UpdateCreator" and
shift your threadStarting logic to a new class called "Update". Create
a new instance of "Update" in the actionPerformed-method of
"UpdateCreator". Maybe that solves your problem.
HTH, Piet


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.