Hey,

I have tried everything that does not work to change the color of the text on a JButton I have in a JPanel. Does anyone know how to do this. I need the actual text on the button to be a different color than black..This is an application so I am not using public void paint( Graphics g);

buttons[1] = new JButton("Open");
buttons[1].addActionListener(this);

That is a sample of my button creation code..I just need to change the word "Open"s color... :?: :?:

Hey,

I have tried everything that does not work to change the color of the text on a JButton I have in a JPanel. Does anyone know how to do this. I need the actual text on the button to be a different color than black..This is an application so I am not using public void paint( Graphics g);

buttons[1] = new JButton("Open");
buttons[1].addActionListener(this);

That is a sample of my button creation code..I just need to change the word "Open"s color... :?: :?:

Try this complete source code :
<URL SNIPPED>

Edited 7 Years Ago by peter_budo: Keep It On The Site - Posts must contain actual content and substance and cannot simply be external links.

We value your input, but please try to keep it on the site as the forum rule request. The code could be easily copy and paste here with no need of external link

Just put the below code above the :"buttons[1] = new JButton("Open");"

UIManager.put("Button.foreground", Color.blue);

also, notice that the question was asked over four years ago.. I'm pretty sure the original poster hasn't been checking for a new answer the last couple of days

button.setOpaque(true);
button.setForeground(Color.BLUE);

it works

Edited 7 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

I know the page is old, but I just wrote this and thought I should put it on the Internet. It uses some tricky bit logic (Integer.Max_Value is all 1's) to get the absolute opposite of the current colour:

/**
     * Sets the Color of the specified button.  Sets the button text Color to the inverse of the Color.
     *
     * @param button The button to set the Color of.
     * @param color The color to set the button to.
     */
    private static void setButtonColor(JButton button, Color color)
    {
        // Sets the button to the specified Color.
        button.setBackground(color);
        // Sets the button text to the inverse of the button Color.
        button.setForeground(new Color(color.getRGB() ^ Integer.MAX_VALUE));
    }
This article has been dead for over six months. Start a new discussion instead.