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 XML encoder / decoder using file "list.xml" There is probably a simpler way, but I haven't found it. This version takes the DefaultListModel and converts to an Object[] and XMLencodes it. The complement is to XMLdecode the Object[], add the Objects to a new DefaultListModel, and set the list's model. You must first populate the list and then use the XML | Save menu option to create a file "list.xml" before you can use the menu XML | Load option to populate an empty list from the persistent store list.xml. Mike Barnes 9/16/05 */ public class XMLListDemo extends JFrame implements ListSelectionListener { public JList list; public DefaultListModel listModel; private JPanel panel; private JButton todoButton, doneButton, editButton; private JMenu xmlMenu; private JMenuItem load, save; private JMenuBar menuBar; private JTextField textValue; public XMLListDemo() { super("XML List 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 xmlMenu = new JMenu("XML"); save = new JMenuItem("Save"); save.addActionListener(new XMLSaveListener()); load = new JMenuItem("Load"); load.addActionListener(new XMLLoadListener()); menuBar = new JMenuBar(); xmlMenu.add(load); xmlMenu.add(save); menuBar.add(xmlMenu); 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 XMLLoadListener implements ActionListener { public void actionPerformed (ActionEvent e) { XMLDecoder fin = null; Object[] elements; try { fin = new XMLDecoder ( new BufferedInputStream (new FileInputStream("list.xml"))); elements = (Object[]) fin.readObject(); listModel = new DefaultListModel(); for(int i = 0; i < elements.length; i++) listModel.addElement(elements[i]); list.setModel(listModel); } catch (FileNotFoundException fnfe) {} fin.close(); System.out.println("XMLDecoder"); } } class XMLSaveListener implements ActionListener { public void actionPerformed (ActionEvent e) { XMLEncoder fout = null; Object[] elements; try { fout = new XMLEncoder ( new BufferedOutputStream (new FileOutputStream("list.xml"))); elements = new Object[list.getModel().getSize()]; elements = ((DefaultListModel) list.getModel()).toArray(); fout.writeObject(elements); } catch (FileNotFoundException fnfe) {} fout.close(); System.out.println("XMLEncoder"); } } 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 XMLListDemo(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }