Animate Your JList Selections #18
Chapter 2, Lists and Combos
|
87
HACK
H A C K
#18
Animate Your JList Selections
Hack #18
Fading in and catching the eye.
Not every GUI involves windows and mouse pointers, and the visual lan-
guage of a GUI can be very different depending on what is provided by the
environment. Typically, GUIs for things like console video games and set-
top boxes don’t use a mouse metaphor, so there’s no onscreen pointer that
the user is tracking. As a result, these systems often give the user more pro-
found feedback when they move around a list—highlights slide from one
item to another, selected items fade in while deselected items fade out, etc.—
so there’s something the eye can track. You can do the same thing in Swing,
with more cell-rendering hackery. You might not need it now, but it’ll be
handy if you ever design a kiosk with Swing.
One way to show a changed selection is to show a brief animation of the cell
selection. Instead of just being highlighted instantly, you fade the selected
cell from its unselected background and foreground colors to its selected
colors over the course of a short time (really short, like a half-second, so it
isn’t annoying).
To do this, you’ll need to create an animator thread that kicks off every time
the selection changes. This short-lived thread repeatedly updates a high-
light color and calls
repaint( ). The cell renderer can then use the updated
highlight color as it redraws the cells in the list. Example 2-18 shows this
technique.
Figure 2-10. Item dropped into new position in a JList
Example 2-18. Animating the JList cell selection
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
88
|
Chapter 2, Lists and Combos
#18 Animate Your JList Selections
HACK
public class AnimatedJList extends JList
implements ListSelectionListener {
static java.util.Random rand = new java.util.Random( );
static Color listForeground, listBackground,
listSelectionForeground, listSelectionBackground;
static float[] foregroundComps, backgroundComps,
foregroundSelectionComps, backgroundSelectionComps;
static {
UIDefaults uid = UIManager.getLookAndFeel().getDefaults( );
listForeground = uid.getColor ("List.foreground");
listBackground = uid.getColor ("List.background");
listSelectionForeground = uid.getColor ("List.selectionForeground");
listSelectionBackground = uid.getColor ("List.selectionBackground");
foregroundComps =
listForeground.getRGBColorComponents(null);
foregroundSelectionComps =
listSelectionForeground.getRGBColorComponents(null);
backgroundComps =
listBackground.getRGBColorComponents(null);
backgroundSelectionComps =
listSelectionBackground.getRGBColorComponents(null);
}
public Color colorizedSelectionForeground,
colorizedSelectionBackground;
public static final int ANIMATION_DURATION = 1000;
public static final int ANIMATION_REFRESH = 50;
public AnimatedJList( ) {
super( );
addListSelectionListener (this);
setCellRenderer (new AnimatedCellRenderer( ));
}
public void valueChanged (ListSelectionEvent lse) {
if (! lse.getValueIsAdjusting( )) {
HashSet selections = new HashSet( );
for (int i=0; i < getModel().getSize( ); i++) {
if (getSelectionModel( ).isSelectedIndex(i))
selections.add (new Integer(i));
}
CellAnimator animator = new CellAnimator (selections.toArray( ));
animator.start( );
}
}
Example 2-18. Animating the JList cell selection (continued)
Animate Your JList Selections #18
Chapter 2, Lists and Combos
|
89
HACK
public static void main (String[] args) {
JList list = new AnimatedJList ( );
DefaultListModel defModel = new DefaultListModel( );
list.setModel (defModel);
String[] listItems = {
"Chris", "Joshua", "Daniel", "Michael",
"Don", "Kimi", "Kelly", "Keagan"
};
Iterator it = Arrays.asList(listItems).iterator( );
while (it.hasNext( ))
defModel.addElement (it.next( ));
// show list
JScrollPane scroller =
new JScrollPane (list,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JFrame frame = new JFrame ("Checkbox JList");
frame.getContentPane( ).add (scroller);
frame.pack( );
frame.setVisible(true);
}
class CellAnimator extends Thread {
Object[] selections;
long startTime;
long stopTime;
public CellAnimator (Object[] s) {
selections = s;
}
public void run( ) {
startTime = System.currentTimeMillis( );
stopTime = startTime + ANIMATION_DURATION;
while (System.currentTimeMillis( ) < stopTime) {
colorizeSelections( );
repaint( );
try { Thread.sleep (ANIMATION_REFRESH); }
catch (InterruptedException ie) {}
}
// one more, at 100% selected color
colorizeSelections( );
repaint( );
}
// colorizeSelections( ) listing below
// AnimatedCellRenderer listing below
}
Example 2-18. Animating the JList cell selection (continued)

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.