By: Nicholas Duchon.
Here are a few lectures on the ListDemo class in Liang 7, pp 536-7:
I hope you find these helpful.
import
java.awt.GridLayout;
import
java.awt.BorderLayout;
import
javax.swing.JFrame;
import
javax.swing.JList;
import
javax.swing.JPanel;
import
javax.swing.JLabel;
import
javax.swing.JScrollPane;
import
javax.swing.ImageIcon;
import
javax.swing.SwingConstants;
import
javax.swing.event.ListSelectionListener;
import
javax.swing.event.ListSelectionEvent;
final static int NUMBER_OF_FLAGS = 9;
final static JLabel nulljl = new JLabel ();
// Declare an
array of Strings for flag titles
private String[] flagTitles = {"Canada", "China", "Denmark",
"France", "Germany", "India", "Norway", "United Kingdom",
"United States of America"
}; // end immediate initialization of String array
// The list for
selecting countries
private JList jlst = new JList(flagTitles);
// Declare an
ImageIcon array for the national flags of 9 countries
private JLabel[] imageLabels = {
new JLabel (new ImageIcon("flags/ca.gif"
)),
new JLabel (new ImageIcon("flags/china.gif" )),
new JLabel (new ImageIcon("flags/denmark.gif")),
new JLabel (new ImageIcon("flags/fr.gif"
)),
new JLabel (new ImageIcon("flags/germany.gif")),
new JLabel (new ImageIcon("flags/india.gif" )),
new JLabel (new ImageIcon("flags/norway.gif" )),
new JLabel (new ImageIcon("flags/uk.gif"
)),
new JLabel (new ImageIcon("flags/us.gif"
))
}; // end immediate initialization of ImageIcon array
// Arrays of
labels for displaying images
private JLabel[] jlblImageViewer = new JLabel[NUMBER_OF_FLAGS];
ListDemo frame = new ListDemo();
frame.setSize(650, 500);
frame.setTitle("ListDemo");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// end main
//
Create a panel to hold nine labels
JPanel p = new JPanel(new GridLayout(3, 3, 5, 5));
for (int i = 0; i < NUMBER_OF_FLAGS; i++) {
p.add(jlblImageViewer[i] = new JLabel());
jlblImageViewer[i].setHorizontalAlignment
(SwingConstants.CENTER);
} // end instantiate JLabels, empty, centered and added to image
panel
//
Add p and the list to the frame
add(p, BorderLayout.CENTER); // image panel
add(new JScrollPane(jlst), BorderLayout.WEST); // selection
panel
//
Register listeners
jlst.addListSelectionListener
(
// instatiate anonymous class (ListDemo$1) which implements
ListSelectionListener interface
new ListSelectionListener()
// Define anonymous inner class (ListDemo$1) immediately
{
/** Handle list selection */
public void valueChanged(ListSelectionEvent e) {
// Get selected indices
int[] indices = jlst.getSelectedIndices();
int i;
// Set icons in the labels
for (i = 0; i < indices.length; i++) {
jlblImageViewer[i] = imageLabels [indices[i]];
} // end for each selectected image
// Remove icons from the rest of the labels
for (; i < NUMBER_OF_FLAGS; i++) {
jlblImageViewer[i] = nulljl;
} // end for the rest of the locations
} // end method valueChanged - interface specified method
} // end anonymous inner class immediate definition
); // end addListSelectionListener method parameter list
}
// end constructor
} // end class
List Demo
ND.