Create new object the MenuItem is pressed  
Author Message
Java





PostPosted: 2005-4-20 21:39:00 Top

java-programmer, Create new object the MenuItem is pressed 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


 
pit.grinja





PostPosted: 2005-4-21 19:28:00 Top

java-programmer >> Create new object the MenuItem is pressed Hi Brian
> 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?
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





PostPosted: 2005-4-22 1:35:00 Top

java-programmer >> Create new object the MenuItem is pressed "Piet" <email***@***.com> wrote in message
news:email***@***.com...
> Hi Brian
>> 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?
> 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

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






 
 
pit.grinja





PostPosted: 2005-4-22 15:43:00 Top

java-programmer >> Create new object the MenuItem is pressed > >> 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?
> > 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.
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:
>
> 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.
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