I'm using Ikonli Packs for my element icons and adding it to a JLabel
works fine.
I want to implement a hover feature but when I do, nothing happens unless I try to call the .setText()
method of my JLabel
.
I thought it might be obligatory to have text in order to detect the cursor, but the JLabel
doesn't have text initially, yet it still detects it, but as I mentioned, if I don't have the .setText()
as well, the icon color will not change.
There are many examples here and on other websites on how to implement hover for labels/buttons/etc., but they all have text in one shape or form.
There's also this example which hovers over images, but yet again, changes the text, not the images themselves.
(The elements seen in the MyForm.java
file have been added via the Swing Palette.)
package bruh;
import com.formdev.flatlaf.FlatDarkLaf;
import org.kordamp.ikonli.materialdesign2.MaterialDesignA;
import org.kordamp.ikonli.swing.FontIcon;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MyForm extends JFrame {
private JPanel mainPanel;
private JLabel myLabel;
public MyForm(String title) {
super(title);
this.setContentPane(mainPanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
Color mainColor = Color.RED;
Color hoverColor = Color.WHITE;
FontIcon icon = FontIcon.of(MaterialDesignA.ACCOUNT);
icon.setIconSize(10);
icon.setIconColor(mainColor);
myLabel.setIcon(icon);
myLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
icon.setIconColor(hoverColor);
myLabel.setIcon(icon);
//myLabel.setText("ENTERED");
}
@Override
public void mouseExited(MouseEvent e) {
icon.setIconColor(mainColor);
myLabel.setIcon(icon);
//myLabel.setText("EXITED");
}
});
}
public static void main(String[] args) {
FlatDarkLaf.setup();
JFrame frame = new MyForm("Hello World");
frame.setVisible(true);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>bruh</groupId>
<artifactId>bruh</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.formdev</groupId>
<artifactId>flatlaf</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-core</artifactId>
<version>12.2.0</version>
</dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-swing</artifactId>
<version>12.2.0</version>
</dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-materialdesign2-pack</artifactId>
<version>12.2.0</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
</project>
As suggested by Andrew Thompson , I used a JButton
instead of JLabel
.
package bruh;
import com.formdev.flatlaf.FlatDarkLaf;
import org.kordamp.ikonli.materialdesign2.MaterialDesignA;
import org.kordamp.ikonli.swing.FontIcon;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MyForm extends JFrame {
private JPanel mainPanel;
private JButton myButton;
public MyForm(String title) {
super(title);
this.setContentPane(mainPanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
Color mainColor = Color.RED;
Color hoverColor = Color.WHITE;
FontIcon icon = FontIcon.of(MaterialDesignA.ACCOUNT);
icon.setIconSize(10);
icon.setIconColor(mainColor);
myButton.setIcon(icon);
// Use these 2 lines to remove the decoration.
myButton.setBorderPainted(false);
myButton.setContentAreaFilled(false);
myButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
icon.setIconColor(hoverColor);
}
@Override
public void mouseExited(MouseEvent e) {
icon.setIconColor(mainColor);
}
});
}
public static void main(String[] args) {
FlatDarkLaf.setup();
JFrame frame = new MyForm("Hello World");
frame.setVisible(true);
}
}
Now only the icon is visible as seen in the image from my original post.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.