Class 2 | Intermediate Java 30-IT-397 |
ListModel Example
import javax.swing.*;
/**
*
* @author default
*/
public class JListObjectModel extends javax.swing.JFrame
{
DefaultListModel buildingModel;
/** Creates new form JListString */
public JListObjectModel() {
initComponents();
// We don't need the Array any more.
// Let's just use the DefaultListModel instead.
buildingModel = new DefaultListModel();
// Build the objects on the fly and add them to the model.
buildingModel.addElement(new Building("Sander", "CECE Computer Labs"));
buildingModel.addElement(new Building("French", "CECE Administration"));
buildingModel.addElement(new Building("Langsam", "Library"));
buildingModel.addElement(new Building("Lindner", "College of Business Administration"));
buildingModel.addElement(new Building("Tangeman", "Old University Center"));
buildingModel.addElement(new Building("Tent City", "New University Center"));
buildingModel.addElement(new Building("Shoemaker", "Arena"));
buildingModel.addElement(new Building("Edwards", "Administration Center"));
// Change setListData(buildings) to setModel(buildingModel)
lstBuildings.setModel(buildingModel);
}
/** 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();
btnAdd = 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);
btnAdd.setText("Add Building");
btnAdd.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
pnlButtons.add(btnAdd);
getContentPane().add(pnlButtons, java.awt.BorderLayout.SOUTH);
lblTitle.setText("Where
have you been?");
getContentPane().add(lblTitle,
java.awt.BorderLayout.NORTH);
pack();
}
private void btnAddActionPerformed(java.awt.event.ActionEvent
evt) {
// Prompt the user for building name, function
String buildingName = JOptionPane.showInputDialog(this, "Enter the building
name:");
String buildingFunction = JOptionPane.showInputDialog(this, "Enter the
building function:");
// Create a new building object based on the user's input.
Building building = new Building(buildingName, buildingFunction);
// Add this building to the model.
buildingModel.addElement(building);
}
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 JListObjectModel().show();
}
// Variables declaration - do not modify
private javax.swing.JLabel lblTitle;
private javax.swing.JList lstBuildings;
private javax.swing.JPanel pnlButtons;
private javax.swing.JButton
btnAdd;
private javax.swing.JScrollPane scrBuildingList;
private javax.swing.JButton btnShow;
// End of variables declaration
}
Created by: Brandan Jones December 17, 2001