Results 1 to 5 of 5
  1. #1
    ShardaD is offline Member
    Join Date
    Jun 2010
    Rep Power

    Default JButton with Icon & text alignment issue


    I have a JButton with a background image & a text of button.
    Now I want a text to be aligned Left & Image should remain at center
    text should be on image.
    For that I have write,

    objButton.setHorizontalAlignment(SwingConstants.EA ST); //for left aligning
    objButton.setHorizontalTextPosition(0); // to place text on the image ---- this is working fine
    but I am not able to align text to left. It is still showing me at center.
    Please help me out....

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    United States
    Rep Power


    What layout are you using? I think only BoxLayout really takes your alignment specification into consideration.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power


    EAST and 0, are not "proper" parameters. Read the API for the list of the proper variables to be used.

    You can't do what you want directly with a JButton since text can only be painted on top of the image in the vertical and horizontal center of the button.

    You can try using the JButton as a Container and add a label to it then you can position the label anywhere you want. For example you might do:

    Java Code:
    JButton button = new JButton( new ImageIcon(...) );
    button.setLayout( new BorderLayout() );
    JLabel label = new JLabel( "text" );
    button.add( label );

  4. #4
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Rep Power


    > I have a JButton with a background image

    if you're talking about a paintComponent image,
    just use drawString with co-ords left of the image
    for your text

  5. #5
    ShardaD is offline Member
    Join Date
    Jun 2010
    Rep Power


    I never used any layout for JButton, now by giving layout to button its working.
    thanks :)

Similar Threads

  1. Text formatting and alignment according to the printer
    By java_fledgeling in forum Advanced Java
    Replies: 2
    Last Post: 06-08-2010, 11:34 AM
  2. Replies: 3
    Last Post: 05-25-2009, 04:33 AM
  3. My icon extends my JButton
    By hitmen in forum AWT / Swing
    Replies: 5
    Last Post: 03-06-2009, 08:49 AM
  4. Alignment Issue...
    By chanduseec in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 09-30-2008, 07:47 AM
  5. Help needed in Frames text alignment
    By ravjot28 in forum Java Applets
    Replies: 2
    Last Post: 06-27-2008, 05:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts