O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

46
|
Chapter 1, Basic JComponents
#10 Building a Drop-Down Menu Button
HACK
With the code so far, you can show the pop-up window. To close it, you
must listen for ancestor events to find out when something above the drop-
down in the component tree has changed. They all just call
hidePopup( )
to
safely turn it off:
public void ancestorAdded(AncestorEvent event){
hidePopup( );
}
public void ancestorRemoved(AncestorEvent event){
hidePopup( );
}
public void ancestorMoved(AncestorEvent event){
if (event.getSource( ) != popup) {
hidePopup( );
}
}
public void hidePopup( ) {
if(popup != null && popup.isVisible( )) {
popup.setVisible(false);
}
}
Adding a Color Selection Panel
With the DropDownComponent finished, you can finally build something with
it. For this hack, I’ve chosen a color selector. This is a small widget that lets
the user pick one of 12 standard colors without having to open up a full
color chooser. Most word processors and spreadsheets have a component
like this, so there’s no reason for Swing not to have one, too.
ColorSelectionPanel, shown in Example 1-22, is just a JPanel with a 4 × 3
grid of buttons. Each button represents one of the most common 10 colors,
plus black and white. When a color button is clicked, it will call
selectColor( ) to fire off a color selection event.
Example 1-22. A color selection panel to be used in the drop-down component
public class ColorSelectionPanel extends JPanel {
public ColorSelectionPanel( ) {
GridBagLayout gbl = new GridBagLayout( );
GridBagConstraints c = new GridBagConstraints( );
setLayout(gbl);
// reusable listener for each button
ActionListener color_listener = new ActionListener( ) {
public void actionPerformed(ActionEvent evt) {
selectColor(((JButton)evt.getSource()).getBackground( ));
}
};
Building a Drop-Down Menu Button #10
Chapter 1, Basic JComponents
|
47
HACK
ColorSelectionPanel uses a custom JButton called ColorButton (shown in
Example 1-23). It has no text and a small size so that you can fit 12 of them
inside the drop-down window. The button’s background comes from the
color it represents, and the button draws its own border, so there is no need
to draw a grid.
// set up the standard 12 colors
Color[] colors = new Color[12];
colors[0] = Color.white;
colors[1] = Color.black;
colors[2] = Color.blue;
colors[3] = Color.cyan;
colors[4] = Color.gray;
colors[5] = Color.green;
colors[6] = Color.lightGray;
colors[7] = Color.magenta;
colors[8] = Color.orange;
colors[9] = Color.pink;
colors[10] = Color.red;
colors[11] = Color.yellow;
// lay out the grid
c.gridheight = 1;
c.gridwidth = 1;
c.fill = c.NONE;
c.weightx = 1.0;
c.weighty = 1.0;
for(int i=0; i<3; i++) {
for(int j=0; j<4; j++) {
c.gridx=j;
c.gridy=i;
JButton button = new ColorButton(colors[j+i*4]);
gbl.setConstraints(button,c);
add(button);
button.addActionListener(color_listener);
}
}
}
// fire off a selectedColor property event
protected Color selectedColor = Color.black;
public void selectColor(Color newColor) {
Color oldColor = selectedColor;
selectedColor = newColor;
firePropertyChange("selectedColor",oldColor, newColor);
}
}
Example 1-22. A color selection panel to be used in the drop-down component (continued)
48
|
Chapter 1, Basic JComponents
#10 Building a Drop-Down Menu Button
HACK
To put the color selector together, you just need to pack the
ColorSelectionPanel and a status button into a DropDownComponent. You also
need to add a property change listener to detect when the user has selected a
new color and then hide the pop up. This is all handled by Example 1-24.
After building the
DropDownComponent and putting it in a standard JFrame,
your color selector will look like Figure 1-28.
Example 1-23. Custom JButton for color selection
public class ColorButton extends JButton {
public ColorButton(Color col) {
super( );
this.setText("");
Dimension dim = new Dimension(15,15);
this.setSize(dim);
this.setPreferredSize(dim);
this.setMinimumSize(dim);
this.setBorderPainted(true);
this.setBackground(col);
}
}
Example 1-24. Assembling a working color selection widget
public class DropDownTest extends JPanel {
public static void main(String[] args) {
final JButton status = new JButton("Color");
final JPanel panel = new ColorSelectionPanel( );
final DropDownComponent dropdown = new DropDownComponent(status,panel);
panel.addPropertyChangeListener("selectedColor",
new PropertyChangeListener( ) {
public void propertyChange(PropertyChangeEvent evt) {
dropdown.hidePopup( );
status.setBackground((Color)evt.getNewValue( ));
}
});
JFrame frame = new JFrame("Drop Down Test");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.getContentPane( ).setLayout(new BorderLayout( ));
frame.getContentPane( ).add("North",dropdown);
frame.getContentPane( ).add("Center",new JLabel("Drop Down Test"));
frame.pack( );
frame.setSize(300,300);
frame.show( );
}
}

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required