Results 1 to 6 of 6
  1. #1
    mrbeast87 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default Can't change the color of a button...

    My code is as simple as this:

    Java Code:
    JButton b = new JButton("Blue button");
    b.setBackground(Color.blue);
    But my button won't change it's color! I'm I missing something?

    Thanks!

    PS: I'm working in mac osx.

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    It works for me:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Test1 {
    
       public static void main(String[] args) {
          JButton blueBtn = new JButton("Blue Button");
          blueBtn.setBackground(Color.blue);
          
          JButton redBtn = new JButton("Red Button");
          redBtn.setBackground(Color.red);
          
          JPanel panel = new JPanel(new GridLayout(1, 0, 5, 0));
          panel.add(blueBtn);
          panel.add(redBtn);
          
          JOptionPane.showMessageDialog(null, panel, "Blue Button Test", JOptionPane.DEFAULT_OPTION);
       }
    }
    You're bug is somewhere else in code not shown.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    You're bug is somewhere else in code not shown.
    Like maybe setting a custom Look and Feel.

    db

  4. #4
    mrbeast87 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Well, I guess the problem has to do with mac osx... Because I just copy and pasted the code curmudgeon supplied, and I got this:



    Do you have any idea why?

  5. #5
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    Same here, also OS X. Seems to be platform specific behavior.

  6. #6
    mrbeast87 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    But there must be a way to change the color of the button, right?

Similar Threads

  1. Replies: 3
    Last Post: 06-27-2010, 02:57 AM
  2. Change the color in my program
    By carl in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 12:20 PM
  3. How to Change the color of MultiColumnListBox
    By Java.child in forum AWT / Swing
    Replies: 1
    Last Post: 01-22-2009, 12:07 AM
  4. How to change string Color
    By Java.child in forum AWT / Swing
    Replies: 3
    Last Post: 01-06-2009, 04:27 AM
  5. How to change TXT color Onclick
    By dave700800 in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 01:39 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
  •