views:

2513

answers:

6
+1  A: 

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
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
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
+3  A: 

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.

+1  A: 

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

dhiller
+2  A: 

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
Yes, I have read this book. Very good book but, as you said, it's mostly about improving the look and feel.
pek
+1  A: 

Maybe this on line book will provide some insight into the design of Swing components?

Savvas Dalkitsis
+1  A: 

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.

John K