Results 1 to 12 of 12
  1. #1
    Zorobay is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Question Writing a MouseWheel listener problem

    Hi!

    Im trying to write a MouseWheel listener but it's not working at all. It's a program that displays RBG values with JLabels and a JPane that displays the color. You are supposed to be able to scroll in the panes to increase or decrease the values, but nothing happens!? I'm a Java noob so forgive me for any obvious errors =(

    Java Code:
    public class panelHandler extends MouseAdapter{
    		public void mouseClicked(MouseEvent event){
    			if(event.getComponent() == panelR){ //Event for Red panel
    				if(event.getButton() == event.BUTTON1) //If left click
    					R++; //add 1 to red
    				else //if right click
    					R--;
    				panelR.setBackground(new Color(R, 0, 0)); //display
    				statusR.setText("G Value: " + R);
    			}
    			else if(event.getComponent() == panelG){ //Event for Green panel
    				if(event.getButton() == event.BUTTON1)
    					G++;	
    				else 
    					G--;			
    				panelG.setBackground(new Color(0,G,0));
    				statusG.setText("G Value: " + G);	
    			}
    			else{
    				if(event.getButton() == event.BUTTON1) //Event for Blue panel
    					B++;
    				else
    					B--;
    				panelB.setBackground(new Color(0,0,B));
    				statusB.setText("B Value: " + B);
    				
    			}
    			statusRGB.setText("RGB Value: " + R + ", " + G + ", " + B);
    			panelRGB.setBackground(new Color(R, G, B));
    		}
    		public void MouseWheelMoved(MouseWheelEvent event){
    			System.out.println("mouse scrolled");
    			int i = event.getWheelRotation();
    			if(event.getComponent() == panelR) //Event for Red panel
    				R += i;
    			if(event.getComponent() == panelG) //Event for Green panel
    				G += i;
    			else //Event for Blue panel
    				B += i;
    			panelRGB.setBackground(new Color(R,G,B));
    			statusRGB.setText("RGB Value: " + R + ", " + G + ", " + B);
    			
    			
    		}
    	}
    The mouse click part works as it should :P

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Add an @Override statement before the methods to be sure you have coded them correctly

  3. #3
    Zorobay is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Well I did that (even though i don't know what @Override does) and it gave me this error:

    The method MouseWheelMoved(MouseWheelEvent) of type GUI.panelHandler must override or implement a supertype method

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    it gave me this error:
    the compiler doesn't think your method is an override. Double check it to make sure of spelling etc

  5. #5
    Zorobay is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Well I cant see any typos. But is the method okay being in the same class as my MouseListener?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    The compiler is usually right. Check again. Besides spelling, check case.

    is the method okay being in the same class as my MouseListener
    Yes, you can have a class implement more than one interface.
    Usually not done but legal.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    Usually not done
    Not so. The very first class in the Java API (by alphabetical listing) implements 5 interfaces.

    db

  8. #8
    Zorobay is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    I really cant find anything that isnt right

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    I guess I was thinking of something else. Instead of adding a bunch of unrelated listener interfaces to a singe class (to keep from having more than one class to work with), I thought it'd be better to have one class for each listener.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    Quote Originally Posted by Zorobay View Post
    I really cant find anything that isnt right
    To you, java may be the same as Java, but to a Java compiler, it isn't.

    db

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    I guess I was thinking of something else. Instead of adding a bunch of unrelated listener interfaces to a singe class (to keep from having more than one class to work with), I thought it'd be better to have one class for each listener.
    I agree with you, with the addition that event listeners should mostly be coded as anonymous inner classes. I disagreed with the notion that a class should not implement multiple interfaces, or that it's 'usually not done'.

    db

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    I really cant find anything that isnt right
    Let me repeat an earlier post with a slight addition:
    The compiler is usually right. Check again. Besides spelling, check case.

Similar Threads

  1. Problem with key listener
    By Mikelmao in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 02:02 PM
  2. JEditorPane + MouseWheel
    By Moncleared in forum New To Java
    Replies: 2
    Last Post: 10-09-2010, 01:27 AM
  3. Replies: 7
    Last Post: 12-29-2009, 01:48 PM
  4. Writing a file listener
    By matzahboy in forum New To Java
    Replies: 8
    Last Post: 04-09-2009, 03:41 PM
  5. #key listener problem
    By mij1_7 in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 09:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •