The example below shows you how we can customize the icon for JButton
Swing components.
package org.kodejava.swing;
import javax.swing.*;
import java.awt.*;
public class JButtonCustomIcon extends JFrame {
public JButtonCustomIcon() throws HeadlessException {
initialize();
}
private void initialize() {
setSize(500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
JButton button = new JButton("Press Me!");
// Below is how we can set various icons for the JButton swing
// component. There are the default icon, selected, disabled, pressed
// and rollover icons.
button.setIcon(new ImageIcon("default.png"));
button.setSelectedIcon(new ImageIcon("selected.png"));
button.setDisabledIcon(new ImageIcon("disabled.png"));
button.setDisabledSelectedIcon(new ImageIcon("disabledSelected.png"));
button.setPressedIcon(new ImageIcon("pressed.png"));
button.setRolloverIcon(new ImageIcon("rollover.png"));
button.setRolloverSelectedIcon(new ImageIcon("rolloverSelected.png"));
getContentPane().add(button);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new JButtonCustomIcon().setVisible(true));
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024