Add icon settings to swing Action in Java
Description
The following code shows how to add icon settings to swing Action.
Example
import java.awt.event.ActionEvent;
/* w w w .jav a 2 s.co m*/
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
public static void main(String[] argv) throws Exception {
// Retrieve the icon
Icon icon = new ImageIcon("a.gif");
// Create an action with an icon
Action action = new AbstractAction("Button Label", icon) {
// This method is called when the button is pressed
public void actionPerformed(ActionEvent evt) {
// Perform action
}
};
// Create the button; the icon will appear to the left of the label
JButton button = new JButton(action);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(button);
f.setSize(300, 200);
f.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »