/* CMPUT114(2003-2004, Fall) http://www-csfy.cs.ualberta.ca/~c114/F03 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; public class JListDo extends JApplet implements ActionListener { private JLabel item; private JList itemList; public void init() { this.item = new JLabel(); this.addButton(); Container container = this.getContentPane(); this.itemList = this.getList(); container.add(this.getPanel()); } private JList getList() { // Create a List with the 5 course items String[] data = {"Math 113", "Cmput 114", "Phil 120", "Cmput 272", "Chem 105"}; JList tempList = new JList(data); tempList.setVisibleRowCount(3); // Enable single selection tempList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); return tempList; } private JButton addButton() { JButton button = new JButton("Get Item"); button.addActionListener(this); return button; } private JPanel getPanel() { // set layout to GridLayout 4 rows and 1 columns JPanel panel = new JPanel(new GridLayout(4,1,0,0)); JScrollPane spane1 = new JScrollPane(this.itemList); panel.add(new JLabel("Example List")); panel.add(spane1); panel.add(this.item); panel.add(this.addButton()); return panel; } public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); // Get the selected value from the list and update the JLabel item.setText((String)itemList.getSelectedValue()); } }