The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JLabel  [4 examples]

e740. Adding an Icon to a JLabel Component

This example creates a JLabel component with an icon.
    // 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);
This example adds or replaces the icon in an existing JLabel component:
    // Add an icon to an existing label
    label.setIcon(icon);
The methods to control the position of the icon and text within a JLabel component are identical to those of a JButton. See also e747 Moving the Icon in a JButton Component, e748 Moving the Label/Icon Pair in a JButton Component, e749 Setting the Gap Size Between the Label and Icon in a JButton Component, and e750 Adding a Disabled Icon to a JButton Component.

 Related Examples
e739. Creating a JLabel Component
e741. Setting the Focus of a JTextField Component Using a JLabel Component
e742. Adding Drag-and-Drop Support to a JLabel Component

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.