Java Code
/*
* Example showing how to loop through a list of items stored in a List object
* @author : E.S.Boese
* @version: Spring 2005
* modified: Fall 2005 convert to swing
*/
import java.awt.*;
import javax.swing.*;
public class Debugging extends JApplet
{
JList list;
DefaultListModel model;
JTextArea textarea;
public void init( )
{
System.out.println( "A" );
setLayout( new FlowLayout( ) );
System.out.println( "B" );
setupList( );
System.out.println( "C" );
textarea = new JTextArea( 5,10 );
add(textarea);
System.out.println( "D" );
addListItemsToTextarea( );
System.out.println( "E" );
}
public void setupList( )
{
System.out.println( "F" );
model = new DefaultListModel( );
list = new JList(model);
model.addElement( "Milk" );
model.addElement( "Cookies" );
model.addElement( "Eggs" );
add(list);
System.out.println( "G" );
}
public void addListItemsToTextarea( )
{
System.out.println( "H" );
for( int i=0; i<model.getSize( ); i++ ); // getSize returns the number of items in the list
{
System.out.println( "I" );
textarea.append( (String )model.get(i) ); // grab the item at index i
textarea.append( "\n" );
}
System.out.println( "K" );
}
}
©2006 by E.S.Boese. All Rights Reserved