import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /* Demo modified from Walrath and Campione's ListDemo.java in The JFC Swing Tutorial: Java 2 Platform, Addison Wesley 1999 that is initially empty and can contain JLabels as items. Note the use of of labels that contain icons and a ListCellRender for displaying the labels. Select an item, its text value is in the bottom textfield. Change the textfield and press "update". The updated item will have a new icon as well as a change in the text. mike barnes 9/12/02 */ public class LabelList extends JFrame implements ListSelectionListener { private JList list; private DefaultListModel listModel; private JPanel panel; private static final String addString = "Add "; private static final String doneString = "Done"; private static final String updateString = "Update"; private JButton addButton, doneButton, updateButton; private JTextField textValue; private static ImageIcon left = new ImageIcon("left.gif"); private static ImageIcon middle = new ImageIcon("middle.gif"); public LabelList() { super("To Do Demo"); listModel = new DefaultListModel(); MyCellRenderer render = new MyCellRenderer(); //Create the list and put it in a scroll pane list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.addListSelectionListener(this); list.setCellRenderer(render); JScrollPane listScrollPane = new JScrollPane(list); addButton = new JButton(addString); addButton.setActionCommand(addString); addButton.addActionListener(new AddListener()); doneButton = new JButton("Done"); doneButton.setActionCommand(doneString); doneButton.setEnabled(false); doneButton.addActionListener(new DoneListener()); updateButton = new JButton("Update"); updateButton.setActionCommand(updateString); updateButton.setEnabled(false); updateButton.addActionListener(new UpdateListener()); textValue = new JTextField(10); textValue.addActionListener(new AddListener()); textValue.setText(""); //Create a panel that uses FlowLayout (the default). JPanel buttonPane = new JPanel(); buttonPane.add(textValue); buttonPane.add(addButton); buttonPane.add(doneButton); buttonPane.add(updateButton); 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 " + list.getSelectedIndex()); } class UpdateListener implements ActionListener { public void actionPerformed(ActionEvent e) { JLabel label = (JLabel) list.getSelectedValue(); label.setIcon(middle); label.setText(textValue.getText()); int index = list.getSelectedIndex(); listModel.remove(index); listModel.insertElementAt(label, index); 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. JLabel label; int index = list.getSelectedIndex(); label = (JLabel) listModel.remove(index); int size = listModel.getSize(); if (size == 0) { // Nobody's left, disable firing. textValue.setText(""); doneButton.setEnabled(false); updateButton.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 Add button class AddListener 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(); JLabel label = new JLabel(textValue.getText(), left, JLabel.LEFT ); //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(label); list.setSelectedIndex(size); //Otherwise insert the new hire after the current selection, //and select new hire. } else { listModel.insertElementAt(label, index+1); list.setSelectedIndex(index+1); } System.out.println("added position is " + list.getSelectedIndex()); } } public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting() == false) { if (list.getSelectedIndex() == -1) { //No selection, disable fire button. doneButton.setEnabled(false); updateButton.setEnabled(false); textValue.setText(""); } else { //Selection, update text field. JLabel label = (JLabel) list.getSelectedValue(); updateButton.setEnabled(true); doneButton.setEnabled(true); textValue.setText(label.getText()); } } } public static void main(String s[]) { JFrame frame = new LabelList(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.pack(); frame.setVisible(true); } // Here is the ListCellRender class MyCellRenderer extends JLabel implements ListCellRenderer { public MyCellRenderer() { setOpaque(true); } public Component getListCellRendererComponent ( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground());} JLabel l = (JLabel)value; ImageIcon icon = (ImageIcon) l.getIcon(); setText(l.getText()); setIcon(icon); return this; } } }