| Class 2 | Intermediate Java 30-IT-397 | 
Using Objects Instead of Strings
/**
 *
 * @author  default
 */
public class Building {
    String name;
    String function;
    /** Creates a new instance of Building */
    public Building(String name, String function)
{
        this.name = name;
        this.function = function;
}
    public void setName(String name) {
        this.name = name;
    }
    public void setFunction(String function) {
        this.function = function;
    }
    public String getFunction() {
        return function;
    }
    public String getName() {
        return name;
    }
    public String toString()
{
       
return name;
    }
}
 
import javax.swing.*;
/**
 *
 * @author  default
 */
public class JListObject extends javax.swing.JFrame
{
Building[] buildings;
    /** Creates new form JListString */
    public JListObject() {
        initComponents();
        // Create a Buildings
array.
        buildings = new Building[8];
        // Populate the array.
        buildings[0] = new Building("Sander",
"CECE Computer Labs");
        buildings[1] = new Building("French",
"CECE Administration");
        buildings[2] = new Building("Langsam",
"Library");
        buildings[3] = new Building("Lindner",
"College of Business Administration");
        buildings[4] = new Building("Tangeman",
"Old University Center");
        buildings[5] = new Building("Tent
City", "New University Center");
        buildings[6] = new Building("Shoemaker",
"Arena");
        buildings[7] = new Building("Edwards",
"Administration Center");
       
// Send the buildings array to populate the JList, instead of the String
array created earlier.
       
lstBuildings.setListData(buildings);
    }
    /** This method is called from within the constructor
to
     * initialize the form.
     * WARNING: Do NOT modify this code. The
content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        scrBuildingList = new
javax.swing.JScrollPane();
        lstBuildings = new javax.swing.JList();
        pnlButtons = new javax.swing.JPanel();
        btnShow = new javax.swing.JButton();
        lblTitle = new javax.swing.JLabel();
        addWindowListener(new
java.awt.event.WindowAdapter() {
           
public void windowClosing(java.awt.event.WindowEvent evt) {
               
exitForm(evt);
           
}
        });
scrBuildingList.setViewportView(lstBuildings);
getContentPane().add(scrBuildingList, java.awt.BorderLayout.CENTER);
        btnShow.setText("Show");
        btnShow.addActionListener(new
java.awt.event.ActionListener() {
           
public void actionPerformed(java.awt.event.ActionEvent evt) {
               
btnShowActionPerformed(evt);
           
}
        });
pnlButtons.add(btnShow);
getContentPane().add(pnlButtons, java.awt.BorderLayout.SOUTH);
        lblTitle.setText("Where
have you been?");
        getContentPane().add(lblTitle,
java.awt.BorderLayout.NORTH);
        pack();
    }
private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {
       
//  get the selected buildings from the JList.
       
Object[] selectedBuildings = lstBuildings.getSelectedValues();
       
// Create a String that we will use to display an informative message to
the user.
       
String message = "Today you have been to the ";
       
for (int i = 0; i < selectedBuildings.length; i++){
           
if (i > 0) { message += ", "; }
           
// Eliminate the for loop and if test.
           
// Just get the Building object directly from the JList.
           
Building building = (Building) selectedBuildings[i];
           
message += building.getFunction();
       
}
JOptionPane.showMessageDialog(this, message, "Buildings Visited", JOptionPane.INFORMATION_MESSAGE);
}
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent
evt) {
        System.exit(0);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new
JListObject().show();
    }
 
    // Variables declaration - do not modify
    private javax.swing.JLabel lblTitle;
    private javax.swing.JList lstBuildings;
    private javax.swing.JPanel pnlButtons;
    private javax.swing.JScrollPane scrBuildingList;
    private javax.swing.JButton btnShow;
    // End of variables declaration
}
 
Created by: Brandan Jones December 17, 2001