Get Default Cell Editor : Table Renderer Editor « Swing JFC « Java






Get Default Cell Editor

 
import java.util.Date;
import java.util.Enumeration;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class Main {
  public static void main(String[] argv) {
    DefaultTableModel model = new DefaultTableModel() {
      public Class getColumnClass(int columnIndex) {
        Object o = getValueAt(0, columnIndex);
        if (o == null) {
          return Object.class;
        } else {
          return o.getClass();
        }
      }
    };
    JTable table = new JTable(model);

    model.addColumn("Boolean", new Object[] { Boolean.TRUE });
    model.addColumn("Date", new Object[] { new Date() });
    model.addColumn("Double", new Object[] { new Double(Math.PI) });
    model.addColumn("Float", new Object[] { new Float(1.2) });
    model.addColumn("Icon", new Object[] { new ImageIcon("icon.gif") });
    model.addColumn("Number", new Object[] { new Integer(1) });
    model.addColumn("Object", new Object[] { "object" });
    
    Enumeration e = table.getColumnModel().getColumns();
    TableColumn col = (TableColumn) e.nextElement();

    col.setCellRenderer(table.getDefaultRenderer(Boolean.class));
    col.setCellEditor(table.getDefaultEditor(Boolean.class));
    
    JFrame f = new JFrame();
    f.setSize(300,300);
    f.add(new JScrollPane(table));
    f.setVisible(true);
  }
}

   
  








Related examples in the same category

1.Shading Rows and Columns in a JTable Component
2.Shades every other column yellow
3.Using Built-In Cell Renderers and Editors in a JTable Component
4.Get Default cell Renderer
5.Creating a Custom Cell Renderer in a JTable Component
6.Creating a Class-Based Custom Cell Renderer in a JTable Component
7.A slider renderer for volume values in a tableA slider renderer for volume values in a table
8.Table Cell Editor: ComboBoxCellEditor
9.Table with a custom cell renderer and editor for the color dataTable with a custom cell renderer and editor for the color data
10.Table with initialized column sizes and a combo box editorTable with initialized column sizes and a combo box editor
11.StockTable 3: CellRendererStockTable 3: CellRenderer
12.Colored Table CellRendererColored Table CellRenderer
13.Change Table cell background with column renderer Change Table cell background with column renderer
14.Install different Table Renderer for even and odd rowsInstall different Table Renderer for even and odd rows
15.A table that allows the user to pick a color from a pulldown listA table that allows the user to pick a color from a pulldown list
16.MixerModel and sliders for rendering volume valuesMixerModel and sliders for rendering volume values
17.Table Editor: Editable Color ColumnTable Editor: Editable Color Column
18.Tab in and out JComboBox in JTable
19.Creating a Custom Table Cell Editor in a JTable Component
20.Preventing Invalid Values in a Cell in a JTable Component
21.Setting the Activation Click Count for a Table Cell Editor in a JTable Component
22.Using a JComboBox in a Cell in a JTable Component
23.Using a List JSpinner as a Cell Editor in a JTable Component
24.Rendering an image in a JTable column
25.This program demonstrates cell rendering and editing in a table.This program demonstrates cell rendering and editing in a table.