The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JButton  [9 examples]

e747. Moving the Icon in a JButton Component

There are two methods for controlling the position of the text relative to the icon - - setVerticalTextPosition() and setHorizontalTextPosition(). There are three settings for each axis, which allows for a total of nine positions.

To control the gap between the text and icon, see e749 Setting the Gap Size Between the Label and Icon in a JButton Component.

Note: Not all placements are possible. For example, it is not possible to place the text above the icon with their left edges aligned. The nine possible placements are demonstrated below.

    // To create a button with an icon, see e746 Adding an Icon to a JButton Component
    
    // Place text over center of icon; they both occupy the same space
    button.setVerticalTextPosition(SwingConstants.CENTER);
    button.setHorizontalTextPosition(SwingConstants.CENTER);
    
    // Place text above icon
    button.setVerticalTextPosition(SwingConstants.TOP);
    button.setHorizontalTextPosition(SwingConstants.CENTER);
    
    // Place text below icon
    button.setVerticalTextPosition(SwingConstants.BOTTOM);
    button.setHorizontalTextPosition(SwingConstants.CENTER);
    
    // Place text to the left of icon, vertically centered
    button.setVerticalTextPosition(SwingConstants.CENTER);
    button.setHorizontalTextPosition(SwingConstants.LEFT);
    
    // Place text to the left of icon and align their tops
    button.setVerticalTextPosition(SwingConstants.TOP);
    button.setHorizontalTextPosition(SwingConstants.LEFT);
    
    // Place text to the left of icon and align their bottoms
    button.setVerticalTextPosition(SwingConstants.BOTTOM);
    button.setHorizontalTextPosition(SwingConstants.LEFT);
    
    // Place text to the right of icon, vertically centered
    button.setVerticalTextPosition(SwingConstants.CENTER);
    button.setHorizontalTextPosition(SwingConstants.RIGHT);
    
    // Place text to the right of icon and align their tops
    button.setVerticalTextPosition(SwingConstants.TOP);
    button.setHorizontalTextPosition(SwingConstants.RIGHT);
    
    // Place text to the right of icon and align their bottoms
    button.setVerticalTextPosition(SwingConstants.BOTTOM);
    button.setHorizontalTextPosition(SwingConstants.RIGHT);

 Related Examples
e743. Creating a JButton Component
e744. Changing the Label of a JButton Component
e745. Creating a Multiline Label for a JButton Component
e746. Adding an Icon to 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
e750. Adding a Disabled Icon to a JButton Component
e751. Adding a Rollover and Pressed Icon to a JButton Component

See also: Actions    JCheckBox    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.