An Editor for the Gender Column : JTable Renderer Editor « Swing « Java Tutorial






An Editor for the Gender Column
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.EventObject;
import java.util.GregorianCalendar;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.EventListenerList;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

class GenderEditor extends JComboBox implements TableCellEditor {

  protected EventListenerList listenerList = new EventListenerList();

  protected ChangeEvent changeEvent = new ChangeEvent(this);

  public GenderEditor() {
    super();
    addItem("Male");
    addItem("Female");
    addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        fireEditingStopped();
      }
    });
  }

  public void addCellEditorListener(CellEditorListener listener) {
    listenerList.add(CellEditorListener.class, listener);
  }

  public void removeCellEditorListener(CellEditorListener listener) {
    listenerList.remove(CellEditorListener.class, listener);
  }

  protected void fireEditingStopped() {
    CellEditorListener listener;
    Object[] listeners = listenerList.getListenerList();
    for (int i = 0; i < listeners.length; i++) {
      if (listeners[i] == CellEditorListener.class) {
        listener = (CellEditorListener) listeners[i + 1];
        listener.editingStopped(changeEvent);
      }
    }
  }

  protected void fireEditingCanceled() {
    CellEditorListener listener;
    Object[] listeners = listenerList.getListenerList();
    for (int i = 0; i < listeners.length; i++) {
      if (listeners[i] == CellEditorListener.class) {
        listener = (CellEditorListener) listeners[i + 1];
        listener.editingCanceled(changeEvent);
      }
    }
  }

  public void cancelCellEditing() {
    fireEditingCanceled();
  }

  public boolean stopCellEditing() {
    fireEditingStopped();
    return true;
  }

  public boolean isCellEditable(EventObject event) {
    return true;
  }

  public boolean shouldSelectCell(EventObject event) {
    return true;
  }

  public Object getCellEditorValue() {
    return new Boolean(getSelectedIndex() == 0 ? true : false);
  }

  public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
      int row, int column) {
    boolean isMale = ((Boolean) value).booleanValue();
    setSelectedIndex(isMale ? 0 : 1);
    return this;
  }
}

public class TableCellEditorJComboBox extends JFrame {
  protected JTable table;

  public static void main(String[] args) {
    TableCellEditorJComboBox stt = new TableCellEditorJComboBox();
    stt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    stt.setSize(400, 200);
    stt.setVisible(true);
  }

  public TableCellEditorJComboBox() {
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    TableValues tv = new TableValues();
    table = new JTable(tv);
    TableColumnModel tcm = table.getColumnModel();
    TableColumn tc = tcm.getColumn(TableValues.GENDER);
    tc.setCellEditor(new GenderEditor());
    JScrollPane jsp = new JScrollPane(table);
    pane.add(jsp, BorderLayout.CENTER);
  }

}

class TableValues extends AbstractTableModel {

  public final static int FIRST_NAME = 0;

  public final static int LAST_NAME = 1;

  public final static int DATE_OF_BIRTH = 2;

  public final static int ACCOUNT_BALANCE = 3;

  public final static int GENDER = 4;

  public final static boolean GENDER_MALE = true;

  public final static boolean GENDER_FEMALE = false;

  public Object[][] values = {
      { "C", "A", new GregorianCalendar(1962, Calendar.FEBRUARY, 20).getTime(), new Float(1.67),
          new Boolean(GENDER_MALE) },
      { "J", "A", new GregorianCalendar(1987, Calendar.JANUARY, 6).getTime(), new Float(2.78),
          new Boolean(GENDER_MALE) },
      { "J", "A", new GregorianCalendar(1989, Calendar.AUGUST, 31).getTime(), new Float(3.89),
          new Boolean(GENDER_FEMALE) },
      { "E", "K", new GregorianCalendar(1945, Calendar.JANUARY, 16).getTime(), new Float(-4.70),
          new Boolean(GENDER_FEMALE) },
      { "B", "S", new GregorianCalendar(1907, Calendar.AUGUST, 2).getTime(), new Float(5.00),
          new Boolean(GENDER_FEMALE) } };

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

  public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
  }

  public int getColumnCount() {
    return values[0].length;
  }

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

}








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