• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

JButton Custom Background

Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...


    Bookmark Topic Watch Topic
  • New Topic