JButton.setBackground(Color color) not working

With JDK 1.4.2, my JButtons below have a blue background. When I compile with 1.5, they are gray. I tried using setOpaque(true) on one of them below. It didn't work.Any ideas?

class MainMenuextends JFrame

{

public MainMenu()throws Exception

{

super("");// no title on main menu

JPanel pane =new JPanel(null,true);

pane.setBackground(new Color(4194496));

Icon icon =new ImageIcon(BizThriveMenu.getResourceDir()+"\\BizThriveSmall.gif");

JLabel lImage =new JLabel(icon);

JButton bEditCustomers =new JButton("<html><FONT COLOR=WHITE>Customers</FONT></html>");

bEditCustomers.setOpaque(true);

bEditCustomers.setBackground(new Color(4194496));

JButton bAccounting =new JButton("<html><FONT COLOR=WHITE>Accounting</FONT></html>");

bAccounting.setBackground(new Color(4194496));

JButton bEditReminders =new JButton("<html><FONT COLOR=WHITE>Reminders</FONT></html>");

bEditReminders.setBackground(new Color(4194496));

JButton bPublish =new JButton("<html><FONT COLOR=WHITE>Publish</FONT></html>");

bPublish.setBackground(new Color(4194496));

JButton bExit =new JButton("<html><FONT COLOR=WHITE>Exit</FONT></html>");

bExit.setBackground(new Color(4194496));

bEditCustomers.addActionListener(

new ActionListener(){

publicvoid actionPerformed(ActionEvent actionEvent){

try{

new TableListFrame(MainMenu.this,"customers");

}

catch (Exception e){

e.printStackTrace();

}

}

}

);

bAccounting.addActionListener(

new ActionListener(){

publicvoid actionPerformed(ActionEvent actionEvent){

try{

new AccountCategoryListFrame(MainMenu.this,"accountCategories");

}

catch (Exception e){

e.printStackTrace();

}

}

}

);

bEditReminders.addActionListener(

new ActionListener(){

publicvoid actionPerformed(ActionEvent actionEvent){

try{

new CurrentRemindersFrame(MainMenu.this);

}

catch (Exception e){

e.printStackTrace();

}

}

}

);

bPublish.addActionListener(

new ActionListener(){

publicvoid actionPerformed(ActionEvent actionEvent){

try{

new Designer(MainMenu.this);

}

catch (Exception e){

e.printStackTrace();

}

}

}

);

bExit.addActionListener(

new ActionListener(){

publicvoid actionPerformed(ActionEvent actionEvent){

System.exit(0);

}

}

);

Font buttonFont = bEditCustomers.getFont();

buttonFont =new Font("Times New Roman", Font.ITALIC+Font.BOLD, 24);

bEditCustomers.setFont(buttonFont);

bAccounting.setFont(buttonFont);

bEditReminders.setFont(buttonFont);

bPublish.setFont(buttonFont);

bExit.setFont(buttonFont);

pane.add(lImage);

pane.add(bEditCustomers);

pane.add(bAccounting);

pane.add(bEditReminders);

pane.add(bPublish);

pane.add(bExit);

int appWidth = 500;

int appHeight = 700;

this.setSize(new Dimension(appWidth,appHeight));

this.setResizable(false);

this.getContentPane().add(pane);

Insets insets = pane.getInsets();

lImage.setBounds(((appWidth-4-icon.getIconWidth())/2)+insets.left, 35+insets.top, icon.getIconWidth(), icon.getIconHeight());

bEditCustomers.setBounds(((appWidth-4-235)/2)+insets.left, 200 + insets.top, 235, 40);

bAccounting.setBounds(((appWidth-4-235)/2)+insets.left, 250 + insets.top, 235, 40);

bEditReminders.setBounds(((appWidth-4-235)/2)+insets.left, 300 + insets.top, 235, 40);

bPublish.setBounds(((appWidth-4-235)/2)+insets.left, 350 + insets.top, 235, 40);

bExit.setBounds(((appWidth-4-235)/2)+insets.left, 400 + insets.top, 235, 40);

//center application window on desktop

Dimension screenSize =null;;

screenSize = getToolkit().getScreenSize();

this.setBounds((screenSize.width - appWidth)/2,

(screenSize.height - appHeight)/2, appWidth, appHeight);

// make the frame visible

this.setVisible(true);

}

[7497 byte] By [OrlandoJima] at [2007-10-3 4:05:12]
# 1

If you want to change the color of the text on a button then just use setForeground( Color.WHITE );

> When I compile with 1.5, they are gray.

I don't use 1.5 so I can't really help you, but neither can anybody else since the code you posted doesn't compile. So we can't see if its a problem with your code or your version and platform.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-14 22:04:28 > top of Java-index,Desktop,Core GUI APIs...
# 2

works OK for me (1.5.0_05)

import javax.swing.*;

import java.awt.*;

class Testing extends JFrame

{

public Testing()

{

JButton b1 = new JButton("B1");

JButton b2 = new JButton("B2");

JButton b3 = new JButton("B2");

b2.setBackground(Color.RED);

b3.setBackground(new Color(4194496));

getContentPane().add(b1,BorderLayout.NORTH);

getContentPane().add(b2,BorderLayout.CENTER);

getContentPane().add(b3,BorderLayout.SOUTH);

pack();

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String[] args){new Testing().setVisible(true);}

}

Michael_Dunna at 2007-7-14 22:04:28 > top of Java-index,Desktop,Core GUI APIs...
# 3

camickr:

1) Though the information about setting the text color is helpful, the background color of the button is the subject of the posting. Please limit your replies to the subject of the posting.

2) Regarding, "I don't use 1.5 so I can't really help you, but neither can anybody else...", please read Michael_Dunn's post, who remarkably was able to provide help. Now, I can try with 1.5.0_05 and see if my code still works. If it does, there may be an issue with my 1.5.0_07 build. If I still have a problem, there may be other factors I need to look for. (I don't know how you did it, Michael, but THANK YOU!)

3) There's no need to "guess" at what the code snipet I gave is doing, especially in regards to the JButtons...again, the topic of my post. The code clearly instantiates JButtons, sets their background color, changes their font, and adds them to the pane. As I mentioned, the background color is ignored. If you know a more "proper" way to set a button's background color or know of a particular issue with 1.5, please let me know. Otherwise, it's alright not to reply to a subject. Nobody will think less of you.

4) Your comment, "And don't forget to use the Code Formatting Tags so the code retains its original formatting." The link you supplied takes me to a page that explains the tags, which I used. This may explain why the code I supplied retained "it's original formatting."

OrlandoJima at 2007-7-14 22:04:28 > top of Java-index,Desktop,Core GUI APIs...
# 4

Michael,

Your snipped worked fine in 1.5.0_07, so I wondered if it was a WindowsLookAndFeel issue in 1.5. I found another post,

http://forum.java.sun.com/thread.jspa?threadID=556973&messageID=2732204

...which concludes that it is a "Windows XP's Visual Styles" issue, not a Swing issue at all.

Following the links on the other post, I came to this quote:

Changing the appearance of a button can always cause conflicts with the current L&F implementation. The Windows L&F for Swing tries to be as close as possible to the native display. On XP, we use the built-in bitmap resources for the buttons. These can not be colorized, just like in the native API.

You should call setContentAreaFilled(false) on the button to avoid having the L&F paint its decorations. This has the side effect that the button's opaque property is set to false, so you need to follow that call with a call to setOpaque(true).

As it turns out, the machine where I was using 1.4 was running Windows 2000, and it worked there. whereas the machine I tried running the code with 1.5 was an XP machine. So, changing to an XP machine is what made the difference, not the JVM version.

A comment on the quote above: setContentAreaFilled(false) keeps even the button borders from being drawn, which I need.Another suggestion made in the other post is to use WindowsClassicLookAndFeel instead of WindowsLookAndFeel. This gives me the results I'm looking for on both XP and 2000.

OrlandoJima at 2007-7-14 22:04:28 > top of Java-index,Desktop,Core GUI APIs...
# 5

As a newbie to the forum you obviously didn't understand how to post a simple direct question, with only the relevant code to demonstrate the problem, which is why I provided you with all the extra information to help you get better answers in the future.

Did you bother to read the link of creating an SSCCE?

Your question is about setting the background color of a button. 99% of the code you posted is not related to that issue. Keep the code simple so we don't waste time reading through unnecessary code. That way we can determine whether the problem is with your code or the environment as I already mentioned.

> 1) Though the information about setting the text color is helpful, the

> background color of the button is the subject of the posting. Please

> limit your replies to the subject of the posting.

If the code posted was only related to the background color then we wouldn't have this issue now would we? When I see something strange or wrong I make a comment whether it is directly related to the question or not. It has been my experience that most people appreciate the bonus advice, so I will not change my behaviour for you. If you don't like the advice, then just ignore it.

> 2) Regarding, "I don't use 1.5 so I can't really help you, but neither can

> anybody else...", please read Michael_Dunn's post, who remarkably

> was able to provide help.

My comment was related to the code you provided. Nobody could use the code to compile and execute to see if it ran on their machine. So, Michael, had to waste time creating his own simple test program to see if it worked, which it did.

My point was if you provided the same simple demo program that Michael did, then all he (or anybody else) would have had to do would be to copy, compile and test the program. You should be spending the time creating the simple test program not each of us individually. Think of how much time is wasted if everybody who wanted to help you had to create their own program? Thats why I included the link on creating an SSCCE, so the next time we don't need to waste time.

> 3) ..... Otherwise, it's alright not to reply to a subject. Nobody will think less of you.

Of course I don't have to replay to every question (and I don't), but if you don't learn to post a question properly you will make the same mistake again and again. That was the context of my answer. It was designed to help you ask a better question the next time.

> 4) Your comment, "And don't forget to use the Code Formatting Tags > so the code retains its original formatting."

That was a standard message I use for many newbies to the forum. I'm amazed at the number of people who think we are mind readers and can guess exactly what is causing the problem without posting any code. So I got tired of typing in a different message every time and I now just cut and paste. Yes I could have edited the last line out, but you would be amazed at how many people format the code the first time but then forget to do it when prompted for more detail. So I just keep it in all messages.

Please keep your comments related to the problem. I think I've learned a think or two over the years on how to ask and answer a question.

camickra at 2007-7-14 22:04:28 > top of Java-index,Desktop,Core GUI APIs...