Results 1 to 2 of 2
Thread: JButton : Background and Text
- 05-25-2011, 08:57 PM #1
Member
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
JButton : Background and Text
Hello,
I want to create a JButton with an image in background and a dynamic text.
I create my own derived JButton to implement paintComponent() method.
So my background is ok but when I try to use setText() I can't see my text...
Then I try to implement drawString() method in my button. With this method I can see my text but my background disappear !
Please can you tell me how to fix that and create button with background and text ?
Thank you.
- 05-26-2011, 04:43 AM #2
There's no need to reinvent the wheel. JButton has setIcon and setText methods (also setRolloverIcon / setPressedIcon / serDisabledIcon ... did I miss any?
You can create an implementation of Icon that returns 0 for its width and height (thus ensuring that the button doesn't reserve space for the Icon) and paints the entire background (using getWidth() / getHeight() of the Component parameter to paintIcon. Something like (mayhave typos)This may displace the button's text off-center due to the text-icon gap. You can fine-tune this either by setIconTextGap(0) or by setting the horizontal and vertical text position to CENTER.Java Code:class BackgrouindIcon implements Icon { @Override public int getIconWidth() { return 0; } @Override public int getIconHeight() { return 0; } @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.ORANGE); g.fillRect(0, 0, c.getWidth(), c.getHeight()); } }
db
Similar Threads
-
Adding JButton to JFrame with background
By bzknight in forum AWT / SwingReplies: 1Last Post: 01-19-2011, 06:55 PM -
Draw text over background image using JLabel and JFrame
By vytska in forum New To JavaReplies: 13Last Post: 09-29-2010, 10:55 PM -
PDFBOX change text colours and remove background
By gstkein in forum Advanced JavaReplies: 1Last Post: 11-20-2009, 11:54 AM -
How to change the default background color of a DISABLED JButton
By ryogananda in forum Java AppletsReplies: 4Last Post: 03-21-2009, 05:48 PM -
JButton onClick change color background
By behrk2 in forum AWT / SwingReplies: 6Last Post: 07-09-2008, 04:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks