Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By tim8w

Thread: Drawing Text on JButton Icon

  1. #1
    tim8w is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Cool Drawing Text on JButton Icon

    Hi,
    I want to have the Text of the JButton written on top of the Icon. I assume that this can't be done without customizing the JButton. Does anyone know how to do this?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Drawing Text on JButton Icon

    Read the API for JButton, not missing the methods inherited from Abstractbutton and also go through the tutorial linked from those APIs.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    tim8w is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Cool Re: Drawing Text on JButton Icon

    db,
    From the documentation, I gathered I needed to do something like the code below which worked:


    ImageIcon BlueButtonIcon = createImageIcon("Blue.gif");
    JButton myBtn;
    JLabel myBtnlbl;

    myBtn = new JButton(RedButtonIcon);
    myBtn.setLayout(new BorderLayout());
    myBtnlbl= new JLabel("My Text");
    myBtnlbl.setForeground(Color.white);
    myBtnlbl.setHorizontalAlignment(SwingConstants.CEN TER);
    myBtn.add(myBtnlbl);


    Thanks!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Drawing Text on JButton Icon

    Why are you adding a JLabel to a JButton? There's API that aligns the button's text relative to the Icon. Also, if you need to be sure of the alignment under any Look-and-Feel, set all alignments explicitly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    tim8w is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Default Re: Drawing Text on JButton Icon

    db,
    Am I missing something? All the API alignments I could find aligned the text around the Icon, not over it...

    tim8w

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Drawing Text on JButton Icon

    There can't be a clearer hint than this:
    Quote Originally Posted by DarrylBurke View Post
    There's API that aligns the button's text relative to the Icon.
    Part of that sentence is copied from the method descriptions in the API. Set them to CENTER.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    tim8w is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Default Re: Drawing Text on JButton Icon

    db,
    Got it. Did the following:

    Java Code:
    ImageIcon GreyButtonIcon = createImageIcon("Grey.gif");
    
    btnA = new JButton("A", GreyButtonIcon);
    btnA.setPreferredSize(new Dimension(48,48));
    btnA.setForeground(Color.white);
    btnA.setHorizontalTextPosition(SwingConstants.CENTER);
    btnA.setBackground(Color.black);
    Last edited by tim8w; 01-19-2013 at 08:27 AM.
    DarrylBurke likes this.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Drawing Text on JButton Icon

    Good show!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 01-15-2013, 06:44 PM
  2. Icon wont change on a JButton
    By Bagzli in forum New To Java
    Replies: 9
    Last Post: 05-15-2011, 10:48 PM
  3. JButton with Icon & text alignment issue
    By ShardaD in forum AWT / Swing
    Replies: 4
    Last Post: 01-11-2011, 11:10 AM
  4. Removing icon from JButton
    By 3RDofApril in forum AWT / Swing
    Replies: 3
    Last Post: 11-04-2010, 02:57 AM
  5. My icon extends my JButton
    By hitmen in forum AWT / Swing
    Replies: 5
    Last Post: 03-06-2009, 07:49 AM

Posting Permissions

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