Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change text color of a JButton

I am writing a simple minesweeper game and it works now, but I am working on the pretty details like making each number a different color.

I keep running into errors when I try to set the text color on the JButton. I can change the text easily enough and the background, but not the text color specifically.

The part that keeps getting all mucked up is:

total = Integer.toString(count);
jb.setText(total);              
if(count == 1)
    jb.setTextColor(Color.blue);
if(count == 2)
    jb.setTextColor(Color.green);
if(count == 3)
    jb.setTextColor(Color.red);

For some reason my error is:

MS.java:109: error: cannot find symbol
                    jb.setTextColor(Color.blue);
                      ^
  symbol:   method setTextColor(Color)
  location: variable jb of type JButton
MS.java:112: error: cannot find symbol
                    jb.setTextColor(Color.green);
                      ^
  symbol:   method setTextColor(Color)
  location: variable jb of type JButton
MS.java:114: error: cannot find symbol
                    jb.setTextColor(Color.red);
                      ^
  symbol:   method setTextColor(Color)
  location: variable jb of type JButton
3 errors
Process javac exited with code 1

This occurs whenever I try to compile, but when I change it to say setBackgroundColor instead of setTextColor it works just fine.

like image 267
Kurt E Avatar asked Mar 13 '13 18:03

Kurt E


People also ask

How do I change the color of text in a JButton?

To set the JButton text color, you can use setForeground .

How do I change the text of a JButton?

By default, we can create a JButton with a text and also can change the text of a JButton by input some text in the text field and click on the button, it will call the actionPerformed() method of ActionListener interface and set an updated text in a button by calling setText(textField.

How do you color a button in Java?

Use the setBackground method to set the background and setForeground to change the colour of your text.

What does JButton Icon mean?

The JButton class is used to create a labeled button that has platform independent implementation. The application result in some action when the button is pushed. It inherits AbstractButton class.


1 Answers

setTextColor is undefined for JButton. To set the JButton text color, you can use setForeground.

button.setForeground(Color.RED);
like image 129
Reimeus Avatar answered Oct 20 '22 10:10

Reimeus