October 18, 2018
Hot Topics:

User Code: IndexedListModel and IndexedTableModel

  • March 26, 2002
  • 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() {

    public void addRow(Vector v, Object userObject) {
        userObjects.put(v, userObject);

    public void addRow(Object[] row, Object userObject) {
        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");
        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();
        btnBrowse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
        btnDelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
        southPanel = new JPanel();
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        getContentPane().add(southPanel, BorderLayout.SOUTH);
        scrollPane.setPreferredSize(new Dimension(250, 200));

    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);
        else {
            if(JOptionPane.showConfirmDialog(this, "Are you sure you want to delete these files?", "", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION)
        while((selIndex = table.getSelectedRow()) != -1) {
            File f = (File) model.getUserObject(selIndex);

    protected void processWindowEvent(WindowEvent e) {
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {

    public static void main(String[] args) {
        IndexedTableExample indexedTableExample1 = new IndexedTableExample();


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

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

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

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


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