Class 2 | Intermediate Java 30-IT-397 |
Example - Source Code and Screen Capture
/**
*
* @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;
}
}
import javax.swing.*;
/**
*
* @author default
*/
public class JListString extends javax.swing.JFrame {
Building[] buildings;
/** Creates new form JListString */
public JListString() {
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");
// Manufacture a String
array from the above building array for display in the JList.
String[] buildingNames
= new String[buildings.length];
for (int k = 0; k <
buildings.length; k++) {
buildingNames[k] = buildings[k].getName();
}
lstBuildings.setListData(buildingNames);
}
/** 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.setName("null");
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 += ", "; }
String buildingName = (String) selectedBuildings[i];
for (int j = 0; j < buildings.length; j++) {
if (buildingName.equals(buildings[j].getName())) {
message += buildings[j].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 JListString().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
}
Part 2: Using Objects Directly with JLists
(As of 4/18/2002, Part 2 is now ready)
Created by: Brandan Jones December 17, 2001