User Code: IndexedListModel and IndexedTableModel
This user has graciously shared a little bag of tricks with us. The first class, IndexedListModel, allows you to have a user Object for each element in the List. All the add/addElement/insertElementAt/set/setElementAt have a user object as a parameter. You can also call the setUserData(int index, Object o)
to set the user's object, as well as retrieve the user object by calling the method getUserObject(int row)
or getUserObject(Object o)
.
The second class, SortableIndexedTableModel, lets you have a user object for each row. When adding a row, you can use the methods addRow(Vector v, Object userObject)
or a addRow(Object[] row, Object userObject)
. You can then use the method getUserObject(int row)
to retrieve the object for the specific row.
Both are followed with examples, along with downloadable files.
import javax.swing.DefaultListModel; import java.util.Hashtable; /*********************************************************************** * Class: IndexedListModel * Author: Amir Kost * Purpose: This class enables to have a user Object for each element in the List. * Usage: All the add/addElement/insertElementAt/set/setElementAt have a user object as a parameter. * You can also call the setUserData(int index, Object o) to set the user's object. * You can retrieve the user object by calling the method getUserObject(int row) or getUserObject(Object o). ***********************************************************************/ public class IndexedListModel extends DefaultListModel{ Hashtable userObjects = new Hashtable(); public IndexedListModel() { } public void addElement(Object o, Object userData) { super.addElement(o); userObjects.put(o, userData); } public void add(int index, Object o, Object userData) { super.add(index, o); userObjects.put(o, userData); } public void insertElementAt(Object o, int index, Object userData) { super.insertElementAt(o, index); userObjects.put(o, userData); } public Object set(int index, Object o, Object userData) { userObjects.put(o, userData); return super.set(index, o); } public void setElementAt(Object o, int index, Object userData) { super.setElementAt(o, index); userObjects.put(o, userData); } public Object remove(int index) { Object o = get(index); userObjects.remove(o); return super.remove(index); } public boolean removeElement(Object o) { userObjects.remove(o); return super.removeElement(o); } public void removeElementAt(int index) { Object o = get(index); userObjects.remove(o); super.removeElementAt(index); } public void removeAllElements() { userObjects.clear(); super.removeAllElements(); } public void clear() { userObjects.clear(); super.clear(); } public void removeRange(int fromIndex, int toIndex) { if(fromIndex > toIndex) throw new IllegalArgumentException(); for(int i = fromIndex; i < toIndex; i++) { Object o = get(i); userObjects.remove(o); super.remove(i); } } public Object getUserObject(Object o) { return userObjects.get(o); } public Object getUserObject(int index) { Object o = get(index); return userObjects.get(o); } public void setUserData(int index, Object o) { Object key = get(index); userObjects.put(key, o); } // sets the data for the key only if key is an element in the List // if key is not found - it does nothing public void setUserData(Object key, Object val) { if(this.indexOf(key) != -1) userObjects.put(key, val); } } import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.File; /*********************************************************************** * Class: IndexedListExample * Purpose: This class enables selection and deletion of files using the IndexedListModel. * It displays only the name of the file, and allows deletion of selected files. * ***********************************************************************/ public class IndexedListExample extends JFrame{ JList list; IndexedListModel model; JButton btnBrowse; JButton btnDelete; JPanel southPanel; JFileChooser fileChooser; public IndexedListExample() { this.setTitle("IndexedList Example"); model = new IndexedListModel(); list = new JList(model); btnBrowse = new JButton("Browse"); btnDelete = new JButton("Delete"); fileChooser = new JFileChooser(); fileChooser.setMultiSelectionEnabled(true); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); btnBrowse.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { onBrowse(); } }); btnDelete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { onDelete(); } }); southPanel = new JPanel(); southPanel.add(btnBrowse); southPanel.add(btnDelete); getContentPane().setLayout(new BorderLayout()); getContentPane().add(list, BorderLayout.CENTER); getContentPane().add(southPanel, BorderLayout.SOUTH); list.setPreferredSize(new Dimension(250, 200)); pack(); setVisible(true); } private void onBrowse() { fileChooser.showDialog(this, "Select Files"); File[] files = fileChooser.getSelectedFiles(); for(int i = 0; i < files.length; i++) { model.addElement(files[i].getName(), files[i]); } } private void onDelete() { int selIndex = list.getSelectedIndex(); if(selIndex == -1) { JOptionPane.showMessageDialog(this, "No files were selected!", "", JOptionPane.ERROR_MESSAGE); return; } else { if(JOptionPane.showConfirmDialog(this, "Are you sure you want to delete these files?", "", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) return; } while((selIndex = list.getSelectedIndex()) != -1) { File f = (File) model.getUserObject(selIndex); f.delete(); model.remove(selIndex); } } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public static void main(String[] args) { IndexedListExample indexedListExample1 = new IndexedListExample(); } }
Page 1 of 2
This article was originally published on March 26, 2002