Results 1 to 14 of 14
  1. #1
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default Simple mouse event question

    Hi i have a simple question about mouse event.

    At the moment i have implemented the MouseListener and the 5 compulsary methods to go along with it.
    The method that im interested in for this example is the mouseEntered method.

    i have two objects calling to the addMouseListener, a label and a JPanel.

    Basically what i want to do is run different code depending on which of the two obove objects the mouse enters.

    So far i can write code which will run if either of the two objects gets entered with the mouse however i dont know run code if the label gets entered and run a different code if the JPanel gets entered.

    If tried using an if statement within the mouseEntered method ie

    if (event.getSource() = "nameOfLabel") //do something
    else if (event.getSource() = "nameOfJPanel") //do something else

    but it seems the getSource() didnt work.

    Any ideas?


    i dont fancy calling a new Anonymous Class for each of the two objects because that will mean writing out 5 mouse methods twice and i think there must be a far easier/simple way!

    thanks for reading.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Are those two lines copied and pasted from your source? If so, the problem may be in th fact that you used a single = sign.

    Instead, try:
    if (event.getSource() == "nameOfLabel")
    or
    if (event.getSource().equals("nameOfLabel"))

    Apologies if I'm way off of the problem, but I figured this was worth mentioning.

  3. #3
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by Zack View Post
    Apologies if I'm way off of the problem, but I figured this was worth mentioning.
    For what it's worth, I think that you're on the money here. To the original poster, if Zack's suggestion doesn't help you fix the problem, then you may wish to create a very simple Swing application that reproduces your problem, has no extraneous code unrelated to the problem and is compilable and runnable so that we can test and alter the offending code ourselves. Also, if the actions of the mouse listener is vastly different for the two components, you're probably better off creating separate listeners for each. Also, if you haven't yet discovered it, the convenience class, MouseAdapter, a wrapper class that implements MouseListener, MouseMotionListener, and MouseWheelListener, can let you create MouseListeners that only declare the methods that you need.

    Suerte!
    Last edited by curmudgeon; 06-20-2010 at 05:05 AM.

  4. #4
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Thnaks for quick replies.
    To Zack, no this is not from my source, however my source does contain a a double equals sign.
    i did a System.out.println(e.getSource()); before making this thread and it came up with some super long thingy which basically wasnt the name of the object, so i figured getSource() wasnt the solution however i think getSource works with actionListener and itemListener but not with mouse events.


    To curmudgeon, thanks for suggesting MouseAdapter, i will use this in future once i've mastered the basics.


    edit

    ill try that getsource.equals method, didnt see u write that before. brb

    edit

    no joy
    Last edited by alacn; 06-20-2010 at 05:18 AM.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  5. #5
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    heres the code. This program is purely just to test mouse event

    PHP Code:
    package fresh;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.ArrayList;
    
    
    public class GUI extends JFrame{
    
        private JPanel mousePanel;
        private JLabel statusBar;
    
        public GUI(){
            super("GUI");
            //setLayout(new FlowLayout());
    
            mousePanel = new JPanel();
            mousePanel.setBackground(Color.white);
            add(mousePanel,BorderLayout.CENTER);
    
            statusBar = new JLabel("defualt");
            add(statusBar,BorderLayout.SOUTH);
    
    
            Handlerclass handler =  new Handlerclass();
            mousePanel.addMouseListener(handler);
            statusBar.addMouseListener(handler);
         //   mousePanel.addMouseMotionListener(handler);
        }
    
        private class Handlerclass implements MouseListener{
            public void mouseClicked(MouseEvent e){
                statusBar.setText(String.format("%d, %d", e.getX(), e.getY() ));
            }
            public void mousePressed(MouseEvent e){
                statusBar.setText("you pressed down");
            }
            public void mouseReleased(MouseEvent e){
                statusBar.setText("you released");
            }
            public void mouseEntered(MouseEvent e){
                statusBar.setText("you entered the area");
                mousePanel.setBackground(Color.red);
    
                if(e.getSource().equals("statusBar"))
                    statusBar.setText("you entered the label, naughty!");
            }
            public void mouseExited(MouseEvent e){
               // statusBar.setText("you exited");
                mousePanel.setBackground(Color.white);
            }
        }
    }
    just to explain the code a bit, if u look at the mouseEntered bit. it sets the label to show "you entered the area", regardless of which object the mouse is entered, however if its the label that the mouse enters, then it should say "you entered the label, naughty", but im just using the wrong if statment i think.

    btw please exuse my lazyness for not doing and if else statment for the two objects.!
    Last edited by alacn; 06-20-2010 at 05:27 AM.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    The source is not a String but a component. So don't use Strings within your equals methods -- rather use the component variables:

    Java Code:
          // if (e.getSource().equals("statusBar"))  // **** incorrect
          if (e.getSource().equals(statusBar))       // **** correct
            statusBar.setText("you entered the label, naughty!");
    Last edited by curmudgeon; 06-20-2010 at 05:29 AM.

  7. #7
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    yeah but if i use the components themself then how would i then tell the differences between two labels?
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by alacn View Post
    yeah but if i use the components themself then how would i then tell the differences between two labels?
    ???

    What two labels? I only see one JLabel in your code above.

  9. #9
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    ???

    What two labels? I only see one JLabel in your code above.
    the code above is just an example so i can become more familiar with mouseevents. im looking for a techique of telling the difference between any two objects even ones which are of the same component.

    Sorry for any confusion.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  10. #10
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by alacn View Post
    the code above is just an example so i can become more familiar with mouseevents. im looking for a method of telling the difference between any two objects even ones which are of the same component.
    Then either the variables in question should be within the scope of the listener, or (and I strongly recommend), use different listeners for different components.

  11. #11
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    For example:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class GUI extends JFrame {
    
      private JPanel mousePanel;
      private JLabel statusBar;
    
      public GUI() {
        super("GUI");
        // setLayout(new FlowLayout());
    
        mousePanel = new JPanel();
        mousePanel.setPreferredSize(new Dimension(400, 300));
        mousePanel.setBackground(Color.white);
        add(mousePanel, BorderLayout.CENTER);
    
        statusBar = new JLabel("defualt");
        add(statusBar, BorderLayout.SOUTH);
    
        Handlerclass handler = new Handlerclass();
        mousePanel.addMouseListener(handler);
        statusBar.addMouseListener(handler);
        mousePanel.addMouseMotionListener(handler);
      }
    
      private class Handlerclass extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
          if (e.getSource().equals(mousePanel)) {
            statusBar.setText(String.format("%d, %d", e.getX(), e.getY()));
          }
        }
    
        public void mouseEntered(MouseEvent e) {
          if (e.getSource().equals(mousePanel)) {
            statusBar.setText("you entered the area");
            mousePanel.setBackground(Color.red);
          }
    
          if (e.getSource().equals(statusBar)) {
            statusBar.setText("you entered the label, naughty!");
          }
        }
        
        public void mouseMoved(MouseEvent e) {
          if (e.getSource().equals(mousePanel)) {
            statusBar.setText(String.format("you entered the area, %d, %d", e.getX(), e.getY()));
          }
        }
    
        public void mouseExited(MouseEvent e) {
          statusBar.setText("");
          mousePanel.setBackground(Color.white);
        }
      }
      
      private static void createAndShowUI() {
        JFrame frame = new GUI();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }

  12. #12
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    thanks for code, seems MouseAdapter get the getSource working and is defo the way forward and saves alot of messing around.

    thanks for upload and help.

    Ill play around with your code when i wake up tommorow, ive been up all night practicing and very tired now. :)

    thanks again
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  13. #13
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    You're welcome. :)

  14. #14
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    oh man im so stupid, i just tried out your code and then released why getSource() didnt work with mouseListener. its because i had the name of the component statusBar in quotes rather than just writing it normally.
    I Wrote it without quotes and it worked with both mouseListener and MouseAdapter (for anyone else reading this in a few years time lol).
    Although i do like mouse adapter alot, cas it means i can use anaymouse classes very smoothly now. ie

    PHP Code:
            statusBar.addMouseListener(
                    new MouseAdapter(){
                        public void mouseEntered(MouseEvent e){
                            statusBar.setText("entered label");
                        }
                    }
            );
    Teaching myself java so that i can eventually join the industry! Started in June 2010

Similar Threads

  1. Mouse event filtering?
    By Taiko in forum AWT / Swing
    Replies: 12
    Last Post: 08-10-2011, 02:00 PM
  2. some simple question?
    By jperson in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 05:32 PM
  3. [SOLVED] Mouse event in JTabbed Pane
    By javanewbie in forum AWT / Swing
    Replies: 6
    Last Post: 06-10-2009, 08:50 AM
  4. Mouse Event + Image Thresholding
    By ojmayolebron in forum AWT / Swing
    Replies: 0
    Last Post: 03-27-2009, 12:17 AM
  5. Exception Event question
    By josephdcoleman in forum New To Java
    Replies: 0
    Last Post: 03-12-2009, 06:08 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
  •