Subpages: 1. JList API overview
2. Basic JList example
3. Custom rendering
4. Processing keyboard input and searching
5. List of check boxes
10.2 Basic JList example
This example displays a list of the united states using an array of Strings in the following format:
2-character abbreviation<tab character>full name<tab character>capital

Figure 10.1 A JList displaying a list of Strings containing tab characters.
<<file figure10-1.gif>>
The Code: StatesList.java
see \Chapter10\1
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class StatesList extends JFrame
{
protected JList m_statesList;
public StatesList() {
super("Swing List [Base]");
setSize(500, 240);
String [] states = {
"AK\tAlaska\tJuneau",
"AL\tAlabama\tMontgomery",
"AR\tArkansas\tLittle Rock",
"AZ\tArizona\tPhoenix",
"CA\tCalifornia\tSacramento",
"CO\tColorado\tDenver",
"CT\tConnecticut\tHartford",
"DE\tDelaware\tDover",
"FL\tFlorida\tTallahassee",
"GA\tGeorgia\tAtlanta",
"HI\tHawaii\tHonolulu",
"IA\tIowa\tDes Moines",
"ID\tIdaho\tBoise",
"IL\tIllinois\tSpringfield",
"IN\tIndiana\tIndianapolis",
"KS\tKansas\tTopeka",
"KY\tKentucky\tFrankfort",
"LA\tLouisiana\tBaton Rouge",
"MA\tMassachusetts\tBoston",
"MD\tMaryland\tAnnapolis",
"ME\tMaine\tAugusta",
"MI\tMichigan\tLansing",
"MN\tMinnesota\tSt.Paul",
"MO\tMissouri\tJefferson City",
"MS\tMississippi\tJackson",
"MT\tMontana\tHelena",
"NC\tNorth Carolina\tRaleigh",
"ND\tNorth Dakota\tBismarck",
"NE\tNebraska\tLincoln",
"NH\tNew Hampshire\tConcord",
"NJ\tNew Jersey\tTrenton",
"NM\tNew Mexico\tSantaFe",
"NV\tNevada\tCarson City",
"NY\tNew York\tAlbany",
"OH\tOhio\tColumbus",
"OK\tOklahoma\tOklahoma City",
"OR\tOregon\tSalem",
"PA\tPennsylvania\tHarrisburg",
"RI\tRhode Island\tProvidence",
"SC\tSouth Carolina\tColumbia",
"SD\tSouth Dakota\tPierre",
"TN\tTennessee\tNashville",
"TX\tTexas\tAustin",
"UT\tUtah\tSalt Lake City",
"VA\tVirginia\tRichmond",
"VT\tVermont\tMontpelier",
"WA\tWashington\tOlympia",
"WV\tWest Virginia\tCharleston",
"WI\tWisconsin\tMadison",
"WY\tWyoming\tCheyenne"
};
m_statesList = new JList(states);
JScrollPane ps = new JScrollPane();
ps.getViewport().add(m_statesList);
getContentPane().add(ps, BorderLayout.CENTER);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
}
public static void main(String argv[]) {
new StatesList();
}
}
Understanding the Code
Class StatesList
Class StatesList extends JFrame to implement the frame container for this example. One instance variable, JList m_statesList, is used to store an array of state Strings (as described above). This list is created by passing the states String array to the JList constructor. It is then added to a JScrollPane instance to provide scrolling capabilities.
Running the Code
Figure 10.1 shows StatesList in action displaying the list of states and their capitals. Note that the separating tab character is displayed as an unpleasant square symbol (we'll fix this in the next example).
UI Guideline : Unbalanced Layout
In this example, the design is unblanced due to the tab character not being displayed correctly. The box is ugly but the spacing is also wrong. The large whitespace area to the right ought to be avoided. The next example corrects this.



RSS feed Java FAQ News