The Java Developers Almanac 1.4


Order this book from Amazon.

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

e748. Moving the Label/Icon Pair in a JButton Component

When the size of the button component is larger than its contents, the label and icon are always kept together. In fact, the gap size (see e749 Setting the Gap Size Between the Label and Icon in a JButton Component) determines the fixed distance between them. Using two alignment methods, you can place the label/icon pair in one of nine possible locations.
    // To create a button with an icon, see e746 Adding an Icon to a JButton Component
    
    // Place the contents in the nw corner
    button.setVerticalAlignment(SwingConstants.TOP);
    button.setHorizontalAlignment(SwingConstants.LEFT);
    
    // Place the contents centered at the top
    button.setVerticalAlignment(SwingConstants.TOP);
    button.setHorizontalAlignment(SwingConstants.CENTER);
    
    // Place the contents in the ne corner
    button.setVerticalAlignment(SwingConstants.BOTTOM);
    button.setHorizontalAlignment(SwingConstants.RIGHT);
    
    // Place the contents in the sw corner
    button.setVerticalAlignment(SwingConstants.BOTTOM);
    button.setHorizontalAlignment(SwingConstants.LEFT);
    
    // Place the contents centered at the bottom
    button.setVerticalAlignment(SwingConstants.BOTTOM);
    button.setHorizontalAlignment(SwingConstants.CENTER);
    
    // Place the contents in the se corner
    button.setVerticalAlignment(SwingConstants.BOTTOM);
    button.setHorizontalAlignment(SwingConstants.RIGHT);
    
    // Place the contents vertically centered on the left
    button.setVerticalAlignment(SwingConstants.CENTER);
    button.setHorizontalAlignment(SwingConstants.LEFT);
    
    // Place the contents directly in the center
    button.setVerticalAlignment(SwingConstants.CENTER);
    button.setHorizontalAlignment(SwingConstants.CENTER);
    
    // Place the contents vertically centered on the right
    button.setVerticalAlignment(SwingConstants.CENTER);
    button.setHorizontalAlignment(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
e747. Moving the Icon 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.