Results 1 to 8 of 8
Thread: Drawing Text on JButton Icon
- 01-18-2013, 08:06 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 9
- Rep Power
- 0
- 01-18-2013, 08:17 PM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-18-2013, 09:14 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 9
- Rep Power
- 0
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!
- 01-19-2013, 05:57 AM #4
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-19-2013, 07:21 AM #5
Member
- Join Date
- Jan 2013
- Posts
- 9
- Rep Power
- 0
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
- 01-19-2013, 07:30 AM #6
- 01-19-2013, 08:24 AM #7
Member
- Join Date
- Jan 2013
- Posts
- 9
- Rep Power
- 0
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.
- 01-19-2013, 08:35 AM #8
Similar Threads
-
How set JButton icon Relative to the JButton size???
By farshad in forum AWT / SwingReplies: 1Last Post: 01-15-2013, 06:44 PM -
Icon wont change on a JButton
By Bagzli in forum New To JavaReplies: 9Last Post: 05-15-2011, 10:48 PM -
JButton with Icon & text alignment issue
By ShardaD in forum AWT / SwingReplies: 4Last Post: 01-11-2011, 11:10 AM -
Removing icon from JButton
By 3RDofApril in forum AWT / SwingReplies: 3Last Post: 11-04-2010, 02:57 AM -
My icon extends my JButton
By hitmen in forum AWT / SwingReplies: 5Last Post: 03-06-2009, 07:49 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks