import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /* ObjectListDemo was extensively modified from Walrath and Campione's ListDemo.java in The JFC Swing Tutorial: Java 2 Platform, Addison Wesley 1999. Note: 1. Use of DefaultListModel. 2. List contains objects, not strings. 3. ListModel displays its item's toString() value in the list view. 4. Starts with an empty but nicely sized list display. 5. Uses a JToolbar with icon labels. 6. Has trace statements to show event sequences. Things for you to experiment with: 0. Make toolbar non floatable. 1. Let the user interactively enter a rank for each item 2. When the items are moved up and down also swap the ranks. 3. Add another "notes" field to the items for longer descriptions. This field should be edited in a custom dialog. 4. Set the applications icon to one of your drawing. 5. Consider using a JTable instead of JList to solve some of these problems. Mike Barnes */ public class ObjectListDemo extends JFrame implements ListSelectionListener { private ObjectListDemo toDo; private JList list; private DefaultListModel listModel; private JPanel southPanel; private JToolBar toolBar; private JButton plusButton, minusButton, upButton, downButton, editButton, sortButton; private JTextField textValue; private ActionListener moveIt; public ObjectListDemo() { super("To Do Demo"); listModel = new DefaultListModel(); //Create the list and put it in a scroll pane list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.addListSelectionListener(this); JScrollPane listScrollPane = new JScrollPane(list); plusButton = new JButton(new ImageIcon("plus.gif")); plusButton.addActionListener(new ToDoListener()); plusButton.setToolTipText("Add item to list"); minusButton = new JButton(new ImageIcon("minus.gif")); minusButton.setEnabled(false); minusButton.addActionListener(new DoneListener()); minusButton.setToolTipText("Remove item from list"); editButton = new JButton(new ImageIcon("edit.gif")); editButton.setEnabled(false); editButton.addActionListener(new EditListener()); editButton.setToolTipText("Update, edit, item in list"); sortButton = new JButton(new ImageIcon("sort.gif")); sortButton.setEnabled(false); sortButton.addActionListener(new SortListener()); sortButton.setToolTipText("Sort items in list by their ranks and re-rank"); moveIt = new MoveListener(); upButton = new JButton(new ImageIcon("up.gif")); upButton.setEnabled(false); upButton.addActionListener(moveIt); upButton.setToolTipText("Move item up in list"); downButton = new JButton(new ImageIcon("down.gif")); downButton.setEnabled(false); downButton.addActionListener(moveIt); downButton.setToolTipText("Move item down in list"); textValue = new JTextField(); textValue.addActionListener(new ToDoListener()); textValue.setText(""); toolBar = new JToolBar(); toolBar.add(upButton); toolBar.add(downButton); toolBar.addSeparator(); toolBar.addSeparator(); toolBar.add(plusButton); toolBar.add(minusButton); toolBar.addSeparator(); toolBar.addSeparator(); toolBar.add(editButton); toolBar.add(sortButton); southPanel = new JPanel(); southPanel.setLayout(new BorderLayout()); southPanel.add(toolBar, BorderLayout.NORTH); southPanel.add(textValue, BorderLayout.SOUTH); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); listScrollPane.setPreferredSize(new Dimension(300, 200)); contentPane.add(listScrollPane, BorderLayout.CENTER); contentPane.add(southPanel, BorderLayout.SOUTH); toDo = this; // set non static reference to this application } class MoveListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("MoveListener"); toDo.showList(); int index = list.getSelectedIndex(); ListItem item = (ListItem) listModel.remove(index); toDo.showList(); System.out.println("remove item at index = " + index + " " + item.show()); if (e.getSource() == upButton) { System.out.println("move up"); listModel.add(index -1, item);} else { System.out.println("move down"); listModel.add(index + 1, item); } list.setSelectedIndex(-1); toDo.setState(); toDo.showList(); } } class EditListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("edit listener"); int index = list.getSelectedIndex(); listModel.remove(index); System.out.println("index = " + index + " " + textValue.getText()); listModel.add(index, new ListItem(textValue.getText())); textValue.setText(""); toDo.setState(); } } class SortListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("sort listener"); toDo.showList(); ListItem item; Object tObject; int i, j, min, minRank, tRank = 0; Object[] items = listModel.toArray(); for(i = 0; i < items.length -1; i++) { minRank = ((ListItem)items[i]).getRank(); min = i; for (j = i + 1; j < items.length; j++) { tRank = ((ListItem)items[j]).getRank(); if ( tRank < minRank) { min = j; minRank = tRank; } } tObject = items[i]; items[i] = items[min]; items[min] = tObject; } // empty list and rebuid it listModel.removeAllElements(); for(i = 0; i < items.length; i++) { ((ListItem)items[i]).setRank(i); listModel.addElement(items[i]); } toDo.showList(); toDo.setState(); } } class DoneListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("DoneListener"); // This method can be called only if // there's a valid selection // so go ahead and remove whatever's selected. ListItem item; int index = list.getSelectedIndex(); item = (ListItem) listModel.remove(index); // System.out.print(item.show()); // System.out.println("remove position is " + index); textValue.setText(""); toDo.setState(); } } //This listener is shared by the text field and plus button class ToDoListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("ToDoListener"); //User didn't type in a name... if (textValue.getText().equals("")) { Toolkit.getDefaultToolkit().beep(); return; } int index = list.getSelectedIndex(); int size = listModel.getSize(); //If no selection or if item in last position is selected, //add the new hire to end of list, and select new hire. if (index == -1 || (index + 1 == size)) listModel.addElement(new ListItem(textValue.getText())); //Otherwise insert the new hire after the current selection, else { index++; listModel.add(index, new ListItem(textValue.getText())); } System.out.println("inserted element at " + index); textValue.setText(""); list.setSelectedIndex(-1); toDo.setState(); } } public void valueChanged(ListSelectionEvent e) { System.out.println("valueChanged"); if (e.getValueIsAdjusting() == false) { int index = list.getSelectedIndex(); if ( index > -1) { //Selection, update text field. System.out.println("item selected = " + index + " model size = " + listModel.getSize()); String name = list.getSelectedValue().toString(); textValue.setText(name); } toDo.setState(); } } public void showList() { ListItem item; Object[] items = listModel.toArray(); for(int i = 0; i < items.length; i++) System.out.println(((ListItem) items[i]).show()); } public void setState() { boolean enable; int size = listModel.getSize(); if (list.isSelectionEmpty() || size == 0) enable = false; else enable = true; minusButton.setEnabled(enable); editButton.setEnabled(enable); upButton.setEnabled(enable); downButton.setEnabled(enable); int index = list.getSelectedIndex(); if (size == 0) sortButton.setEnabled(false); else sortButton.setEnabled(true); if (index == size -1) downButton.setEnabled(false); if (index == 0) upButton.setEnabled(false); } public static void main(String s[]) { JFrame app = new ObjectListDemo(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.pack(); app.setVisible(true); } } class ListItem { private String description; private int rank; public ListItem (String d, int r) { description = d; rank = r; System.out.println("create ListItem " + show()); } public ListItem (String d) { description = d; rank = (int) Math.round(Math.random()*100); System.out.println("create ListItem " + show()); } public int getRank() { return rank; } public void setRank(int value) { rank = value; } public String toString() { return description; } public String show() { return ("\"" + description + "\"" + " has rank of " + rank + " "); } }