Skip to content

Instantly share code, notes, and snippets.

@TabsPH
Created November 12, 2012 07:03
Show Gist options
  • Save TabsPH/4057899 to your computer and use it in GitHub Desktop.
Save TabsPH/4057899 to your computer and use it in GitHub Desktop.
Hover event for such components like JButton and JLabel
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
public class Hover extends MouseAdapter {
private String lblStr = "";
public Hover( String lblStr ) {
this.lblStr = lblStr;
}
@Override
public void mouseEntered(MouseEvent e) {
JLabel lbl = (JLabel) e.getComponent();
lbl.setForeground(new Color(255, 127, 80));
lbl.setText( String.format( "<html><u>%s</u></html>", lblStr) );
}
@Override
public void mouseExited(MouseEvent e) {
JLabel lbl = (JLabel) e.getComponent();
lbl.setText( String.format( "<html><u>%s</u><</html>", lblStr ) );
lbl.setForeground( new Color(245, 245, 245) );
}
}
@alenpaulvarghese
Copy link

Thanks.

@pereraniromi1004
Copy link

Thanks

@adideas
Copy link

adideas commented Jan 4, 2023

Thanks)

@Kurokeida
Copy link

Searching for a while thanks for your post i have found a way to work with my own using yours as reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment