This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Java event handling? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Java event handling?" Watch "Java event handling?" New topic
Author

Java event handling?

Adam Chalkley
Greenhorn

Joined: Nov 27, 2013
Posts: 10
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

Joined: Aug 07, 2007
Posts: 2166
    
  47
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

Joined: Aug 07, 2007
Posts: 2166
    
  47
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

Joined: Oct 13, 2005
Posts: 37926
    
  22
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
Greenhorn

Joined: Nov 27, 2013
Posts: 10
Thanks Tony,Thanks campbell,

but still a little stuck,how can I actually make the event.getSource work,thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
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
Greenhorn

Joined: Nov 27, 2013
Posts: 10
Thanks =) I'll give that a try
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java event handling?
 
Similar Threads
Need help creating classes
Problem with Sort Method
The method showMessageDialog(Component, Object) in the type JOptionPane is not applic
Problem with Double!!!!!!
Calculations Based on Selected Radio Button