38

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.

Ilmari Karonen
  • 44,762
  • 9
  • 83
  • 142
pek
  • 16,939
  • 27
  • 83
  • 98

6 Answers6

19

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.

user11153
  • 6,686
  • 4
  • 44
  • 45
John K
  • 2,032
  • 2
  • 16
  • 19
  • 3
    This link is out of date. Though the actual page still exists, you'll find the images and links within the page are all missing. Does anyone have an updated link? Also, generally, on SO we put the relevant content from the link in the answer precisely for cases like this where the link becomes out of date. – Jason Mar 14 '16 at 14:48
15

The JDK is open source. To understand how you write a Swing component, there is no better place to look than the source for Swing components. In general, when you create custom components, you are interested in one or more of the following: {interaction semantics, visual representation, compositional structure}

Start with the simplest cases:

Passive components: JLabel
Interactive components: JButton

JLabel will demonstrate the barebones extension of JComponent for custom drawing of a component; in this case, it is drawing the 'text' string value as its representation.

JButton will demonstrate the barebones extension for user interaction in addition to drawing the component.

Amanda S
  • 3,178
  • 4
  • 31
  • 45
alphazero
  • 26,728
  • 3
  • 28
  • 26
  • 6
    A couple of caveats to keep in mind when reading Swing code: there is a lot of code in there for backwards compatibility; most of the actual graphics code is in the UI classes; the Swing classes have "evolved" and are not necessarily representative of best practices. – Devon_C_Miller Jul 27 '11 at 02:36
  • Web address for the component code –  May 05 '14 at 17:38
10

Filthy Rich Clients is an excellent, very readable book on this topic. It's more about improving the look and feel of Swing components than creating entirely new ones, but it does get into the guts of Swing and provides a lot of practical examples.

Amanda S
  • 3,178
  • 4
  • 31
  • 45
  • Yes, I have read this book. Very good book but, as you said, it's mostly about improving the look and feel. – pek May 07 '09 at 05:28
4

This is a pretty open question, the short simple answer is that you subclass JComponent or one of it's descendants and add the functionality that you require. Obviously, depending on what you're wanting to do this may vary in difficulty. For starters I recommend you read Sun's tutorial on using Swing components where there are examples on how to use scroll panes and popup menus. Once you have read through those and experimented you might have more specific questions that will be easier for people to provide considered answers to.

If the two components in your mock up are all you are wanting to implement then giving us some better idea of how you want them to function will yield better answers.

ninesided
  • 22,501
  • 13
  • 78
  • 106
  • I have read the Java Tutorial (and always do when it comes to Swing), but it doesn't quite go as deep as I need. – pek May 05 '09 at 02:15
  • can you be more specific in explaining what it is you need then? Are you wanting to know specifically how to achieve the functionality that you've mocked up in the question or is the question intended to be more general? – ninesided May 05 '09 at 02:31
2

Personally I find the above answers too general or vague. Also, as someone is inevitably going to bump into this thread just as I did, I might as well provide what I found:

Hope this helps...

kralyk
  • 3,703
  • 1
  • 26
  • 30
1

Read Swing Hacks: Tips & Tools for Building Killer GUIs (Marinacci, Adamson). IMHO an essential book for effectively working with swing in general.

Daniel Hiller
  • 3,245
  • 3
  • 21
  • 33