How to create a custom Swing Component

pek picture pek · May 5, 2009 · Viewed 47.9k times · Source

I've always wanted to create custom components in Java, or customize existing ones, but my searches never resulted in anything useful. So I decided to ask the StackOverflow community:

Where can I find information about customizing Java GUI Components in general?

And when I mean customizing, I'm not talking about changing colors, fonts etc. I mean really customize them. Here are two mockup example components:

Custom Components Mockup

Notes
I started this question mainly to find how to create the above two custom components. But then I realized that there isn't a general question about hacking swing components. So I thought it would be better to have a list of resources.

In case you are wondering how do the two components in the mockup work, here it is:

A customized JScrollPane that has two Scrollbars for each orientation. Each scrollbar can act differently. For example, the outer ones scroll normally and the inner ones move the view in a more Picasa-like way. I actually got a working(!) answer from google groups here, but that was just code.

The second one is a custom JComboBox which, when the popup list is expanded, has a JComponent at the end. This can be anything, from a simple JLabel with an icon, to a JButton that manipulates the list in a way.

But this question isn't about those specific components. It's about finding resources (websites, books, articles etc.) to learn how to create them.

Answer

John K picture John K · Jun 24, 2010

This article, How To Write a Custom Swing Component should be really helpful. It covers replicating a Windows slider control that has both discrete steps and a continuous range.