Results 1 to 8 of 8
- 11-23-2011, 04:13 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
JButton background not displaying (FRUSTRATING)

I've been trying to get one of my JButtons to change it's background color for several hours. For some reason, the color I choose shows up behind the actual button.

Here's an excerpt of my code.
I have tried multiple different settings for the button, including "setContentAreaFilled(true)," but I can't seem to get it to work.Java Code:colorIcon = new JButton(); colorIcon.setOpaque(true); colorIcon.setBackground(Color.BLACK); colorIcon.setForeground(Color.BLACK);
Very frustrating, any ideas? Thanks.
- 11-23-2011, 07:20 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 65
- Rep Power
- 0
Re: JButton background not displaying (FRUSTRATING)
you have to setOpaque(false) :)
I am always confused in this type of methods :)
OOps sorry wrong info:
if you want to make it in brute force
Graphics g=colorIcon.getGraphics();
g.setColor(Color.BLACK);
g.fillRect(colorIcon.getBounds); //You have to write this bounds
colorIcon.paintComponent(g);
It will fill background.Last edited by Grkn; 11-23-2011 at 07:28 PM.
- 11-23-2011, 07:34 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 65
- Rep Power
- 0
Re: JButton background not displaying (FRUSTRATING)
you code works in my computer :D
- 11-23-2011, 07:43 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
Re: JButton background not displaying (FRUSTRATING)
I found about your "brute force" method about an hour before you posted and have decided to use that.
I know my code should work, not sure why it doesn't. That's why it was so frustrating.
Anyways, it works now, thanks for your help.
- 11-23-2011, 08:28 PM #5
Re: JButton background not displaying (FRUSTRATING)
Never never never use getGraphics() of a Component. That way lies grief.
Never never never call paintComponent or any painting method from client code.
The reason you're not seeing the background color is that the default Metal UI for a button uses a Map of colors to draw a gradient which is painted over the background.
Here are three sane approaches to obtaining a button with a background color.dbJava Code:import java.awt.*; import javax.swing.*; import javax.swing.plaf.ButtonUI; import javax.swing.plaf.basic.BasicButtonUI; public class ColoredButtonsDemo { public static void main(String[] args) throws Exception { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new ColoredButtonsDemo().makeUI(); } }); } public void makeUI() { JButton coloredButton1 = new JButton("One"); coloredButton1.setUI((ButtonUI) BasicButtonUI.createUI(coloredButton1)); coloredButton1.setBackground(Color.CYAN); JButton coloredButton2 = new JButton("Two"); coloredButton2.setIcon(new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.YELLOW); g.fillRect(0, 0, c.getWidth(), c.getHeight()); } @Override public int getIconWidth() { return 0; } @Override public int getIconHeight() { return 0; } }); JButton coloredButton3 = new JButton("Three") { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(new Color(255, 0, 255, 128)); // semi transparent magenta g.fillRect(0, 0, getWidth(), getHeight()); } }; JPanel panel = new JPanel(); panel.add(coloredButton1); panel.add(coloredButton2); panel.add(coloredButton3); JFrame frame = new JFrame(); frame.add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); } }Why do they call it rush hour when nothing moves? - Robin Williams
- 11-23-2011, 08:31 PM #6
Re: JButton background not displaying (FRUSTRATING)
Why do they call it rush hour when nothing moves? - Robin Williams
- 11-23-2011, 09:10 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 65
- Rep Power
- 0
Re: JButton background not displaying (FRUSTRATING)
hey I know there are mistakes. Sory
1-) Sometimes it can be neccessary to use getGraphics()
I will give example. Maybe It can be wrong so just tell me what is wrong with it.
I designed a drag and drop menu and item tracks mouse when I moves on the panel
2-)I told him to write bounds clearly near the code
3-)It's written wrong it must be paintComponents();
Also I told him that it is brute force: Not good way to implement code.
If It is desperate then he/she can use
Of course I'm wrong.I won't insist that I wrote the correct code.
I did not check the code and wrote it
- 11-23-2011, 11:25 PM #8
Re: JButton background not displaying (FRUSTRATING)
It's never recommended to use getGraphics() of a Component except for extremely advanced performance intensive applications.
No idea what you mean by
Please read the documentation and understand the functionality of the different painting methods. Start by reading the API for paintComponents(...) and you will see what it's meant to do. Certainly not for custom-painting a background.I told him to write bounds clearly near the code
Regrettable. Any time I do that, I specify that the code is untested. But mostly I take the time to test any code I post, except if it's cut from already-working code that I have on record.I did not check the code and wrote it
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
JButton : Background and Text
By Keys in forum AWT / SwingReplies: 1Last Post: 05-26-2011, 04:43 AM -
Adding JButton to JFrame with background
By bzknight in forum AWT / SwingReplies: 1Last Post: 01-19-2011, 06:55 PM -
why isnt my background color displaying correctly ?
By nicnicnic in forum Java 2DReplies: 8Last Post: 10-29-2009, 10: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


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks