extends DefaultCellEditor : JTable Renderer Editor « Swing « Java Tutorial






extends DefaultCellEditor
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;

import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

public class DefaultCellEditorJTextFieldAlignment extends JFrame {
  DefaultTableModel model = new DefaultTableModel(new Object[][] { { "some", "text" },
      { "any", "text" }, { "even", "more" }, { "text", "strings" }, { "and", "other" },
      { "text", "values" } }, new Object[] { "Column 1", "Column 2" });

  public DefaultCellEditorJTextFieldAlignment() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(model);
    table.setDefaultEditor(Object.class, new MyEditor());
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    pack();
  }

  public static void main(String arg[]) {
    new DefaultCellEditorJTextFieldAlignment().setVisible(true);
  }
}

class MyEditor extends DefaultCellEditor {
  public MyEditor() {
    super(new JTextField());
  }

  public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
      int row, int column) {
    JTextField editor = (JTextField) super.getTableCellEditorComponent(table, value, isSelected,
        row, column);

    if (value != null)
      editor.setText(value.toString());
    if (column == 0) {
      editor.setHorizontalAlignment(SwingConstants.CENTER);
      editor.setFont(new Font("Serif", Font.BOLD, 14));
    } else {
      editor.setHorizontalAlignment(SwingConstants.RIGHT);
      editor.setFont(new Font("Serif", Font.ITALIC, 12));
    }
    return editor;
  }
}








14.60.JTable Renderer Editor
14.60.1.Get Default cell Renderer
14.60.2.Get Default Cell Editor
14.60.3.Default Editors and Renderers
14.60.4.Rendering Table Cells: To install the renderer as the default renderer for the JTable for Object.classRendering Table Cells: To install the renderer as the default renderer for the JTable for Object.class
14.60.5.implements TableCellRendererimplements TableCellRenderer
14.60.6.extends DefaultCellEditorextends DefaultCellEditor
14.60.7.Cell Renderer for the Gender ColumnCell Renderer for the Gender Column
14.60.8.Demonstrates the editable property of Swing tables.Demonstrates the editable property of Swing tables.
14.60.9.An Editor for the Gender ColumnAn Editor for the Gender Column
14.60.10.ColorChooserEditor
14.60.11.Using a JComboBox in a Cell in a JTable Component
14.60.12.Editing Table Cells: JComboBox As Table Cell RendererEditing Table Cells: JComboBox As Table Cell Renderer
14.60.13.Using a List JSpinner as a Cell Editor in a JTable Component
14.60.14.Using default Boolean value cell editor and rendererUsing default Boolean value cell editor and renderer
14.60.15.Creating a Class-Based Custom Cell Renderer in a JTable Component
14.60.16.Creating a Custom Cell Renderer in a JTable Component
14.60.17.Preventing Invalid Values in a Cell in a JTable Component
14.60.18.Using Built-In Cell Renderers and Editors in a JTable Component
14.60.19.Save the current value in the cell being edited and stops the editing process
14.60.20.Discard any changes made by the user and stops the editing process