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

Thread: JButton background not displaying (FRUSTRATING)

  1. #1
    seabass341 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Angry 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.

    JButton background not displaying (FRUSTRATING)-button-agh.png

    Here's an excerpt of my code.
    Java Code:
    colorIcon = new JButton();
    colorIcon.setOpaque(true);
    colorIcon.setBackground(Color.BLACK);
    colorIcon.setForeground(Color.BLACK);
    I have tried multiple different settings for the button, including "setContentAreaFilled(true)," but I can't seem to get it to work.

    Very frustrating, any ideas? Thanks.

  2. #2
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default 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.

  3. #3
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: JButton background not displaying (FRUSTRATING)

    you code works in my computer :D

  4. #4
    seabass341 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default 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.

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

    Default 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.
    Java 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);
      }
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: JButton background not displaying (FRUSTRATING)

    Quote Originally Posted by Grkn View Post
    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.
    1. As said, never never never use getGraphics() of a Component.
    2. Where did you find a method Graphics#fillRect(Rectangle)?
    3. How do you call the protected method paintComponent from your code?

    Please post compilable advice.

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

  7. #7
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default 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

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

    Default Re: JButton background not displaying (FRUSTRATING)

    Quote Originally Posted by Grkn View Post
    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
    It's never recommended to use getGraphics() of a Component except for extremely advanced performance intensive applications.

    No idea what you mean by
    I told him to write bounds clearly near the code
    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 did not check the code and wrote it
    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.

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

Similar Threads

  1. JButton : Background and Text
    By Keys in forum AWT / Swing
    Replies: 1
    Last Post: 05-26-2011, 04:43 AM
  2. Adding JButton to JFrame with background
    By bzknight in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2011, 06:55 PM
  3. Replies: 8
    Last Post: 10-29-2009, 10:54 AM
  4. Replies: 4
    Last Post: 03-21-2009, 05:48 PM
  5. JButton onClick change color background
    By behrk2 in forum AWT / Swing
    Replies: 6
    Last Post: 07-09-2008, 04:54 PM

Tags for this Thread

Posting Permissions

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