954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

ZEEPLE
Newbie Poster
15 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

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

nanosani
Unauthenticated Liar
Team Colleague
1,830 posts since Jul 2004
Reputation Points: 45
Solved Threads: 56
 

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 :

carnado2008
Newbie Poster
2 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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

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

chetan_8187
Newbie Poster
9 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
button.setOpaque(true);
button.setForeground(Color.BLUE);


it works

safadi
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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));
    }
ants280
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You