Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacFan26

macrumors 65816
Original poster
Jan 8, 2003
1,219
1
San Francisco, California
Does anyone know if it's possible to change the background color of a Java JButton in OS X? This is a screenshot of a game I had to write for a CS project. It's like Light's Out, where the lights are green when on and black when off. In Windows it's easier to see because the entire button is the color, rather than the space in the background. Is there a way to change this in OS X?
 

Attachments

  • 1.jpg
    1.jpg
    36.4 KB · Views: 510

therevolution

macrumors 6502
May 12, 2003
468
0
Best I can come up with is to change the look and feel to Metal, so that it looks like the Windows default:
Code:
    ...
    try {
    	UIManager.setLookAndFeel(new MetalLookAndFeel());
    }	
    catch(Exception e) {}

    JButton button = new JButton(" ");
    button.setBackground(Color.red);
    ...

I'm not seeing an easy way to change button backgrounds with the OS X L&F. I'd imagine it would involve overriding ButtonUI's paint() method... no fun. Anybody have a better solution?
 

jsw

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
Share a link to the code or PM/email me with it and I'll look at it for you. But tomorrow - I'm going to bed now. Hopefully, someone can fix it for you before then - but I'd be happy to see what I can do.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
There is no way to change the button face in the Mac OS X look and feel but if you make a button with a glyph (I've forgotten the class), you can put any image you want in there.
 

MacFan26

macrumors 65816
Original poster
Jan 8, 2003
1,219
1
San Francisco, California
Thanks guys, looks like there's no simple way to do this. :( Oh well.

here's the code I was using to set the colors on the board:

for (int x = 0; x < 8; x++)
for (int y = 0; y < 8; y++)
{
if (m.isLitAt(x, y))
board[x][y].setBackground(Color.GREEN);
else
board[x][y].setBackground(Color.BLACK);
}

For the fun of it I tried doing setForeground, but alas, then there was no color :rolleyes:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.