Java Code Examples for javax.swing.table.TableCellEditor

The following examples show how to use javax.swing.table.TableCellEditor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: hottub   Source File: OldJTable.java    License: GNU General Public License v2.0 6 votes vote down vote up
public TableColumn addColumn(Object columnIdentifier, int width,
                             TableCellRenderer renderer,
                             TableCellEditor editor, List columnData) {
    checkDefaultTableModel();

    // Set up the model side first
    DefaultTableModel m = (DefaultTableModel)getModel();
    m.addColumn(columnIdentifier, columnData.toArray());

    // The column will have been added to the end, so the index of the
    // column in the model is the last element.
    TableColumn newColumn = new TableColumn(
            m.getColumnCount()-1, width, renderer, editor);
    super.addColumn(newColumn);
    return newColumn;
}
 
Example 2
public boolean editCellAt(int index, EventObject e) {
	if (cellEditor != null && !cellEditor.stopCellEditing()) {
		return false;
	}
	if (!isCellEditable(index)) {
		return false;
	}
	TableCellEditor editor = getCellEditor(index);

	if (editor != null && editor.isCellEditable(e)) {
		editorComp = prepareEditor(editor, index);
		editorComp.setBounds(getHeaderRect(index));
		add(editorComp);
		editorComp.validate();
		setCellEditor(editor);
		setEditingColumn(index);
		editor.addCellEditorListener(this);

		return true;
	}
	return false;
}
 
Example 3
Source Project: ramus   Source File: BaseDialog.java    License: GNU General Public License v3.0 6 votes vote down vote up
private void commitComponent(final Component container) {
    if (container == null)
        return;
    if (container instanceof JTable) {
        TableCellEditor cellEditor = ((JTable) container).getCellEditor();
        if (cellEditor != null) {
            try {
                cellEditor.stopCellEditing();
            } catch (Exception e) {
                try {
                    cellEditor.cancelCellEditing();
                } catch (Exception ex) {

                }
            }
        }
    }
}
 
Example 4
Source Project: gameserver   Source File: MyTable.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public TableCellEditor getCellEditor(int row, int column) {
	if ( editorFactory != null ) {
		TableModel tableModel = this.getModel();
		if ( tableModel != null ) {
			String columnName = getColumnName(column, tableModel);
			//Use original row / column index
			TableCellEditor editor = this.editorFactory.getCellEditor(
					row, column, columnName, tableModel, this);
			if ( editor != null ) {
				return editor;
			}
		}
	}
	return super.getCellEditor(row, column);
}
 
Example 5
@Override
public final boolean editCellAt(final int row, final int column, EventObject e) {
    if (LOGGER.isLoggable(Level.TRACE)) {
        LOGGER.log(Level.TRACE, "editCellAt(row="+row+", col="+column+
                ", e="+e+")");
    }
    if (JConsole.isDebug()) {
        System.err.println("edit: "+getValueName(row)+"="+getValue(row));
    }
    boolean retVal = super.editCellAt(row, column, e);
    if (retVal) {
        final TableCellEditor tableCellEditor =
                getColumnModel().getColumn(column).getCellEditor();
        if (tableCellEditor == valueCellEditor) {
            ((JComponent) tableCellEditor).requestFocus();
        }
    }
    return retVal;
}
 
Example 6
Source Project: cuba   Source File: DesktopAbstractTable.java    License: Apache License 2.0 6 votes vote down vote up
protected TableCellEditor getCellEditor(int row, int column) {

        TableColumn tableColumn = impl.getColumnModel().getColumn(column);
        if (tableColumn.getIdentifier() instanceof Column) {
            Column columnConf = (Column) tableColumn.getIdentifier();

            if (editableColumns != null
                    && columnConf.getId() instanceof MetaPropertyPath
                    && editableColumns.contains(columnConf.getId())) {

                return tableFieldFactory.createEditComponent(row, columnConf);
            }
        }

        return null;
    }
 
Example 7
Source Project: openjdk-8   Source File: OldJTable.java    License: GNU General Public License v2.0 6 votes vote down vote up
public TableColumn addColumn(Object columnIdentifier, int width,
                             TableCellRenderer renderer,
                             TableCellEditor editor, List columnData) {
    checkDefaultTableModel();

    // Set up the model side first
    DefaultTableModel m = (DefaultTableModel)getModel();
    m.addColumn(columnIdentifier, columnData.toArray());

    // The column will have been added to the end, so the index of the
    // column in the model is the last element.
    TableColumn newColumn = new TableColumn(
            m.getColumnCount()-1, width, renderer, editor);
    super.addColumn(newColumn);
    return newColumn;
}
 
Example 8
Source Project: consulo   Source File: PropertyTable.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void valueCommitted(PropertyEditor source, boolean continueEditing, boolean closeEditorOnError) {
  if (isEditing()) {
    Object value;
    TableCellEditor tableCellEditor = cellEditor;

    try {
      value = tableCellEditor.getCellEditorValue();
    }
    catch (Exception e) {
      showInvalidInput(e);
      return;
    }

    if (setValueAtRow(editingRow, value)) {
      if (!continueEditing) {
        tableCellEditor.stopCellEditing();
      }
    }
    else if (closeEditorOnError) {
      tableCellEditor.cancelCellEditing();
    }
  }
}
 
Example 9
@Override
public TableCellEditor getCellEditor(int row, int column) {
	if (column == 0) { // RowNo.
		if (row == IS_SELECTED_ROW) {
			return globalCheckBoxCellEditor;
		}
		return super.getCellEditor();
	}
	column--;
	if (row == VALUE_TYPE_ROW) {
		return valueTypeCellEditor;
	}
	if (row == IS_SELECTED_ROW) {
		return checkBoxCellEditor;
	}
	if (row == ROLE_ROW) {
		return roleCellEditor;
	}
	// ATTRIBUTE_NAME_ROW
	return super.getCellEditor(row, column);
}
 
Example 10
Source Project: ghidra   Source File: MemoryMapProvider1Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testEditName() throws Exception {

	table.addRowSelectionInterval(0, 0);
	Rectangle rect = table.getCellRect(0, MemoryMapModel.NAME, true);
	clickMouse(table, 1, rect.x, rect.y, 2, 0);
	waitForPostedSwingRunnables();

	SwingUtilities.invokeAndWait(() -> {
		int row = 0;
		TableCellEditor editor = table.getCellEditor(row, MemoryMapModel.NAME);
		Component c = editor.getTableCellEditorComponent(table,
			model.getValueAt(row, MemoryMapModel.NAME), true, row, MemoryMapModel.NAME);
		JTextField tf = (JTextField) c;

		tf.setText(".test");
		editor.stopCellEditing();
	});
	waitForPostedSwingRunnables();
	assertEquals(".test", model.getValueAt(0, MemoryMapModel.NAME));
}
 
Example 11
Source Project: openjdk-8   Source File: XMBeanAttributes.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public final boolean editCellAt(final int row, final int column, EventObject e) {
    if (LOGGER.isLoggable(Level.FINER)) {
        LOGGER.finer("editCellAt(row="+row+", col="+column+
                ", e="+e+")");
    }
    if (JConsole.isDebug()) {
        System.err.println("edit: "+getValueName(row)+"="+getValue(row));
    }
    boolean retVal = super.editCellAt(row, column, e);
    if (retVal) {
        final TableCellEditor tableCellEditor =
                getColumnModel().getColumn(column).getCellEditor();
        if (tableCellEditor == valueCellEditor) {
            ((JComponent) tableCellEditor).requestFocus();
        }
    }
    return retVal;
}
 
Example 12
Source Project: PolyGlot   Source File: PDeclensionGridPanel.java    License: MIT License 6 votes vote down vote up
/**
 * Gets map of all declined word forms. Key = combined ID, value = word form
 * @return 
 */
@Override
public Map<String, String> getAllDecValues() {
    Map<String, String> ret = new HashMap<>();
    
    TableCellEditor editor = table.getCellEditor();
    if (editor != null) {
        editor.stopCellEditing();
    }
    
    decIdsToGridLocation.entrySet().forEach((entry) -> {
        Object val = table.getModel().getValueAt(entry.getValue().height, entry.getValue().width);
        ret.put(entry.getKey(), val == null ? "" : (String)val);
    });
    
    return ret;
}
 
Example 13
Source Project: ganttproject   Source File: GPTreeTableBase.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public Component prepareEditor(TableCellEditor editor, int row, int column) {
  Component result = super.prepareEditor(editor, row, column);
  if (result instanceof JTextComponent) {
    Object textFieldFont = UIManager.get("TextField.font");
    if (textFieldFont instanceof Font) {
      result.setFont((Font) textFieldFont);
    }

    if (Boolean.TRUE == getClientProperty("GPTreeTableBase.clearText")) {
      ((JTextComponent) result).setText("");
    }
    if (Boolean.TRUE == getClientProperty("GPTreeTableBase.selectAll")) {
      SwingUtilities.invokeLater(TreeTableCellEditorImpl.createSelectAllCommand((JTextComponent) result));
    }
    if (Boolean.TRUE == getClientProperty("GPTreeTableBase.unselectAll")) {
      SwingUtilities.invokeLater(TreeTableCellEditorImpl.createUnselectAllCommand((JTextComponent) result));
      putClientProperty("GPTreeTableBase.unselectAll", false);
    }
  }
  return result;
}
 
Example 14
private static Pair<JLabel, ? extends TableCellEditor> createDateValidatorComponents(final String hint, DateFormat... dateFormats) {
  Supplier<List<DateFormat>> formatSupplier = Suppliers.<List<DateFormat>>ofInstance(Lists.newArrayList(dateFormats));
  final JLabel hintLabel = new JLabel(" "); // non-empty label to occupy some vertical space
  final ValueValidator<Date> realValidator = UIUtil.createStringDateValidator(null, formatSupplier);
  ValueValidator<Date> decorator = new ValueValidator<Date>() {
    @Override
    public Date parse(String text) throws ValidationException {
      try {
        Date result = realValidator.parse(text);
        hintLabel.setText("");
        return result;
      } catch (ValidationException e) {
        e.printStackTrace();
        hintLabel.setText(hint);
        throw e;
      }
    }
  };
  GPDateCellEditor dateEditor = new GPDateCellEditor(null, true, decorator, formatSupplier);
  return Pair.create(hintLabel, dateEditor);
}
 
Example 15
Source Project: darklaf   Source File: DarkMultiCellEditor.java    License: MIT License 5 votes vote down vote up
private TableCellEditor getEditor(final Object value) {
    if (value instanceof Number) {
        return spinnerEditor.get();
    } else {
        return getDelegate();
    }
}
 
Example 16
Source Project: netbeans   Source File: EditablePropertyDisplayer.java    License: Apache License 2.0 5 votes vote down vote up
private void cancelEditor() {
    if (getInplaceEditor() != null) {
        java.awt.Container parent = getParent();
        while (parent != null && !(parent instanceof javax.swing.JTable)) {
            parent = parent.getParent();
        }
        if (parent != null) {
            TableCellEditor tce = ((javax.swing.JTable) parent).getCellEditor();
            if (tce != null) {
                tce.cancelCellEditing();
            }
        }
    }
}
 
Example 17
public void setData( final Element[] elements ) {
  final TableCellEditor tableCellEditor = table.getCellEditor();
  if ( tableCellEditor != null ) {
    tableCellEditor.stopCellEditing();
  }

  dataModel.setData( elements );
}
 
Example 18
Source Project: jdk8u_jdk   Source File: TableTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        KeyboardFocusManager.getCurrentKeyboardFocusManager();
        System.setSecurityManager(new AppletSecurity());

        JTable table = new JTable();
        TableCellEditor de = table.getDefaultEditor(Double.class);
        if (de == null) {
            throw new RuntimeException("Table default editor is null");
        }
    }
 
Example 19
Source Project: netbeans   Source File: DelegatingCellEditor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean stopCellEditing() {
    if (currentEditor != null) {
        boolean status = currentEditor.stopCellEditing();
        if (status) {
            canceledEditorRef = new WeakReference<TableCellEditor>(currentEditor);
            currentEditor = null;
        }
        return status;
    }
    return true;
}
 
Example 20
public void cancelCellEditing() {
    if (LOGGER.isLoggable(Level.FINER)) {
        LOGGER.finer("Cancel Editing Row: "+getEditingRow());
    }
    final TableCellEditor tableCellEditor = getCellEditor();
    if (tableCellEditor != null) {
        tableCellEditor.cancelCellEditing();
    }
}
 
Example 21
Source Project: chipster   Source File: EditableHeader.java    License: MIT License 5 votes vote down vote up
public void editingStopped(ChangeEvent e) {
	TableCellEditor editor = getCellEditor();
	if (editor != null) {
		Object value = editor.getCellEditorValue();
		int index = getEditingColumn();
		columnModel.getColumn(index).setHeaderValue(value);
		removeEditor();
	}
}
 
Example 22
Source Project: GpsPrune   Source File: TextFileLoader.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Close the combo box on the selected row of the field table
 * @param inRow currently selected row number
 */
private void closeTableComboBox(int inRow)
{
	TableCellEditor editor = _fieldTable.getCellEditor(inRow, 1);
	if (editor != null)
	{
		editor.stopCellEditing();
	}
}
 
Example 23
public void stopCellEditing() {
    if (LOGGER.isLoggable(Level.FINER)) {
        LOGGER.finer("Stop Editing Row: "+getEditingRow());
    }
    final TableCellEditor tableCellEditor = getCellEditor();
    if (tableCellEditor != null) {
        tableCellEditor.stopCellEditing();
    }
}
 
Example 24
/**
 * Cancels on-going cell editing 
 */
public void cancelEditing() {
  TableCellEditor editor = getCellEditor();
  if (editor != null) {
    editor.cancelCellEditing();
  }    
}
 
Example 25
Source Project: netbeans   Source File: ProfilerTable.java    License: Apache License 2.0 5 votes vote down vote up
public Component prepareEditor(TableCellEditor editor, int row, int column) {
    Component c = super.prepareEditor(editor, row, column);
    
    c.setForeground(getSelectionForeground());
    c.setBackground(getSelectionBackground());
    
    return c;
}
 
Example 26
@Override
public void editingStopped(ChangeEvent e) {
	TableCellEditor editor = getCellEditor();
	if (editor != null) {
		Object value = editor.getCellEditorValue();
		int index = getEditingColumn();
		columnModel.getColumn(index).setHeaderValue(value);
		removeEditor();
	}
}
 
Example 27
Source Project: ghidra   Source File: FunctionEditorDialog.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Component prepareEditor(TableCellEditor editor, int row, int column) {
	Component component = super.prepareEditor(editor, row, column);
	if (component != null && !"Storage".equals(getColumnName(column))) {
		component.removeFocusListener(focusListener);
		component.addFocusListener(focusListener);
	}
	return component;
}
 
Example 28
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 5 votes vote down vote up
private static JTable makeTable(TableModel model) {
  return new JTable(model) {
    @Override public void updateUI() {
      // [JDK-6788475] Changing to Nimbus LAF and back doesn't reset look and feel of JTable completely - Java Bug System
      // https://bugs.openjdk.java.net/browse/JDK-6788475
      // XXX: set dummy ColorUIResource
      setSelectionForeground(new ColorUIResource(Color.RED));
      setSelectionBackground(new ColorUIResource(Color.RED));
      super.updateUI();
      updateRenderer();
      JCheckBox checkBox = makeBooleanEditor(this);
      setDefaultEditor(Boolean.class, new DefaultCellEditor(checkBox));
    }

    private void updateRenderer() {
      TableModel m = getModel();
      for (int i = 0; i < m.getColumnCount(); i++) {
        TableCellRenderer r = getDefaultRenderer(m.getColumnClass(i));
        if (r instanceof Component) {
          SwingUtilities.updateComponentTreeUI((Component) r);
        }
      }
    }

    @Override public Component prepareEditor(TableCellEditor editor, int row, int column) {
      Component c = super.prepareEditor(editor, row, column);
      if (c instanceof JCheckBox) {
        JCheckBox b = (JCheckBox) c;
        b.setBackground(getSelectionBackground());
        b.setBorderPainted(true);
      }
      return c;
    }
  };
}
 
Example 29
Source Project: netbeans   Source File: DisplayTable.java    License: Apache License 2.0 5 votes vote down vote up
/**
    * Creates a combobox for a cell editor. 
    *
    * @return the combobox that is used as the editor.
    */
   public JComboBox setChoices(int row, int col, String[] choices,
			boolean editable) { 
       TableCellEditor ed = new ComboBoxTableCellEditor(choices);                          
cellEditors[row][col] = ed;

// if the table is editable, we should turn off the [...] editor
// when there's a choice on the row.
data[row][2]=NbBundle.getBundle(DisplayTable.class).getString("MON_Editing");  
cellEditors[row][2] = null;
       
       return ((ComboBoxTableCellEditor)ed).getComboBox();
   }
 
Example 30
Source Project: netbeans   Source File: SheetTable.java    License: Apache License 2.0 5 votes vote down vote up
/** Overridden to set the colors apropriately - we always want the editor
* to appear selected */
@Override
public Component prepareEditor(TableCellEditor editor, int row, int col) {
    if (editor == null) {
        return null;
    }

    Component result = super.prepareEditor(editor, row, col);

    if (result == null) {
        return null;
    }

    if( 1 == col ) {
    //Usually result == ine, but custom impls may not be
    InplaceEditor ine = getEditor().getInplaceEditor();

    if (ine.supportsTextEntry()) {
        result.setBackground(PropUtils.getTextFieldBackground());
        result.setForeground(PropUtils.getTextFieldForeground());
    }
    }

    if (result instanceof JComponent) {
        //unlikely that it won't be
        ((JComponent) result).setBorder(BorderFactory.createEmptyBorder(0, PropUtils.getTextMargin(), 0, 0));
    }

    return result;
}