Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom Swing Component [closed]

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.

like image 894
pek Avatar asked May 05 '09 01:05

pek


2 Answers

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.

like image 145
John K Avatar answered Sep 22 '22 19:09

John K


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.

like image 40
alphazero Avatar answered Sep 24 '22 19:09

alphazero