Use model to control the Editable Columns : Table Model « Swing JFC « Java






Use model to control the Editable Columns

Use model to control the Editable Columns
     
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
*/

import java.awt.BorderLayout;
import java.util.Date;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class EditableColumn {
  public static void main(String args[]) {
    TableModel model = new AbstractTableModel() {
      Icon icon1 = new ImageIcon("TreeCollapsed.gif");

      Icon icon2 = new ImageIcon("TreeExpanded.gif");

      Object rowData[][] = {
          { new Integer(1), "ichi", Boolean.TRUE,
              new Date("01/01/2000"), icon1 },
          { new Integer(2), "ni", Boolean.TRUE,
              new Date("04/15/1999"), icon2 },
          { new Integer(3), "san", Boolean.FALSE,
              new Date("12/07/1941"), icon2 },
          { new Integer(4), "shi", Boolean.TRUE,
              new Date("02/29/2000"), icon1 },
          { new Integer(5), "go", Boolean.FALSE,
              new Date("05/23/1995"), icon1 }, };

      String columnNames[] = { "English", "Japanese", "Boolean", "Date",
          "ImageIcon" };

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

      public String getColumnName(int column) {
        return columnNames[column];
      }

      public int getRowCount() {
        return rowData.length;
      }

      public Object getValueAt(int row, int column) {
        return rowData[row][column];
      }

      public Class getColumnClass(int column) {
        return (getValueAt(0, column).getClass());
      }

      public void setValueAt(Object value, int row, int column) {
        rowData[row][column] = value;
      }

      public boolean isCellEditable(int row, int column) {
        return (column != 4);
      }
    };

    JFrame frame = new JFrame("Column Renderer Table");
    JTable table = new JTable(model);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}

           
         
    
    
    
    
  








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.Custom table model, File data based ModelCustom table model, File data based Model
22.Paging or pagable JTable(Table) Model for large data setPaging or pagable JTable(Table) Model for large data set
23.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.
24.File data Table: file name, size, type File data Table: file name, size, type
25.Stocks data Table: illustrate the TableModel Stocks data Table: illustrate the TableModel
26.AbstractTableModel backed by HashtableAbstractTableModel backed by Hashtable
27.Fixed data vs dynamic data TableFixed data vs dynamic data Table
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