Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Swing / AWT / SWT and the fly likes JButton Custom Background Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton Custom Background" Watch "JButton Custom Background" New topic

JButton Custom Background

Cliff Archibald

Joined: Oct 13, 2004
Posts: 2
I am designing a GUI that will be run on a LCD display with poor performance at large viewing angles... for this reason it has been requested that the backgrounds of my buttons , which when active are usually light gray (75,75,75), be a stiple pattern (pixel checkerboard) of white (255,255,255) and a darker gray (150,150,150) that will appear light gray and have a more consistent look at large viewing angles...

Currently I am simply changing my backgound colors with

this.setBackground(Color.BLACK); , etc..

My Buttons text are dynamic and get changed with .setText() , and are used as a part of an actionlistener in my app that uses the getActionCommand() from the event to do the appropriate action...

Also I am adding and removing borders based on the status of the button using methods such as:

this.setBorder( BorderFactory.createLineBorder(Color.white,2)); which cannot be affected by the solution... or must be integrated into it..

Is there any way to paint a pattern instead of a solid color for the background fill?

I prefer to code it so that the pattern fill colors can be adjusted via programming , vs. using an image background using .setIcon() ...

I am think something along the lines of a method like .setBackgroundFill ( Color, Color) that get added to my class which extends JButton...

Although I did try using setIcon() with an ImageIcon and noticed that it didn't work as a Button Background, but was over my button text... I can't add the button text to the image icon due to run time configuration differences...

Any help would be highly appreciated... I am assumeing I proabbly need to override some paint method.. but that about where I get lost... and there may be a simple way to do the pattern I couldn't find in the api...
Cliff Archibald

Joined: Oct 13, 2004
Posts: 2
It's amazing what you can figure out just by taking the time to ask a question...

I have added this to my class that extends JButton...

This appears to be a good framework for what I want, it just leaves me with 2 issues...

1) How do I use graphics methods to paint a checkboard of two colors on a pixel by pixel basis instead of a solid fill , Should I try to paint it point by point? seems like that would take a lot of proccessor time.... 10,875 pixels in my 145 x 75 buttons...


2) When I do use this code the rectangle fill is painted over the text of the JButton... How can I bring that text in front... I tried calling super.paintComponent(g) AFTER my fill , but it appears that the first thing it does it clear out whatevers there.. so the rectangle fill gets wiped out...


I agree. Here's the link:
subject: JButton Custom Background
It's not a secret anymore!