001: //WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package examples;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022:
023: import javax.swing.*;
024: import javax.swing.event.*;
025:
026: public class ListDemo extends JFrame {
027: public static void main(String s[]) {
028: JFrame frame = new ListDemo();
029:
030: frame.addWindowListener(new WindowAdapter() {
031: public void windowClosing(WindowEvent e) {
032: System.exit(0);
033: }
034: });
035:
036: frame.pack();
037: frame.setVisible(true);
038: }
039:
040: public ListDemo() {
041: getContentPane().add(new ListDemoPanel());
042: }
043:
044: public static class ListDemoPanel extends JPanel implements
045: ListSelectionListener {
046: JList list;
047: DefaultListModel listModel;
048:
049: private static final String hireString = "Hire";
050: private static final String fireString = "Fire";
051: JButton fireButton;
052: JTextField employeeName;
053:
054: public ListDemoPanel() {
055: listModel = new DefaultListModel();
056: listModel.addElement("Alison Huml");
057: listModel.addElement("Kathy Walrath");
058: listModel.addElement("Lisa Friendly");
059: listModel.addElement("Mary Campione");
060:
061: //Create the list and put it in a scroll pane
062: list = new JList(listModel);
063: list
064: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
065: list.setSelectedIndex(0);
066: list.addListSelectionListener(this );
067: JScrollPane listScrollPane = new JScrollPane(list);
068:
069: JButton hireButton = new JButton(hireString);
070: hireButton.setActionCommand(hireString);
071: hireButton.addActionListener(new HireListener());
072:
073: fireButton = new JButton(fireString);
074: fireButton.setActionCommand(fireString);
075: fireButton.addActionListener(new FireListener());
076:
077: employeeName = new JTextField(10);
078: employeeName.addActionListener(new HireListener());
079: String name = listModel.getElementAt(
080: list.getSelectedIndex()).toString();
081: employeeName.setText(name);
082:
083: //Create a panel that uses FlowLayout (the default).
084: JPanel buttonPane = new JPanel();
085: buttonPane.add(employeeName);
086: buttonPane.add(hireButton);
087: buttonPane.add(fireButton);
088:
089: Container contentPane = this ;
090: setLayout(new BorderLayout());
091: contentPane.add(listScrollPane, BorderLayout.CENTER);
092: contentPane.add(buttonPane, BorderLayout.SOUTH);
093: }
094:
095: class FireListener implements ActionListener {
096: public void actionPerformed(ActionEvent e) {
097: //This method can be called only if
098: //there's a valid selection
099: //so go ahead and remove whatever's selected.
100: int index = list.getSelectedIndex();
101: listModel.remove(index);
102:
103: int size = listModel.getSize();
104:
105: if (size == 0) {
106: //Nobody's left, disable firing.
107: fireButton.setEnabled(false);
108:
109: } else {
110: //Adjust the selection.
111: if (index == listModel.getSize()) //removed item in last position
112: index--;
113: list.setSelectedIndex(index); //otherwise select same index
114: }
115: }
116: }
117:
118: //This listener is shared by the text field and the hire button
119: class HireListener implements ActionListener {
120: public void actionPerformed(ActionEvent e) {
121:
122: //User didn't type in a name...
123: if (employeeName.getText().equals("")) {
124: Toolkit.getDefaultToolkit().beep();
125: return;
126: }
127:
128: int index = list.getSelectedIndex();
129: int size = listModel.getSize();
130:
131: //If no selection or if item in last position is selected,
132: //add the new hire to end of list, and select new hire.
133: if (index == -1 || (index + 1 == size)) {
134: listModel.addElement(employeeName.getText());
135: list.setSelectedIndex(size);
136:
137: //Otherwise insert the new hire after the current selection,
138: //and select new hire.
139: } else {
140: listModel.insertElementAt(employeeName.getText(),
141: index + 1);
142: list.setSelectedIndex(index + 1);
143: }
144: }
145: }
146:
147: public void valueChanged(ListSelectionEvent e) {
148: if (e.getValueIsAdjusting() == false) {
149:
150: if (list.getSelectedIndex() == -1) {
151: //No selection, disable fire button.
152: fireButton.setEnabled(false);
153: employeeName.setText("");
154:
155: } else {
156: //Selection, update text field.
157: fireButton.setEnabled(true);
158: String name = list.getSelectedValue().toString();
159: employeeName.setText(name);
160: }
161: }
162: }
163:
164: }
165: }
|