The Java Developers Almanac 1.4


Order this book from Amazon.

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

e754. Adding an Icon to the Label of a JCheckBox Component

Unlike a button, setIcon() does not add an icon to the text label. Rather, in a checkbox, the method is used to customize the icons used to depict its state. However, by using the HTML capabilities in a label, it is possible to add an icon to the label without affecting the state-depicting icons. This example demonstrates the technique.
    // Define an HTML fragment with an icon on the left and text on the right.
    // The elements are embedded in a 3-column table.
    String label = "<html><table cellpadding=0><tr><td><img src=file:"
        // The location of the icon
        + icon.gif"
        + "/></td><td width="
    
        // The gap, in pixels, between icon and text
        + 3
        + "><td>"
    
        // Retrieve the current label text
        + checkbox.getText()
        + "</td></tr></table></html>";
    
    // Add the icon
    checkbox.setText(label);

 Related Examples
e752. Creating a JCheckbox Component
e753. Getting and Setting the State of a JCheckbox Component
e755. Customizing the Icons in a JCheckBox Component

See also: Actions    JButton    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    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.