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... :?: :?:

Recommended Answers

All 7 Replies

try using ... button.setForegroundColor(Color.blue);

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>

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

Member Avatar for ants280

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));
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.