Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java event handling?

 
Adam Chalkley
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I've been reading tutorials and even books and so far from what I've read I'm doing the right code yet I'm getting an error.can someone please help me with this,this is very discouraging.

only thing you have to really read is the end of the code
here is my code,everything works until I add the 'if(event.getSource()==button)' part of the code,what could be possibly wrong it looks like the correct syntax from what I've read,if someone can help me out with this really discouraging situation I would be more than thankfull,thanks alot

 
Tony Docherty
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The actionPerformed() method is being passed an event which is referenced by 'e', your if statement is using 'event' which is a totally different variable.
 
Tony Docherty
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just noticed that also your 'button' is variable is declared as local to the mess() constructor so isn't even visible in the actionPerformed method. In fact this code won't even compile, are you sure it's the code you are running?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48952
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote: . . .

public class mess extends JFrame implements ActionListener {
. . .
that might be syntax which works, and which you find in many books and the Java Tutorials, but I think it is non‑OO design. Create a class which implements the Action Listener interface. You can have it as a private inner class, a public top‑level class or an anonymous class. The link I gave you suggests a class which handles such an event.

You will doubtless know there are scores of different kinds of event; if you move a Swing display across the screen, you get many events occurring per second, but you ignore most of them by not listening for them. I suggest you try the colour listener class, in this sort of code:-Never mind about the rest of the code. Just a frame with a panel and that button on. No need to confuse things with lots of other components or code; you will have enough difficulty remembering which colour it turns things

This looks like a GUI‑related problem, so I shall move the discussion to our GUIs forum. I shall also try adding code tags to your post; they should make it look a lot better.
 
Adam Chalkley
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tony,Thanks campbell,

but still a little stuck,how can I actually make the event.getSource work,thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 48952
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote: . . . how can I actually make the event.getSource work,thanks
You don't.

You add a different Listener to each control.Or you find somebody colour‑prejudiced who won't mislead you with the wrong colours.

If you have to controls which do the same thing
 
Adam Chalkley
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks =) I'll give that a try
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic