Welcome to Marlon's place!

Message of the day

"There is a brave man waiting, who'll take me back to where I come from"

(The Black Heart Rebellion)

How to create a borderless JButton

posted: July 7, 2009

Sometimes you want to use a JButton that does not look like a button. The most common example is probably the link to the company homepage, to be embedded somewhere in your application.

We've all seen them: a button that does not resemble a traditional button, but looks more like a hyperlink you encounter on the internet.

It's a nice-to-have feature, and it's easily achieved by using the JButton class which is already present in Java.

The following snippet will show you how to create a borderless JButton which looks like a clickable link, meanwhile preserving the possibilities a JButton offers.

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class BorderlessJButtonExample extends JFrame
  public BorderlessJButtonExample()
    super("Borderless JButton example");
    this.getContentPane().setLayout(new FlowLayout());
    this.getContentPane().add(new JLabel("Website:"));
    JButton button = new JButton("http://www.bodicker.be/");
    button.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    button.addActionListener(new ActionListener()
      public void actionPerformed(ActionEvent ae)
        System.out.println("Link clicked!");
  public static void main(String[] args)
    new BorderlessJButtonExample();

To make the JButton look like a clickable link, there are a few things that needed to be changed, as you can see in the snippet above. Let's have a look at the different things that need to be taken care of:

  1. button.setForeground(Color.blue);

    After the button has been created, the colour is changed to blue in order to make it look more like the default hyperlink.

  2. button.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

    Creating a new border for the JButton is not really necessary, but keep in mind that a JButton is a bit larger than a JLabel for instance. Giving the JButton a smaller border results in a smaller empty area around the link.

  3. button.setBorderPainted(false);

    The JButton's default border needs to be suppressed - so here we specify that the border itself will not be painted. At this point, we have a JButton that resembles a JLabel.

  4. button.setContentAreaFilled(false);

    At first you will not notice any difference after adding this statement - it's only when you click the link that you will see why this is needed. Clicking the link still makes the JButton "react" by changing the background colour to a darker grey. If the content area is not filled, this no longer happens.

  5. button.setFocusPainted(false);

    Last but not least: should the JButton receive focus, the focus should not be painted. By choosing not to paint it, we can get rid of that little line around the link text.

Ok, so now that we know how to create a "clickable link" in our Swing applications, it's time to see what we can do with it.

The example I have given above was the ability to open the default browser window with a given URL. This may not seem like such a hard thing to do, but keep in mind that Java is a platform independent language. As a result of this, not only the operation of opening the browser window will be different from platform to platform, the available browsers will differ as well.

After a lot of research, I've found the best option to be the open source project "Bare bones browser launch for Java" - not only is it free, but it works on Mac OS X, GNU/Linux, Unix (Solaris) and Windows.

As far as browsers are concerned, both Opera and Firefox are supported, as well as Konqueror, Epiphany, Mozilla and Netscape.

Previous articles

Recent C++ stuff

Recent Delphi stuff

Recent Java stuff