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(); } }
Here's IndexedTableModel.
/*********************************************************************** * * Class: IndexedTableModel * By: Amir Kost * Purpose: This class allows having a user object for each row. * Usage: 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. * ***********************************************************************/ import java.util.*; import javax.swing.table.TableModel; import javax.swing.event.TableModelEvent; import javax.swing.JTable; import javax.swing.table.*; public class IndexedTableModel extends DefaultTableModel{ protected Hashtable userObjects = new Hashtable(); public IndexedTableModel() { super(); } public void addRow(Vector v, Object userObject) { super.addRow(v); userObjects.put(v, userObject); } public void addRow(Object[] row, Object userObject) { super.addRow(row); Vector v = (Vector) dataVector.get(dataVector.size() - 1); userObjects.put(v, userObject); } public Object getUserObject(int row) { return userObjects.get(dataVector.get(row)); } } import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.File; import java.util.Date; /*********************************************************************** * Class: IndexedTableExample * Purpose: This class enables selection and deletion of files using the IndexedTableModel. * It displays only the name of the file and its date, and allows deletion of selected files. * ***********************************************************************/ public class IndexedTableExample extends JFrame{ JTable table; IndexedTableModel model; JScrollPane scrollPane; JButton btnBrowse; JButton btnDelete; JPanel southPanel; JFileChooser fileChooser; public IndexedTableExample() { this.setTitle("IndexedTable Example"); model = new IndexedTableModel(); model.addColumn("File Name"); model.addColumn("Date"); table = new JTable(model) { public boolean isCellEditable(int row, int col) { return false; } }; scrollPane = new JScrollPane(table); 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(scrollPane, BorderLayout.CENTER); getContentPane().add(southPanel, BorderLayout.SOUTH); scrollPane.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++) { Object[] row = new Object[2]; row[0] = files[i].getName(); row[1] = new Date(files[i].lastModified()); model.addRow(row, files[i]); } } private void onDelete() { int selIndex = table.getSelectedRow(); 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 = table.getSelectedRow()) != -1) { File f = (File) model.getUserObject(selIndex); f.delete(); model.removeRow(selIndex); } } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public static void main(String[] args) { IndexedTableExample indexedTableExample1 = new IndexedTableExample(); } }
Downloads
indexedlistexample.java = 3 Kb
indexedtableexample.java = 3 Kb
About the Author
Amir Kost has a B.A. degree in computer science and philosophy from
the University of Tel Aviv. He is currently working for Textology Ltd.,
which has a unique technology for classifying text documents for large
organizations, where he creates and maintains Swing applications.