Custom table model, File data based Model : Table Model « Swing JFC « Java






Custom table model, File data based Model

Custom table model, File data based Model
     
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// FileTable2.java
//A test frame for the custom table model, FileModel. This version uses a
//custom renderer (BigRenderer.java) to flag large files with an exclamation
//point icon.

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.util.Date;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

public class FileTable2 extends JFrame {

  public FileTable2() {
    super("Custom TableModel Test");
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    FileModel fm = new FileModel();
    JTable jt = new JTable(fm);
    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    jt.setColumnSelectionAllowed(true);
    jt.setDefaultRenderer(Number.class, new BigRenderer(1000));

    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
  }

  public static void main(String args[]) {
    FileTable2 ft = new FileTable2();
    ft.setVisible(true);
  }
}

//FileModel.java
//A custom table model to display information on a directory of files.
//

class FileModel extends AbstractTableModel {

  String titles[] = new String[] { "Directory?", "File Name", "Read?",
      "Write?", "Size", "Last Modified" };

  Class types[] = new Class[] { Boolean.class, String.class, Boolean.class,
      Boolean.class, Number.class, Date.class };

  Object data[][];

  public FileModel() {
    this(".");
  }

  public FileModel(String dir) {
    File pwd = new File(dir);
    setFileStats(pwd);
  }

  // Implement the methods of the TableModel interface we're interested
  // in. Only getRowCount(), getColumnCount() and getValueAt() are
  // required. The other methods tailor the look of the table.
  public int getRowCount() {
    return data.length;
  }

  public int getColumnCount() {
    return titles.length;
  }

  public String getColumnName(int c) {
    return titles[c];
  }

  public Class getColumnClass(int c) {
    return types[c];
  }

  public Object getValueAt(int r, int c) {
    return data[r][c];
  }

  //  Our own method for setting/changing the current directory
  // being displayed. This method fills the data set with file info
  // from the given directory. It also fires an update event so this
  // method could also be called after the table is on display.
  public void setFileStats(File dir) {
    String files[] = dir.list();
    data = new Object[files.length][titles.length];

    for (int i = 0; i < files.length; i++) {
      File tmp = new File(files[i]);
      data[i][0] = new Boolean(tmp.isDirectory());
      data[i][1] = tmp.getName();
      data[i][2] = new Boolean(tmp.canRead());
      data[i][3] = new Boolean(tmp.canWrite());
      data[i][4] = new Long(tmp.length());
      data[i][5] = new Date(tmp.lastModified());
    }

    // Just in case anyone's listening...
    fireTableDataChanged();
  }
}

//BigRenderer.java
//A renderer for numbers that shows an icon in front of big numbers.
//

class BigRenderer extends DefaultTableCellRenderer {
  double threshold;

  Icon bang = new ImageIcon("bang.gif");

  public BigRenderer(double t) {
    threshold = t;
    setHorizontalAlignment(JLabel.RIGHT);
    setHorizontalTextPosition(SwingConstants.RIGHT);
  }

  public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int col) {
    // be a little paranoid about where the user tries to use this renderer
    if (value instanceof Number) {
      if (((Number) value).doubleValue() > threshold) {
        setIcon(bang);
      } else {
        setIcon(null);
      }
    } else {
      setIcon(null);
    }
    return super.getTableCellRendererComponent(table, value, isSelected,
        hasFocus, row, col);
  }
}

           
         
    
    
    
    
  








Related examples in the same category

1.Append a row to a table through DefaultTableModel at specified row
2.Remove the first row from a table with DefaultTableModel
3.Remove the last row from a table with DefaultTableModel
4.Move the first row to the end of the table
5.Move the last row to the beginning of the table
6.Move the first two rows to the end of the table
7.Move the last two rows to the start of the table
8.Get all the table data from DefaultTableModel
9.Copy (clone) the data from the second row
10.Overwrite the date from the first row with DefaultTableModel
11.Copy data from a table to a list
12.Insert a new column to a table
13.JTable class using default table models and a convenienceJTable class using default table models and a convenience
14.extends DefaultTableModel to create your own table model and build table from thatextends DefaultTableModel to create your own table model and build table from that
15.Table with a custom TableModelTable with a custom TableModel
16.extends AbstractTableModel to create custom modelextends AbstractTableModel to create custom model
17.Table model is based on call backTable model is based on call back
18.Hard code data in array for TableModelHard code data in array for TableModel
19.A JTable class using default table models and a convenience constructorA JTable class using default table models and a convenience constructor
20.Custom model, POJO and JTableCustom model, POJO and JTable
21.Paging or pagable JTable(Table) Model for large data setPaging or pagable JTable(Table) Model for large data set
22.Paging JTable(Table) Model with an input field for dynamically altering the size of a page.Paging JTable(Table) Model with an input field for dynamically altering the size of a page.
23.File data Table: file name, size, type File data Table: file name, size, type
24.Stocks data Table: illustrate the TableModel Stocks data Table: illustrate the TableModel
25.AbstractTableModel backed by HashtableAbstractTableModel backed by Hashtable
26.Fixed data vs dynamic data TableFixed data vs dynamic data Table
27.Use model to control the Editable ColumnsUse model to control the Editable Columns
28.Converts a visible column index to a column index in the model.
29.Converts a column index in the model to a visible column index
30.Returns the visible columns in the order that they appear in the model
31.Appending a Column to a JTable Component using DefaultTableModel
32.Add a column with values.
33.Disable autoCreateColumnsFromModel
34.Add a column without affecting existing columns
35.Remove the first visible column without removing the underlying data
36.Sharing a Table Model Between JTable Components
37.Creating simple JTable using AbstractTableModel
38.Map TableModel
39.TableSorter extends AbstractTableModel
40.TableSorter is a decorator for TableModels
41.Bean Property Table Model
42.A simple extension of JTable that supports the use of a SortableTableModel.
43.This program shows how to build a table from a table modelThis program shows how to build a table from a table model