javax.swing.timer
I have a button and a timer in a gui program. I want the timer to trigger events and the button to trigger different events.
The button works fine but I am having trouble adding the timer event to the action listener.
Here is the code I am trying in the Action Listener. I am expecting the code in the catch to run when the timer trigers. I am using a try catch handler because the e.getActionCommand needs an action command from the timer and I do not know what it is, it appears to be "null".
The Timer does trigger the catch and so I know that the Action Performed code is trying to run for the timer.
publicvoid actionPerformed(ActionEvent e){
try{
System.out.println("e is " + e);
if (e.getActionCommand().equals("Exit")){
this.myExit();
}
}
catch(Exception excp){
j.restart();
// j is my Timer
System.out.println(excp +"excp " + e);
}
}
Where am I going wrong?
[1465 byte] By [
nerak99a] at [2007-11-26 14:26:57]

what's supposed to happen?
Simple, don't use the getActionCommand, use getSource.
class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if(e.getSource() == timer)
{
// Do something that is trigger by the timer
}
else if (e.getSource() == btn)
{
//do something triggered by the button
}
}
}
~Tim
It sounds like you want clicking the button and the timer ticking to trigger *different* reactions. So why funnel them through the same ActionListener? Write two of them.
As the code is written, j.restart. However, once the code is working a JTextField will be updated.
OK, so how do I implement two action listeners in the same class?
Or is it better to implement a class for each ActionListener?
In examples I have seen, it appears to be common practice to funnel all the the buttons in a GUI through the same ActionListener and distinguish between them with if statements.
ONE CLASS CAN HAVE ONLY ONE UNIQUE ACTION LISTENER
YOU HAVE TO HAVE ONE MORE CLASS I GUESS..IF YOU REALLY WANT TO USE TWO ACTION LISTENERS....
How can you use two action listeners in same class..think about it..when you press button.. control should go to only one actionListener is'nt it..?
Thank you KayDE.
Can I humbly point out that this is a "new to java" site and consequently there will be many occasions where naive or even stupid questions are asked. It is important not to intimidate people who are asking questions.
Using capitals like you have here (IMHO) is inappropriate.
"One class can only have one unique ActionListener" is what I thought was the case as you can see from what I wrote about examples I have seen.
What I was interested in is whether (from an elegance point of view) in general,
ActionListeners should always be in their own class rather than seeing the ActionListener in the same class as the button (or whatever) as is often the case.
BTW Don't you think that Gnome is the best desktop system for linux?
> ONE CLASS CAN HAVE ONLY ONE UNIQUE ACTION LISTENER
>
> YOU HAVE TO HAVE ONE MORE CLASS I GUESS..IF YOU
> REALLY WANT TO USE TWO ACTION LISTENERS....
>
> How can you use two action listeners in same
> class..think about it..when you press button..
> control should go to only one actionListener is'nt
> it..?
utterly wrong on several levels
1) classes don't have listeners at all. instances of classes do
2) you mean the following code won't work? really? try it
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ManyListeners {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300,200);
JButton button = new JButton("press me");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.err.println("I am the first action listener");
}});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.err.println("I am the second action listener");
}});
frame.getContentPane().add(button);
frame.setVisible(true);
}
}
both listeners are invoked. why else is the method called addActionListener, and not setActionListener?
think about it
Mmmm Interesting tone in this reply as well.
Why use
> utterly wrong on several levels.
when you could have said something like
Interesting point, but (perhaps pedantically) of course object have listeners rather than classes.
The following code illustrates my point rather neatly. It shows how you can have actions that are always performed alongside action that are only performed for some events with the code in the same class file.
>
> 1) classes don't have listeners at all. instances of
> classes do
> 2) you mean the following code won't work? really?
> try it
>
> > import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
>
> import javax.swing.JButton;
> import javax.swing.JFrame;
>
> public class ManyListeners {
>
> public static void main(String[] args) {
> JFrame frame = new JFrame();
> frame.setSize(300,200);
> JButton button = new JButton("press me");
> button.addActionListener(new ActionListener() {
>
> public void actionPerformed(ActionEvent e) {
> System.err.println("I am the first action
> tion listener");
> }});
> button.addActionListener(new ActionListener() {
>
> public void actionPerformed(ActionEvent e) {
> System.err.println("I am the second action
> tion listener");
> }});
> frame.getContentPane().add(button);
> frame.setVisible(true);
> }
>
> }
>
>
> both listeners are invoked. why else is the method
> called addActionListener, and not setActionListener?
>
> think about it
Why bully people? Maybe I am over sensitive :-)
I hardly think it's bullying. but anyway, that's just my style. I don't concern myself overly-much with being inoffensive (not that I go out of my way to offend) or polite, either online or in real life. the guy boldly proclaimed something to be true which was false.
my motivation was to correct the misinformation for the benefit of others, not to make friends :-)
I have watched this forum for some time but have been prevented from posting questions because of the bullying tone from exports who
don't concern themselves with being inoffensive
An argument delivered succinctly and politely is much more powerful than a triumphant howl when you spot an error from someone else. It also improves the standard of debate.
If you want to abuse people that make errors then go to alt.abuse.victims or some such place. This is a beginners forum, not the Colosseum.
> I have watched this forum for some time but have been
> prevented from posting questions because of the
> bullying tone from exports who
how is it bullying?
> don't concern themselves with being
> inoffensive
>
> An argument delivered succinctly and politely is much
> more powerful than a triumphant howl when you spot an
> error from someone else. It also improves the
> standard of debate.
how can one debate something that is factually wrong?
> If you want to abuse people that make errors then go
> to alt.abuse.victims or some such place. This is a
> beginners forum, not the Colosseum.
again, how is it abusive?
> my motivation was to correct the misinformation for
> the benefit of others, not to make friends :-)
My only suggestion along this line is that if you're looking to get your message across as effectively as possible (especially for the benefit of others), you're likely to have better success on average if you do concern yourself with being polite. No one will force you to be polite, but it's been my experience that it makes for much more pleasant reading.
:o)
~
> > my motivation was to correct the misinformation
> for
> > the benefit of others, not to make friends :-)
>
> My only suggestion along this line is that if you're
> looking to get your message across as effectively as
> possible (especially for the benefit of others),
> you're likely to have better success on average if
> you do concern yourself with being polite. No
> one will force you to be polite, but it's been my
> experience that it makes for much more pleasant
> reading.
>
> :o)
>
> ~
something of an over-reaction to what was, after all, only a mildly blunt post :-) the guy posted something plainly wrong, in a manner that suggested it was absolute fact, and that it should be plainly obvious it was absolute fact. and he shouted it
I'd rather just get my point across than spend too much time worrying about whether the tone is exactly right. funnily, it's only online that people take offence to my plain-speaking tone
> I'd rather just get my point across than spend too much time worrying about whether the tone is exactly right.
And that's certainly your prerogative. However, if you're really looking for effectiveness in getting your point across, it might be worthwhile to further consider how your message will be perceived, and that the way in which you are trying to get your point across may hinder, rather than help, you in your goal.
I'm not saying you're doing anything wrong; just some things to consider, if you haven't already.
Cheers! :o)
~
> I have watched this forum for some time but have been
> prevented from posting questions because of the
> bullying tone from exports who
Maybe you should get a few polite imports.
> An argument delivered succinctly and politely is much
> more powerful than a triumphant howl when you spot an
> error from someone else. It also improves the
> standard of debate.
Politeness is personal judgement.
> If you want to abuse people that make errors then go
> to alt.abuse.victims or some such place. This is a
> beginners forum, not the Colosseum.
Professor, if you intend to preach why don't you start posting something that is actually related to Java?
> Politeness is personal judgement.
I'd say it's more cultural than personal.
> Professor, if you intend to preach why don't you start posting something that is actually related to Java?
It's related to this forum, which I happen to think (contrary to the "moderators") is fair game for discussion. :o)
~
> Mmmm Interesting tone in this reply as well.If you are interested in tones rather than the reply, you are misplacing a few priorities.> Why bully people? Maybe I am over sensitive :-)You obviously never had a bully in your school.
> > Politeness is personal judgement.
>
> I'd say it's more cultural than
> personal.
I don't say that I am always right but it is personal in my perception. Of course, perceptions are often biased. :)
> > Professor, if you intend to preach why don't you
> start posting something that is actually related to
> Java?
>
> It's related to this forum, which I happen to think
> (contrary to the "moderators") is fair game for
> discussion. :o)
It's so ironic that the only post someone makes is to tell someone that he/she is rude. I guess "setting an example" is something that doesn't work any more. Someone posting only to criticize criticism is ironic.
> If you are interested in tones rather than the reply,
> you are misplacing a few priorities.
I think there's a middle ground here. I think petitioners should be patient with responses, looking through the noise intrinsic to a public, unmoderated forum for the good information that can be filtered and gleaned. I also think respondents should be patient, and try to provide information for the benefit of the community and understand that bluntness is often perceived as arrogance, which contributes to the noise moreso than the signal.
But that's just my opinion. ;o)
~
> > If you are interested in tones rather than the
> reply,
> > you are misplacing a few priorities.
>
> I think there's a middle ground here. I think
> petitioners should be patient with responses, looking
> through the noise intrinsic to a public, unmoderated
> forum for the good information that can be filtered
> and gleaned. I also think respondents should be
> patient, and try to provide information for the
> benefit of the community and understand that
> bluntness is often perceived as arrogance, which
> contributes to the noise moreso than the signal.
>
> But that's just my opinion. ;o)
>
> ~
I agree with the patience part.
That is the point is it not. The purpose of this forum is to act as a guide to new users and your primary motivation is not to get your point across but to help people.
There are plenty of forums where the cut and thrust of debate is more combative if that is your bag!
The error regarding class - object was hardly a major error. Many people in these forums are careless about the fact that their code applies to a object rather than a class.
Your other point was pretty valid but then again, hardly an earth shattering "gotcha".
> I'd rather just get my point across than spend too
> much time worrying about whether the tone is exactly
> right.
> > I'd rather just get my point across than spend
> too much time worrying about whether the tone is
> exactly right.
>
> And that's certainly your prerogative. However, if
> you're really looking for effectiveness in getting
> your point across, it might be worthwhile to further
> consider how your message will be perceived,
> and that the way in which you are trying to get your
> point across may hinder, rather than help, you in
> your goal.
>
> I'm not saying you're doing anything wrong; just some
> things to consider, if you haven't already.
>
> Cheers! :o)
>
> ~
my attitude isn't some haphazard accident, it's very carefully considered. the foundation is "do as you would be done by", and if I post something inaccurate, I fully expect to be treated the exact same way. you won't see any hard-done-by whingeing when it happens, either. believe it or not, every manager I've ever worked under has commented (positively) on my diplomacy and tact. I just don't go out of my way to always be diplomatic and tactful, I find it often gets in the way
your comments are welcome, though :-)
> That is the point is it not. The purpose of this
> forum is to act as a guide to new users and your
> primary motivation is not to get your point across
> but to help people.
Most of the people come here to amuse themselves. That's at least what I heard. :o
> That is the point is it not. The purpose of this
> forum is to act as a guide to new users
yes, it is. and as such, it's important that misinformation is clearly pointed out as such. woolly dancing around the issue can get in the way of that. I judged that this was such a a situation
> and your
> primary motivation is not to get your point across
> but to help people.
it's nobody's place but mine to dictate my motivation
> There are plenty of forums where the cut and thrust
> of debate is more combative if that is your bag!
mountains? molehills? I didn't exactly shout the guy down
> The error regarding class - object was hardly a major
> error. Many people in these forums are careless about
> the fact that their code applies to a object rather
> than a class.
right, and they shouldn't be. the difference - while minor to many - needs to be recognized and acknowledged early, before the wrong information is set in stone in some newbie's head
> Your other point was pretty valid but then again,
> hardly an earth shattering "gotcha".
and not treated as an earth-shattering gotcha
As a finnish-native-speaker and regular java forum visitor, I do have to admire the level of quality of the english language text people have posted in this thread.I just wonder what it would have looked like if it had been posted completely in sms-talk-english?Cheers
> As a finnish-native-speaker and regular java forum
> visitor, I do have to admire the level of quality of
> the english language text people have posted in this
> thread.
*sarcasmatron scan returns negative * :-)
> I just wonder what it would have looked like if it
> had been posted completely in sms-talk-english?
>
like a noobs question, probably :-)
> As a finnish-native-speaker and regular java forum
> visitor, I do have to admire the level of quality of
> the english language text people have posted in this
> thread.
>
> I just wonder what it would have looked like if it
> had been posted completely in sms-talk-english?
>
OP: i hv 1 dbt abt tmrs
Yawmark: Please make the extra effort to write out words such as "please" and "your". The extra keystrokes won't cost much in the way of time, and the enhanced clarity will be appreciated by those communicating on a forum with international readership.
OP: do u hv codes
Others: Ahhhh my eyes my eyes!
OP: srry i ddnt get u
LOL. I've been busted!:o)~
> OP: i hv 1 dbt abt tmrs
>
> Yawmark: Please make the extra effort to write out
> words such as "please" and "your". The extra
> keystrokes won't cost much in the way of time, and
> the enhanced clarity will be appreciated by those
> communicating on a forum with international
> readership.
>
> OP: do u hv codes
>
> Others: Ahhhh my eyes my eyes!
>
> OP: srry i ddnt get u
javawonderexpert: how 2 use timers plzzz. expln immdetily wth codez
isn't there a frightening trend toward people re-posting questions right after the answers?!
>OP: do u hv codesPairing that with a title containing "URGENT1!!!1!" makes me sayHelvetin perkeleJukka
> >OP: do u hv codes> > Pairing that with a title containing "URGENT1!!!1!"> makes me say> > Helvetin perkele> > JukkaI feel like I'm in Finnish 101 today :-)Helvetin, I do get!