Class ComboBoxCellEditor

  • All Implemented Interfaces:
    java.io.Serializable, javax.swing.CellEditor, javax.swing.table.TableCellEditor

    public class ComboBoxCellEditor
    extends javax.swing.AbstractCellEditor
    implements javax.swing.table.TableCellEditor, java.io.Serializable

    This is a cell editor that can be used when a combo box (that has been set up for automatic completion) is to be used in a JTable. The DefaultCellEditor won't work in this case, because each time an item gets selected it stops cell editing and hides the combo box.

    Usage example:

    
     JTable table = ...;
     JComboBox comboBox = ...;
     ...
     TableColumn column = table.getColumnModel().getColumn(0);
     column.setCellEditor(new ComboBoxCellEditor(comboBox));
     

    See Also:
    Serialized Form
    • Field Summary

      • Fields inherited from class javax.swing.AbstractCellEditor

        changeEvent, listenerList
    • Constructor Summary

      Constructors 
      Constructor Description
      ComboBoxCellEditor​(javax.swing.JComboBox comboBox)
      Creates a new ComboBoxCellEditor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object getCellEditorValue()
      Returns the value contained in the combo box
      java.awt.Component getTableCellEditorComponent​(javax.swing.JTable table, java.lang.Object value, boolean isSelected, int row, int column)
      Sets an initial value for the combo box.
      boolean stopCellEditing()
      Tells the combo box to stop editing and accept any partially edited value as the value of the combo box.
      • Methods inherited from class javax.swing.AbstractCellEditor

        addCellEditorListener, cancelCellEditing, fireEditingCanceled, fireEditingStopped, getCellEditorListeners, isCellEditable, removeCellEditorListener, shouldSelectCell
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface javax.swing.CellEditor

        addCellEditorListener, cancelCellEditing, isCellEditable, removeCellEditorListener, shouldSelectCell
    • Constructor Detail

      • ComboBoxCellEditor

        public ComboBoxCellEditor​(javax.swing.JComboBox comboBox)
        Creates a new ComboBoxCellEditor.
        Parameters:
        comboBox - the comboBox that should be used as the cell editor.
    • Method Detail

      • getCellEditorValue

        public java.lang.Object getCellEditorValue()
        Returns the value contained in the combo box
        Specified by:
        getCellEditorValue in interface javax.swing.CellEditor
        Returns:
        the value contained in the combo box
      • stopCellEditing

        public boolean stopCellEditing()
        Tells the combo box to stop editing and accept any partially edited value as the value of the combo box. Always returns true.
        Specified by:
        stopCellEditing in interface javax.swing.CellEditor
        Overrides:
        stopCellEditing in class javax.swing.AbstractCellEditor
        Returns:
        true
      • getTableCellEditorComponent

        public java.awt.Component getTableCellEditorComponent​(javax.swing.JTable table,
                                                              java.lang.Object value,
                                                              boolean isSelected,
                                                              int row,
                                                              int column)
        Sets an initial value for the combo box. Returns the combo box that should be added to the client's Component hierarchy. Once installed in the client's hierarchy this combo box will then be able to draw and receive user input.
        Specified by:
        getTableCellEditorComponent in interface javax.swing.table.TableCellEditor
        Parameters:
        table - the JTable that is asking the editor to edit; can be null
        value - the value of the cell to be edited; null is a valid value
        isSelected - will be ignored
        row - the row of the cell being edited
        column - the column of the cell being edited
        Returns:
        the combo box for editing