import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.io.*; import java.beans.*; /* Demo modified from Walrath and Campione's ListDemo.java in The JFC Swing Tutorial: Java 2 Platform, Addison Wesley 1999 that has an initially empty list. This example also demonstrates persistence with Serialization using file "list.data" You must first serialize "save" a list to list.data before you can load serialized data from list.data into the list. Mike Barnes 9/16/05 */ public class ListSerializableDemo extends JFrame implements ListSelectionListener, Serializable { public JList list; public DefaultListModel listModel; private JPanel panel; private JButton todoButton, doneButton, editButton; private JMenu menuSerializable; private JMenuItem load, save; private JMenuBar menuBar; private JTextField textValue; public ListSerializableDemo() { super("List Serializable 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); todoButton = new JButton("To do"); todoButton.addActionListener(new ToDoListener()); doneButton = new JButton("Done"); doneButton.setEnabled(false); doneButton.addActionListener(new DoneListener()); editButton = new JButton("Edit"); editButton.setEnabled(false); editButton.addActionListener(new EditListener()); // menu menuSerializable = new JMenu("Serialize"); save = new JMenuItem("Save"); save.addActionListener(new SaveListener()); load = new JMenuItem("Load"); load.addActionListener(new LoadListener()); menuBar = new JMenuBar(); menuSerializable.add(load); menuSerializable.add(save); menuBar.add(menuSerializable); setJMenuBar(menuBar); textValue = new JTextField(10); textValue.addActionListener(new ToDoListener()); textValue.setText(""); //Create a panel that uses FlowLayout (the default). JPanel buttonPane = new JPanel(); buttonPane.add(textValue); buttonPane.add(todoButton); buttonPane.add(doneButton); buttonPane.add(editButton); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); listScrollPane.setPreferredSize(new Dimension(300, 200)); contentPane.add(listScrollPane, BorderLayout.CENTER); contentPane.add(buttonPane, BorderLayout.SOUTH); System.out.println("selected position is" + Integer.toString(list.getSelectedIndex())); } class LoadListener implements ActionListener { public void actionPerformed (ActionEvent e) { ObjectInputStream fin = null; // file to read // open file to read try { fin = new ObjectInputStream( new FileInputStream("list.data")); } catch(IOException ioe) { System.out.println("load list.data not found."); System.exit(0); } // read the listModel, or, the entire list try { listModel = (DefaultListModel) fin.readObject(); list.setModel(listModel); } catch (ClassNotFoundException cnf) { System.out.println("Item class not found, exit"); System.exit(0); } catch (IOException ioe) { System.out.println("Error reading items"); System.exit(0); } try { fin.close(); } catch(IOException ioe) { System.out.println("IOexception fout.close"); System.exit(0); } System.out.println("list data read"); } } class SaveListener implements ActionListener { public void actionPerformed (ActionEvent e) { ObjectOutputStream fout = null; // file to write String item; try { fout = new ObjectOutputStream( new FileOutputStream("list.data")); } catch(IOException ioe) { System.out.println("save list.data not found"); System.exit(0); } // write out the listModel, or, write the list.... try { fout.writeObject(list.getModel()); } catch(IOException ioe) { System.out.println("save IOExeception " + ioe); System.exit(0); } try { fout.close(); } catch(IOException ioe) { System.out.println("IOexception fout.close"); System.exit(0); } System.out.println("list.data wrote"); } } class EditListener implements ActionListener { public void actionPerformed(ActionEvent e) { int index = list.getSelectedIndex(); listModel.insertElementAt(textValue.getText(), index); listModel.remove(index +1); list.setSelectedIndex(index); } } class DoneListener implements ActionListener { public void actionPerformed(ActionEvent e) { //This method can be called only if //there's a valid selection //so go ahead and remove whatever's selected. int index = list.getSelectedIndex(); System.out.println("remove position is" + Integer.toString(list.getSelectedIndex())); listModel.remove(index); int size = listModel.getSize(); if (size == 0) { //Nobody's left, disable firing. doneButton.setEnabled(false); editButton.setEnabled(false); } else { //Adjust the selection. if (index == listModel.getSize())//removed item in last position index--; list.setSelectedIndex(index); } //otherwise select same index } } //This listener is shared by the text field and the ToDo button class ToDoListener implements ActionListener { public void actionPerformed(ActionEvent e) { //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(textValue.getText()); list.setSelectedIndex(size); } //Otherwise insert the new hire after the current selection, //and select new hire. else { listModel.insertElementAt(textValue.getText(), index+1); list.setSelectedIndex(index+1); } System.out.println("added position is" + Integer.toString(list.getSelectedIndex())); } } public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting() == false) { if (list.getSelectedIndex() == -1) { //No selection, disable fire button. doneButton.setEnabled(false); editButton.setEnabled(false); textValue.setText(""); } else { //Selection, update text field. editButton.setEnabled(true); doneButton.setEnabled(true); String name = list.getSelectedValue().toString(); textValue.setText(name); } } } public static void main(String s[]) { JFrame frame = new ListSerializableDemo(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }