Adding an Icon to a JLabel Component
This example creates a JLabel component with an icon.
This example adds or replaces the icon in an existing JLabel
component:
The methods to control the position of the icon and text within a
JLabel component are identical to those of a JButton. See
also Moving the Icon in a JButton Component,
Moving the Label/Icon Pair in a JButton Component,
Setting the Gap Size Between the Label and Icon in a JButton Component,
and Adding a Disabled Icon to a JButton Component.
// Fetch icon
Icon icon = new ImageIcon("icon.gif");
// Create a label with text and an icon; the icon appears to the left of the text
JLabel label = new JLabel("Text Label", icon, JLabel.CENTER);
// Create a label with only an icon
label = new JLabel(icon);
// Add an icon to an existing label
label.setIcon(icon);
Post a comment