mattbriançon

Transparent JButtons

May 17th, 2007 by matt

A few hours ago I was trying to create a toolbar for a new GUI in Java. Making square buttons, etc. was easy but what I couldn’t figure out was how just have the icon present without any beveledness. I searched for a while and finally came across a nice solution.

After creating the new button (JButton button = new JButton(new ImageIcon("path/to/icon");) all it takes is two simple method calls to make it just an icon. The first, button.setBorder(null);, is fairly self-explanatory but the second, button.setContentAreaFilled(false);, while almost as self-explanatory, is not obvious at all. In fact, I read through the API twice before I caught it.

I still have one problem though. This solution works well if you want just the icon present however it breaks if you try to add text. This is rather important in my app so I’m going to have to work out a solution.

I’ve actually learned a few neat tricks recently which I’m going to post here. If you’re at all interested, stay tuned.

No Comments

Leave A Comment