Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: MouseListener & MouseMotionListener

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MouseListener & MouseMotionListener

    Hey,

    I'm new at Java and have never programmed before but recently a friend of mine inspired me so I'm really interested and willing to learn. I have been looking at java for 2 weeks or so now.

    Anyways, my question is:

    Do the mouse events have to be used with a JButton or something like a JLabel?

    For example; could I just have a plain background and still handle events? i.e. if(event.getSource()==COORDINATES);

    Can't really explain it to well myself lol but in simple terms, can I handle an event when the mouse enters/clicks/drags on a COORDINATION?

    Thanks in advance


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: MouseListener & MouseMotionListener

    You can use mouse event with any object that allows you to add the listener (for example anything that extends Component - JPanel, JButton, etc...) I'm not sure what you are asking about with regards to COORDINATION, but for a plain background you can use a JPanel without anything added or drawn, add the listener, and deal with mouse events appropriately.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MouseListener & MouseMotionListener

    Quote Originally Posted by copeg View Post
    You can use mouse event with any object that allows you to add the listener (for example anything that extends Component - JPanel, JButton, etc...) I'm not sure what you are asking about with regards to COORDINATION, but for a plain background you can use a JPanel without anything added or drawn, add the listener, and deal with mouse events appropriately.
    Thanks for the reply,

    Basically because I only ever used FlowLayout(); I'm not sure on how to use any other layout or even JPanel, i know how to create one etc but i don't quite understand it which is why I just use FlowLayout();. I also don't know how to create my OWN buttons or labels, i just use JButton or JLabel etc.

    I found a way of adding a background image to my window and it doesn't require FlowLayou(); so I don't know how to add JButtons to it etc. So basically what I was asking was, could I create a background picture and add stuff like BUTTON1, BUTTON2 as TEXT ON MY background picture then find the coordinates on the background picture of where i want to handle the events?

    Do you know what i mean?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: MouseListener & MouseMotionListener

    Yep. Create your own JPanel and over-ride it's paintComponent method. This will allow you to draw anything you want onto that JPanel. You can still add other components (such as buttons and labels) onto that JPanel, and their ActionListeners/MouseListeners are un-effected by the custom painting.

    // example JPanel with a background image, and some buttons
     
    public class BackgroundPanel extends JPanel implements ActionListener
    {
         private Image background;
         private JButton button;
         public BackgroundImage(Image background)
         {
              this.background = background;
              this.button = new JButton("Push me!");
              this.button.addActionListener(this);
         }
     
         public void paintComponent(Graphics g)
         {
              g.drawImage(background, 0, 0, Color.white, this);
         }
     
         public void ActionPerformed(ActionEvent e)
         {
              if (e.getSource() == this.button)
              {
                   JOptionPane.showMessageDialog(this, "You pushed the button!");
              }
         }
    }
    Last edited by helloworld922; March 20th, 2010 at 02:10 PM.

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MouseListener & MouseMotionListener

    Quote Originally Posted by helloworld922 View Post
    Yep. Create your own JPanel and over-ride it's paintComponent method. This will allow you to draw anything you want onto that JPanel. You can still add other components (such as buttons and labels) onto that JPanel, and their ActionListeners/MouseListeners are un-effected by the custom painting.

    // example JPanel with a background image, and some buttons
     
    public class BackgroundPanel extends JPanel implements ActionListener
    {
         private Image background;
         private JButton button;
         public BackgroundImage(Image background)
         {
              this.background = background;
              this.button = new JButton("Push me!");
              this.button.addActionListener(this);
         }
     
         public void paintComponent(Graphics g)
         {
              g.drawImage(background, 0, 0, Color.white, this);
         }
     
         public void ActionPerformed(ActionEvent e)
         {
              if (e.getSource() == this.button)
              {
                   JOptionPane.showMessageDialog(this, "You pushed the button!");
              }
         }
    }
    Thanks a lot, I really appreciate it. But what I really need to know is, if I can make any random space on my background picture CLICKABLE? so it handles an event when I click it.
    Last edited by JavaLearner; March 21st, 2010 at 04:46 AM.

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: MouseListener & MouseMotionListener

    Add a MouseListener to the JPanel and check the coordinates from MouseEvent#getPoint().

    That said, this sounds like a bad approach. Why do you think you need to do that?

    db

  7. #7
    Junior Member
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MouseListener & MouseMotionListener

    Quote Originally Posted by Darryl.Burke View Post
    Add a MouseListener to the JPanel and check the coordinates from MouseEvent#getPoint().

    That said, this sounds like a bad approach. Why do you think you need to do that?

    db
    This is what I mean finally someone understood me

    Basically, I only ever used FlowLayout and I don't know how to add my own background image to it. I sort of know how to do it on a JPanel but can't add JButtons to it etc. So I wondered if I could just draw out the buttons on a background image and make stuff clickable just by the coords.

  8. #8
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: MouseListener & MouseMotionListener

    As helloworld922 has already told you at #4, performing custom painting doesn't preclude adding components to the panel.

    Maybe you're trying to run before you've learned to walk. I recommend the Swing tutorials on the Sun site (link on next line)
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    db

  9. #9
    Junior Member
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MouseListener & MouseMotionListener

    Quote Originally Posted by Darryl.Burke View Post
    As helloworld922 has already told you at #4, performing custom painting doesn't preclude adding components to the panel.

    Maybe you're trying to run before you've learned to walk. I recommend the Swing tutorials on the Sun site (link on next line)
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    db
    Thanks for the useful links (Y)

  10. #10
    Junior Member
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MouseListener & MouseMotionListener

    There are no such things to be used while you are on swing.As I have seen and used Jlabels ,they can be easily updated using the Action listeners and action events.

Similar Threads

  1. MouseMotionListener is not updating my variable
    By olemagro in forum AWT / Java Swing
    Replies: 1
    Last Post: February 8th, 2010, 03:44 PM