dcsimg
November 24, 2020
Hot Topics:

User Code: IndexedListModel and IndexedTableModel

  • By Amir Kost
  • Send Email »
  • More Articles »

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

indexedlistmodel.java = 3 Kb

indexedlistexample.java = 3 Kb

indexedtablemodel.java =1 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.





Page 2 of 2



This article was originally published on March 26, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.


Thanks for your registration, follow us on our social networks to keep up-to-date