Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Swing Chapter 10. (The basics) List Boxes. Easy for reading, Click here!

Swing Chapter 10. (The basics) List Boxes. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 2/5 


Previous Page Previous Page (1/5) - Next Page (3/5) Next Page
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.



[ Return to Swing (Book) ]


Search Books
Custom Search
Latest articles
 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 2


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy