JButton backgrounds

We use a lot of JButtons in our Java PDF viewer and thought that the following observations might be of help to other Java developers.

When you create a JButton it can be transparent (no background) and you can see through them or have a background color. This is done using the method setOpaque(boolean flag) where the flag is true or false. The background color is set using the method – setBackground(Color color)). So a button with a blue background would be

JButton myButton =new JButton(“press me”);

myButton.setBackground(Color.blue);

So far so good but not very exciting in an age when everyone expects lots of eye candy.

Color can be any any value, but you can also define a Color as transparent or semi-transparent. – we use this to mimic Acrobat’s see-through empty signature buttons on a PDF display.

JButton myButton =new JButton(“press me”);

myButton.setBackground(new Color(255,0,0,128));

The four values are R,G,B,A (where A is the transparency or Alpha value so the above example gives a red background which is 50% transparent). If you use integer numbers, they are all between 0 and 255.

There is one more way we could add a fancy background, with an image. This allows you to display a custom image for the Button, which can include transparency and any number of fancy eye candy.

JButton myButton =new JButton(“press me”);
myButton.setIcon(new ImageIcon(image));

In this example image can be an Image or a BufferedImage.

So we have rapidly gone from a dull, boring component to something with huge potential. We can also add some pretty cool ‘rollover’ effects – but that is for another day….

Related Posts:

  • No Related Posts
The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

5 thoughts on “JButton backgrounds

  1. Andrew Scythe

    As I tested your method in my project ( myButton.setIcon(ImageIcon) ) it does NOT set background to JButton. It adds image next to text (on the left to be exact), so it’s not a background. It’s just a picture. To be exact (once again), distance between text and image is set by setTextIconGap(int) method).

    So I ask, do You know a way to set background image to JButton instead of standard Java Steel theme?

  2. Which version of Java are you using?

  3. Andrew Scythe

    Java SE – fresh download of JDK + NetBeans (writing just in case).

  4. i dont think there is a way to add an image background.

    the only option i know is to set the icon, disable the text by setting it to null.
    then write a custom imageicon/image or icon,
    that draws the background and then draws the text ontop.

  5. Antonio Marrero

    extends from jbutton and override the paint method

    @Override
    public void paint(Graphics g) {
    if (bgImage != null) {
    g.drawImage(bgImage, 0, 0, getWidth(), getHeight(), this);
    setContentAreaFilled(false);
    setBorder(null);
    } else {
    setOpaque(true);
    }
    super.paint(g);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>