MouseListener on a label?

Image change when a cursor goes over a label

Page 1 of 1

12 Replies - 31839 Views - Last Post: 17 April 2008 - 08:12 AM Rate Topic: -----

#1 Chopster   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 134
  • Joined: 29-March 08

MouseListener on a label?

Post icon  Posted 14 April 2008 - 09:32 AM

Hey guys,

I am having a little problem with a mouse rollover effect, i have made a custom image(JLabel) and i would like it, so when the user hovers over the image(Jlabel) it would change to another image(JLabel). I can get this to work with buttons but not with JLabels, i have searched but can't seem to find a way of doing it.

Any Help would be great.

Thanks in advance.
Jason
Is This A Good Question/Topic? 0
  • +

Replies To: MouseListener on a label?

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: MouseListener on a label?

Posted 14 April 2008 - 10:08 AM

View PostChopster, on 14 Apr, 2008 - 09:32 AM, said:

Hey guys,

I am having a little problem with a mouse rollover effect, i have made a custom image(JLabel) and i would like it, so when the user hovers over the image(Jlabel) it would change to another image(JLabel). I can get this to work with buttons but not with JLabels, i have searched but can't seem to find a way of doing it.

Any Help would be great.

Thanks in advance.
Jason

Small exemple.

Every time the mouse will enter the JLabel its nbOver count will change
Every time the mouse will be click while in the JLabel its nbClick count will change

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public class Over extends JFrame implements MouseListener {

	int nbOver = 0, nbClick = 0;
	JLabel label;
	
	Over() {
		super("Over Label");
	    label = new JLabel("Over: 0 Click: 0");
	    label.addMouseListener(this);
	    add(label);
	}

	public void mouseClicked(MouseEvent arg0) {
	}
	public void mouseEntered(MouseEvent arg0) {
		nbOver++;
		label.setText("Over: " + nbOver + " Click: " + nbClick);
	}
	public void mouseExited(MouseEvent arg0) {
	}
	public void mousePressed(MouseEvent arg0) {
		nbClick++;
		label.setText("Over: " + nbOver + " Click: " + nbClick);
	}
	public void mouseReleased(MouseEvent arg0) {
	}
	
	public static void main(String[] arg) {
		Over o = new Over();
		o.setBounds(0, 0, 200, 100);
		o.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		o.setVisible(true);
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 Chopster   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 134
  • Joined: 29-March 08

Re: MouseListener on a label?

Posted 14 April 2008 - 10:32 AM

Thanks

I'll give it a blast
Was This Post Helpful? 0
  • +
  • -

#4 Chopster   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 134
  • Joined: 29-March 08

Re: MouseListener on a label?

Posted 16 April 2008 - 02:20 PM

Hi again,

Well to start off your code worked fine PBL, i got your little program running and all was well. The problem lies when i tried to adapt it a little bit for my code. Basically i can get the JFrame to show up, but i can't see my image, yet i get no errors, i am using a null layout for this project, please can someone look through this code and tell me why my image will not display?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.io.File.*;
import javax.media.*;
import java.awt.event.MouseEvent;  
import java.awt.event.MouseListener; 

public class MP2 extends JFrame implements MouseListener{
	
	static JFrame frame;
	static Container pane;
	static Insets insets;
	static JLabel playbtn,playbtnl;
	ImageIcon icon = new ImageIcon("playBtn.gif");
	ImageIcon icon2 = new ImageIcon("playBtnl.gif");
		
	MP2() {  
		playbtn = new JLabel(icon);  
		playbtn.addMouseListener(this);  
		pane.add(playbtn);
		playbtn.setBounds(insets.left + 25,insets.top + 120, 50, 25);
		playbtnl = new JLabel(icon2);
		playbtnl.addMouseListener(this);
		playbtnl.setBounds(insets.left + 25,insets.top + 120, 50, 25);
		}  
  
	public void mouseClicked(MouseEvent arg0) {  
	}  
	public void mouseEntered(MouseEvent arg0) {  
		  pane.add(playbtnl);
		  playbtn.setVisible(false);
	}  
	public void mouseExited(MouseEvent arg0) { 
		playbtn.setVisible(true);
	}  
	public void mousePressed(MouseEvent arg0) {  
		  
	}  
	public void mouseReleased(MouseEvent arg0) {  
	}  
	  
	public static void main(String[] arg) {
		//Create the look and feel
		try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
		catch (ClassNotFoundException e){}
		catch (InstantiationException e){}
		catch (IllegalAccessException e){}
		catch (UnsupportedLookAndFeelException e){}
		
		frame = new JFrame("JPlayer 2.0");
		frame.setSize(217,300);
		pane = frame.getContentPane();
		insets = pane.getInsets();
		//Apply the null layout
		pane.setLayout(null);
		pane.setBackground(Color.BLACK);
		frame.setVisible(true);
	}  
}  



Thanks in Advance
Jason
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: MouseListener on a label?

Posted 16 April 2008 - 02:39 PM

First obvious mistake.

You can make a JComponent (like your playbtn) visible or invisible as often as you want but you should just add it once to another JComponent (like pane). Once it is there it is there visible or not.

So remove your add method from the listener.
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: MouseListener on a label?

Posted 16 April 2008 - 02:53 PM

Second mistake

JLabel by default are not opaque... so their background color is the one of the Panel
You won't see much of your Jlabel if the Panel background is black unless to set your JLabel to opaque

label.setOpaque(true);

and set up its background color to something

label.setBackground(Color.xxxx);
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: MouseListener on a label?

Posted 16 April 2008 - 02:59 PM

Third and fourth mistakes:

- insets will be 0,0,0,0 until the frame is made visible
- you never made a new MP2 so your labels will never be created

I'll let you fix all these before posting another reply
Was This Post Helpful? 0
  • +
  • -

#8 m2s87   User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: MouseListener on a label?

Posted 16 April 2008 - 06:51 PM

pbl is funny (or what ^_^). Anyhow if you want text and img of label to change when hoovering, you might end up with code similar to this:
import java.awt.Color;
import java.awt.Container;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


	public static class MP2 extends JFrame {
		public static class MPlabel extends JLabel implements MouseListener {
			private static final long serialVersionUID = 2710679661071094991L;
			
			public static final String funny_in = " U R in me";
			public static final String funny_out = " i miss you";
			
			public static final ImageIcon img_in = new ImageIcon("playBtn.gif");
			public static final ImageIcon img_out = new ImageIcon("playBtn1.gif");

			private String _funny_in;
			private String _funny_out;

			private ImageIcon _img_in;
			private ImageIcon _img_out;

			public MPlabel() {
				super(MPlabel.funny_out);
				this._funny_in = MPlabel.funny_in;
				this._funny_out = MPlabel.funny_out;
				this._img_in = MPlabel.img_in;
				this._img_out = MPlabel.img_out;
				this.addMouseListener(this);
			}

			public MPlabel(ImageIcon in, ImageIcon out) {
				super(out);
				this._funny_in = MPlabel.funny_in;
				this._funny_out = MPlabel.funny_out;
				this._img_in = in;
				this._img_out = out;
				this.addMouseListener(this);
			}

			public MPlabel(String in, String out) {
				super(MPlabel.funny_out);
				this._funny_in = in;
				this._funny_out = out;
				this._img_in = MPlabel.img_in;
				this._img_out = MPlabel.img_out;
				this.addMouseListener(this);
			}

			public String get_funny_in() {
				return _funny_in;
			}

			public String get_funny_out() {
				return _funny_out;
			}

			public ImageIcon get_img_in() {
				return _img_in;
			}

			public ImageIcon get_img_out() {
				return _img_out;
			}

			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void mouseEntered(MouseEvent arg0) {
				Object x = arg0.getComponent();
				if (x instanceof JLabel) {
					((JLabel) x).setText(MPlabel.funny_in);
					((JLabel) x).setIcon(MPlabel.img_in);
				}
			}

			public void mouseExited(MouseEvent arg0) {
				Object x = arg0.getComponent();
				if (x instanceof JLabel) {
					((JLabel) x).setText(MPlabel.funny_out);
					((JLabel) x).setIcon(MPlabel.img_out);
				}
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			public void set_funny_in(String _funny_in) {
				this._funny_in = _funny_in;
			}

			public void set_funny_out(String _funny_out) {
				this._funny_out = _funny_out;
			}

			public void set_img_in(ImageIcon _img_in) {
				this._img_in = _img_in;
			}

			public void set_img_out(ImageIcon _img_out) {
				this._img_out = _img_out;
			}
		}

		private static final long serialVersionUID = -3476067320597868539L;
		private Container pane;
		private Insets insets;
		private MPlabel playbtn, playbtnl;

		MP2(String title) {
			super(title);

			pane = this.getContentPane();
			pane.setLayout(null);
			pane.setBackground(Color.BLACK);

			insets = pane.getInsets();

			playbtn = new MPlabel();
			playbtn.setOpaque(true);
			playbtn.setBackground(Color.GREEN);
			// playbtn.addMouseListener(this);

			playbtnl = new MPlabel();
			playbtnl.setOpaque(true);
			playbtnl.setBackground(Color.GREEN);
			// playbtnl.addMouseListener(this);

			pane.add(playbtn);
			pane.add(playbtnl);

			playbtn.setBounds(insets.left + 25, insets.top + 120, 55, 25);
			playbtnl.setBounds(insets.left + 25, insets.top + 150, 55, 25);

			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.setVisible(true);
		}
	}

	private static MP2 frame;

	public static void main(String[] arg) {
		// Create the look and feel
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
		} catch (InstantiationException e) {
		} catch (IllegalAccessException e) {
		} catch (UnsupportedLookAndFeelException e) {
		}

		frame = new MP2("JPlayer 2.0");
		frame.setSize(217, 300);
	}

Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: MouseListener on a label?

Posted 16 April 2008 - 09:36 PM

View Postm2s87, on 16 Apr, 2008 - 06:51 PM, said:

pbl is funny (or what ^_^). Anyhow if you want text and img of label to change when hoovering, you might end up with code similar to this:


I am not trying to be funny.
I try to have a pedagogic approach with people learning Java.
They have so much problem making their programs work I don't think they need to learn right now about Annotation Type or UIManager or serialVersionUID you just confused them.
These students have assigment to do and they try hard (at least some of them.... and beleive me they are quickly identifiees) and they are no reason to suggest them to put in their code something that obviously their teachers will wonder "from the heck where they did take that from ?".
Even if their code is sluggish, and there are many ways to make them look better I think it is a better approach to let them revise their sluggish code and make it works than writting, in a perfect way, the solution to their problem.
You learn programming by test and trial not by having a beautiful solution just presented to you.
When the program contains:
toto = toto + 1;
I let it that way no need to teach them toto++ right away.
At least that is what I am trying to do. If this not the philosphy of this site let me know. I'll just retire.

This post has been edited by pbl: 16 April 2008 - 10:01 PM

Was This Post Helpful? 0
  • +
  • -

#10 m2s87   User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: MouseListener on a label?

Posted 16 April 2008 - 10:03 PM

View Postpbl, on 17 Apr, 2008 - 07:36 AM, said:

View Postm2s87, on 16 Apr, 2008 - 06:51 PM, said:

pbl is funny (or what ^_^). Anyhow if you want text and img of label to change when hoovering, you might end up with code similar to this:


I am not trying to be funny.
I try to have a pedagogic approach with people learning Java.
They have so much problem making their programs work I don't think they need to learn right now about Annotation Type or UIManager.
These students have assigment to do and they try hard (at least some of them.... and beleive me they are quickly identifiees) and they are no reason to suggest them to put in their code something that obviously their teachers will wonder "from the heck where they did take that from ?".
Even if their code is sluggish, and there are many ways to make them look better I think it is a better approach to let them revise their sluggish code and make it works than writting, in a perfect way, the solution to their problem.
You learn programming by test and trial not by having a beautiful solution just presented to you.
At least that is what I am trying to do. If this not the philosphy of this site let me know. I'll just retire.

Your right, sorry that i thought your funny. I am student myself, so i am used to helping people around. I do not write beautiful or perfect solutions, but i write solutions that work. And i know that this site philosophy is to guide and teach not to show fixed solutions, i guess i just suck at pedagogic approach of teaching. Keep up the good work :^:
Was This Post Helpful? 0
  • +
  • -

#11 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: MouseListener on a label?

Posted 16 April 2008 - 11:02 PM

View Postm2s87, on 16 Apr, 2008 - 10:03 PM, said:

Your right, sorry that i thought your funny. I am student myself, so i am used to helping people around. I do not write beautiful or perfect solutions, but i write solutions that work. And i know that this site philosophy is to guide and teach not to show fixed solutions, i guess i just suck at pedagogic approach of teaching. Keep up the good work :^:


Thanks I really appreciate it.

But for people who just start to understand what an if is there is no need to insert a concept like ? :
And the Font f was not used... so may be your Eclipse showed you that it was not a used variable but who cares ?
Let them f*** assignment works

This post has been edited by pbl: 16 April 2008 - 11:14 PM

Was This Post Helpful? 0
  • +
  • -

#12 Chopster   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 134
  • Joined: 29-March 08

Re: MouseListener on a label?

Posted 17 April 2008 - 07:00 AM

Whoah, thanks for all the information.

I guess i have a lot to learn, it seems i learn one thing but then get confused by another, it also seems there are so many different ways of doing things, well hopefully i will get there someday, thanks for all the advice, i will go through it now and see what i can do.

Thanks

Jason

p.s not that it makes a difference, but this is a personal project not an assignment, i only say this as you might have already realised my reply's can be slow, as this is my free time rather than college time, so please bear with me if i don't reply straight away with my success or more likely failure :P
Was This Post Helpful? 0
  • +
  • -

#13 Chopster   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 134
  • Joined: 29-March 08

Re: MouseListener on a label?

Posted 17 April 2008 - 08:12 AM

Ok some progress atlast!

I got it working, well kind of :P

The image does indeed change to the next image, exactly what i wanted it to do, one problem is that it changes the icon as soon as the mouse pointer enters the java program, not when it passes over the label. Also i am a little bit confused on how i go about setting where i want the label to go, aswell as any other components i add, sorry if this is very basic stuff, but i am only used to writing code with a null layout, and i set the positions of stuff manually. But this code i have adapted from you PBL i cannot seem to use the null layout code to position my stuff.

Thanks for all your great help so far, my code is below, feel free to pick it apart, i got the feeling there was more you were gunna tell me PBL, go for it.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.io.File.*;
import javax.media.*;
import java.awt.event.MouseEvent;  
import java.awt.event.MouseListener; 

public class MP2 extends JFrame implements MouseListener{
	
	static JLabel playbtn,playbtnl;
	ImageIcon icon = new ImageIcon("playBtn.gif");
	ImageIcon icon2 = new ImageIcon("playBtnl.gif");
		
	MP2() { 
		super("JPlayer 2.0");
		playbtn = new JLabel(icon);  
		playbtn.addMouseListener(this);  
		add(playbtn);
		playbtnl = new JLabel(icon2);  
		playbtnl.addMouseListener(this);
					 
		}  
  
	public void mouseClicked(MouseEvent arg0) {  
	}  
	public void mouseEntered(MouseEvent arg0) {
		add(playbtnl);
		playbtn.setVisible(false);
				
	}  
	public void mouseExited(MouseEvent arg0) { 
		playbtn.setVisible(true);
	}  
	public void mousePressed(MouseEvent arg0) {  
		  
	}  
	public void mouseReleased(MouseEvent arg0) {  
	}  
	  
	public static void main(String[] arg) {
		//Create the look and feel
		try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
		catch (ClassNotFoundException e){}
		catch (InstantiationException e){}
		catch (IllegalAccessException e){}
		catch (UnsupportedLookAndFeelException e){}
		
		MP2 o = new MP2();
		o.setBounds(0,0,217,300);
		o.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		o.setVisible(true);
			
		}  
}  



Thanks in Advance
Jason
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1