下面列出了怎么用javax.swing.DefaultCellEditor的API类实例代码及写法,或者点击链接到github查看源代码。
public Test6505027(JFrame main) {
Container container = main;
if (INTERNAL) {
JInternalFrame frame = new JInternalFrame();
frame.setBounds(OFFSET, OFFSET, WIDTH, HEIGHT);
frame.setVisible(true);
JDesktopPane desktop = new JDesktopPane();
desktop.add(frame, new Integer(1));
container.add(desktop);
container = frame;
}
if (TERMINATE) {
this.table.putClientProperty(KEY, Boolean.TRUE);
}
TableColumn column = this.table.getColumn(COLUMNS[1]);
column.setCellEditor(new DefaultCellEditor(new JComboBox(ITEMS)));
container.add(BorderLayout.NORTH, new JTextField());
container.add(BorderLayout.CENTER, new JScrollPane(this.table));
}
public void setUpSportColumn(JTable table,
TableColumn sportColumn) {
//Set up the editor for the sport cells.
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Knitting");
comboBox.addItem("Speed reading");
comboBox.addItem("Pool");
comboBox.addItem("None of the above");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
/**
* Creates a new instance.
*
* @param portalProvider The application's portal provider.
* @param eventSource Where components can register for events.
*/
@Inject
public ContinuousLoadPanel(SharedKernelServicePortalProvider portalProvider,
@ApplicationEventBus EventSource eventSource) {
this.portalProvider = requireNonNull(portalProvider, "portalProvider");
this.eventSource = requireNonNull(eventSource, "eventSource");
initComponents();
JComboBox<TransportOrderData.Deadline> deadlineComboBox = new JComboBox<>();
deadlineComboBox.addItem(null);
for (TransportOrderData.Deadline i : TransportOrderData.Deadline.values()) {
deadlineComboBox.addItem(i);
}
DefaultCellEditor deadlineEditor = new DefaultCellEditor(deadlineComboBox);
toTable.setDefaultEditor(TransportOrderData.Deadline.class, deadlineEditor);
toTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
doTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
}
private void buildTableModels(TransportOrderData transportOrder) {
requireNonNull(transportOrder, "transportOrder");
// Drive orders
locationsComboBox.removeAllItems();
operationTypesComboBox.removeAllItems();
SortedSet<Location> sortedLocationSet = new TreeSet<>(Comparators.objectsByName());
sortedLocationSet.addAll(objectService.fetchObjects(Location.class));
for (Location i : sortedLocationSet) {
locationsComboBox.addItem(i.getReference());
}
locationsComboBox.addItemListener((ItemEvent e) -> locationsComboBoxItemStateChanged(e));
doTable.setModel(new DriveOrderTableModel(transportOrder.getDriveOrders()));
doTable.setDefaultEditor(TCSObjectReference.class, new DefaultCellEditor(locationsComboBox));
doTable.setDefaultEditor(String.class, new DefaultCellEditor(operationTypesComboBox));
// Properties
propertyTable.setModel(new PropertyTableModel(transportOrder.getProperties()));
}
public Test6505027(JFrame main) {
Container container = main;
if (INTERNAL) {
JInternalFrame frame = new JInternalFrame();
frame.setBounds(OFFSET, OFFSET, WIDTH, HEIGHT);
frame.setVisible(true);
JDesktopPane desktop = new JDesktopPane();
desktop.add(frame, new Integer(1));
container.add(desktop);
container = frame;
}
if (TERMINATE) {
this.table.putClientProperty(KEY, Boolean.TRUE);
}
TableColumn column = this.table.getColumn(COLUMNS[1]);
column.setCellEditor(new DefaultCellEditor(new JComboBox(ITEMS)));
container.add(BorderLayout.NORTH, new JTextField());
container.add(BorderLayout.CENTER, new JScrollPane(this.table));
}
public Test6505027(JFrame main) {
Container container = main;
if (INTERNAL) {
JInternalFrame frame = new JInternalFrame();
frame.setBounds(OFFSET, OFFSET, WIDTH, HEIGHT);
frame.setVisible(true);
JDesktopPane desktop = new JDesktopPane();
desktop.add(frame, new Integer(1));
container.add(desktop);
container = frame;
}
if (TERMINATE) {
this.table.putClientProperty(KEY, Boolean.TRUE);
}
TableColumn column = this.table.getColumn(COLUMNS[1]);
column.setCellEditor(new DefaultCellEditor(new JComboBox(ITEMS)));
container.add(BorderLayout.NORTH, new JTextField());
container.add(BorderLayout.CENTER, new JScrollPane(this.table));
}
public void updateGroupByItems(int dragRow, int dropRow) {
MyRow drag_row = (MyRow) model.getObjectForRow(dragRow);
JComboBox groupByComboDrag = (JComboBox) ((DefaultCellEditor) table.getCellEditor(dragRow, 6)).getComponent();
MyRow drop_row = (MyRow) model.getObjectForRow(dropRow);
JComboBox groupByComboDrop = (JComboBox) ((DefaultCellEditor) table.getCellEditor(dropRow, 6)).getComponent();
String sDrag = (String) groupByComboDrag.getItemAt(0);
String sDrop = (String) groupByComboDrop.getItemAt(0);
if ("".equals(sDrag) && !sDrag.equals(sDrop)) {
groupByComboDrop.insertItemAt("", 0);
groupByComboDrag.removeItem("");
}
if ("".equals(sDrop) && !sDrop.equals(sDrag)) {
groupByComboDrag.insertItemAt("", 0);
groupByComboDrop.removeItem("");
}
}
public Test6505027(JFrame main) {
Container container = main;
if (INTERNAL) {
JInternalFrame frame = new JInternalFrame();
frame.setBounds(OFFSET, OFFSET, WIDTH, HEIGHT);
frame.setVisible(true);
JDesktopPane desktop = new JDesktopPane();
desktop.add(frame, new Integer(1));
container.add(desktop);
container = frame;
}
if (TERMINATE) {
this.table.putClientProperty(KEY, Boolean.TRUE);
}
TableColumn column = this.table.getColumn(COLUMNS[1]);
column.setCellEditor(new DefaultCellEditor(new JComboBox(ITEMS)));
container.add(BorderLayout.NORTH, new JTextField());
container.add(BorderLayout.CENTER, new JScrollPane(this.table));
}
private void setupTableModel() {
tablemodelParameterValues = new ParameterTableModel(dataFiles, parameterValues);
tableParameterValues.setModel(tablemodelParameterValues);
for (int columnIndex =
0; columnIndex < (tablemodelParameterValues.getColumnCount() - 1); columnIndex++) {
UserParameter<?, ?> parameter = tablemodelParameterValues.getParameter(columnIndex + 1);
if (parameter instanceof ComboParameter) {
Object choices[] = ((ComboParameter<?>) parameter).getChoices();
tableParameterValues.getColumnModel().getColumn(columnIndex + 1)
.setCellEditor(new DefaultCellEditor(new JComboBox<Object>(choices)));
}
}
}
public Test6505027(JFrame main) {
Container container = main;
if (INTERNAL) {
JInternalFrame frame = new JInternalFrame();
frame.setBounds(OFFSET, OFFSET, WIDTH, HEIGHT);
frame.setVisible(true);
JDesktopPane desktop = new JDesktopPane();
desktop.add(frame, new Integer(1));
container.add(desktop);
container = frame;
}
if (TERMINATE) {
this.table.putClientProperty(KEY, Boolean.TRUE);
}
TableColumn column = this.table.getColumn(COLUMNS[1]);
column.setCellEditor(new DefaultCellEditor(new JComboBox(ITEMS)));
container.add(BorderLayout.NORTH, new JTextField());
container.add(BorderLayout.CENTER, new JScrollPane(this.table));
}
/**
* setDefaultTableEditorsClicks, This sets the number of clicks required to start the default
* table editors in the supplied table. Typically you would set the table editors to start after
* 1 click or 2 clicks, as desired.
*
* The default table editors of the table editors that are supplied by the JTable class, for
* Objects, Numbers, and Booleans. Note, the editor which is used to edit Objects, is the same
* editor used for editing Strings.
*/
public static void setDefaultTableEditorsClicks(JTable table, int clickCountToStart) {
TableCellEditor editor;
editor = table.getDefaultEditor(Object.class);
if (editor instanceof DefaultCellEditor) {
((DefaultCellEditor) editor).setClickCountToStart(clickCountToStart);
}
editor = table.getDefaultEditor(Number.class);
if (editor instanceof DefaultCellEditor) {
((DefaultCellEditor) editor).setClickCountToStart(clickCountToStart);
}
editor = table.getDefaultEditor(Boolean.class);
if (editor instanceof DefaultCellEditor) {
((DefaultCellEditor) editor).setClickCountToStart(clickCountToStart);
}
}
public void setUpSportColumn(JTable table, TableColumn sportColumn) {
// Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Knitting");
comboBox.addItem("Speed reading");
comboBox.addItem("Pool");
comboBox.addItem("None of the above");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
// Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
public Test6505027(JFrame main) {
Container container = main;
if (INTERNAL) {
JInternalFrame frame = new JInternalFrame();
frame.setBounds(OFFSET, OFFSET, WIDTH, HEIGHT);
frame.setVisible(true);
JDesktopPane desktop = new JDesktopPane();
desktop.add(frame, new Integer(1));
container.add(desktop);
container = frame;
}
if (TERMINATE) {
this.table.putClientProperty(KEY, Boolean.TRUE);
}
TableColumn column = this.table.getColumn(COLUMNS[1]);
column.setCellEditor(new DefaultCellEditor(new JComboBox(ITEMS)));
container.add(BorderLayout.NORTH, new JTextField());
container.add(BorderLayout.CENTER, new JScrollPane(this.table));
}
@Override
public TableCellEditor getCellEditor(int row, int column) {
if(showParamTypes) {
String paramName = (String) tableModel.getValueAt(row, 0);
Class type = (column == 2) ? (Class) tableModel.getValueAt(row, 1) : Boolean.class;
if (Enum.class.isAssignableFrom(type)) {
JComboBox combo = new JComboBox(type.getEnumConstants());
return new DefaultCellEditor(combo);
} else if (type == Boolean.class || type == Boolean.TYPE) {
JCheckBox cb = new JCheckBox();
cb.setHorizontalAlignment(JLabel.CENTER);
cb.setBorderPainted(true);
return new DefaultCellEditor(cb);
} else if (paramName.toLowerCase().contains(Constants.PASSWORD)) {
return new DefaultCellEditor(new JPasswordField());
}
}
return super.getCellEditor(row, column);
}
@Override
public void setRangeSearchableFields(Collection<String> rangeSearchableFields) {
pointRangeQueryTable.setModel(new PointTypesTableModel(rangeSearchableFields));
pointRangeQueryTable.setShowGrid(true);
String[] numTypes = Arrays.stream(PointTypesTableModel.NumType.values())
.map(PointTypesTableModel.NumType::name)
.toArray(String[]::new);
JComboBox<String> numTypesCombo = new JComboBox<>(numTypes);
numTypesCombo.setRenderer((list, value, index, isSelected, cellHasFocus) -> new JLabel(value));
pointRangeQueryTable.getColumnModel().getColumn(PointTypesTableModel.Column.TYPE.getIndex()).setCellEditor(new DefaultCellEditor(numTypesCombo));
pointRangeQueryTable.getColumnModel().getColumn(PointTypesTableModel.Column.TYPE.getIndex()).setCellRenderer(
(table, value, isSelected, hasFocus, row, column) -> new JLabel((String) value)
);
pointRangeQueryTable.getColumnModel().getColumn(PointTypesTableModel.Column.FIELD.getIndex()).setPreferredWidth(PointTypesTableModel.Column.FIELD.getColumnWidth());
pointRangeQueryTable.setPreferredScrollableViewportSize(pointRangeQueryTable.getPreferredSize());
// set default type to Integer
for (int i = 0; i < rangeSearchableFields.size(); i++) {
pointRangeQueryTable.setValueAt(PointTypesTableModel.NumType.INT.name(), i, PointTypesTableModel.Column.TYPE.getIndex());
}
}
DDTable(String[] headers, String titleKey, Editable editable) {
super(new Object[0][headers.length], headers);
this.headers = headers;
this.titleKey = titleKey;
this.editable = editable;
this.darkerColor = getBackground().darker();
setModel(new DDTableModel(headers, editable));
setColors(editable);
this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setIntercellSpacing(new Dimension(margin, margin));
DefaultCellEditor dce = new DefaultCellEditor(new CellText(this));
dce.setClickCountToStart(1);
getColumnModel().getColumn(0).setCellEditor(dce);
getColumnModel().getColumn(1).setCellEditor(dce);
}
@Override
public void actionPerformed(ActionEvent e) {
associationModel.addAlias("...");
removeButton.setEnabled(true);
aliasNameScrollPane.repaint();
int rowIndex = 0;
for (String aliasName : associationModel.getAliasNames()) {
if (aliasName.equals("...")) {
break;
}
rowIndex++;
}
DefaultCellEditor editor = (DefaultCellEditor) aliasNames.getCellEditor(rowIndex, 0);
aliasNames.editCellAt(rowIndex, 0);
final JTextField textField = (JTextField) editor.getComponent();
textField.requestFocus();
textField.selectAll();
}
@Override
public TableCellEditor getCellEditor(int row, int column) {
List<String> usedTypes = new LinkedList<String>();
for (int i = 0; i < Ontology.VALUE_TYPE_NAMES.length; i++) {
if ((i != Ontology.ATTRIBUTE_VALUE) && (i != Ontology.FILE_PATH)
&& (!Ontology.ATTRIBUTE_VALUE_TYPE.isA(i, Ontology.DATE_TIME))) {
usedTypes.add(Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(i));
}
}
String[] valueTypes = new String[usedTypes.size()];
int vCounter = 0;
for (String type : usedTypes) {
valueTypes[vCounter++] = type;
}
JComboBox<String> typeBox = new JComboBox<>(valueTypes);
typeBox.setBackground(javax.swing.UIManager.getColor("Table.cellBackground"));
return new DefaultCellEditor(typeBox);
}
public Test6505027(JFrame main) {
Container container = main;
if (INTERNAL) {
JInternalFrame frame = new JInternalFrame();
frame.setBounds(OFFSET, OFFSET, WIDTH, HEIGHT);
frame.setVisible(true);
JDesktopPane desktop = new JDesktopPane();
desktop.add(frame, new Integer(1));
container.add(desktop);
container = frame;
}
if (TERMINATE) {
this.table.putClientProperty(KEY, Boolean.TRUE);
}
TableColumn column = this.table.getColumn(COLUMNS[1]);
column.setCellEditor(new DefaultCellEditor(new JComboBox(ITEMS)));
container.add(BorderLayout.NORTH, new JTextField());
container.add(BorderLayout.CENTER, new JScrollPane(this.table));
}
private TableCellEditor createBaseQualifierEditor() {
List<Qualifier> qualifiers = engine.getQualifiers();
Collections.sort(qualifiers, new Comparator<Qualifier>() {
@Override
public int compare(Qualifier o1, Qualifier o2) {
return collator.compare(o1.getName(), o2.getName());
}
});
JComboBox box = new JComboBox();
box.addItem(null);
for (Qualifier qualifier : qualifiers)
box.addItem(qualifier);
return new DefaultCellEditor(box);
}
private Component getPortsPanel() {
final ActionMap actionMap = app.getContext().getActionMap(this.getClass(), this);
final JPanel portsPanel = new JPanel(new MigLayout("", "", ""));
portsPanel.setBorder(
BorderFactory.createTitledBorder(app.getResourceMap().getString("preset_dialog.ports.title")));
tableModel = new PortsTableModel(app, ports);
portsTable = new JTable(tableModel);
portsTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
portsTable.getSelectionModel()
.addListSelectionListener(e -> firePropertyChange(PROPERTY_PORT_SELECTED, false, isPortSelected()));
final JComboBox<Protocol> protocolComboBox = new JComboBox<>();
protocolComboBox.addItem(Protocol.TCP);
protocolComboBox.addItem(Protocol.UDP);
portsTable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(protocolComboBox));
portsPanel.add(new JScrollPane(portsTable), "spany 3");
portsPanel.add(new JButton(actionMap.get(ACTION_ADD_PORT)), "wrap");
portsPanel.add(new JButton(actionMap.get(ACTION_ADD_PORT_RANGE)), "wrap");
portsPanel.add(new JButton(actionMap.get(ACTION_REMOVE_PORT)), "wrap");
return portsPanel;
}
@Override
public TableCellEditor getCellEditor(int row, int column,
String columnName, TableModel tableModel, JTable table) {
if ( "value".equals(columnName) || "default".equals(columnName) ) {
int modelColIndex = table.convertColumnIndexToModel(column);
int modelRowIndex = table.convertRowIndexToModel(row);
Object value = this.tableModel.getValueAt(modelRowIndex, modelColIndex);
if ( value instanceof BasicDBList ) {
MongoDBArrayEditor editor = new MongoDBArrayEditor();
editor.setDBObject((BasicDBList)value);
return editor;
} else {
JXTextField field = new JXTextField(value.toString());
field.setPreferredSize(new Dimension(100, 36));
field.setBorder(null);
return new DefaultCellEditor(field);
}
}
return null;
}
private static void setColumn(final JTable table, final int columnIndex, final ArrayList<String> values0) {
// Copy the list of labels into a String[] with a leading "" as the default "no choice" value.
final String[] values = new String[values0.size() + 1];
values[0] = "";
for (int i = 0; i < values0.size(); i++) {
values[i + 1] = values0.get(i);
}
final TableColumn tcol = table.getColumnModel().getColumn(columnIndex);
tcol.setCellRenderer(new MappingCellRenderer(values));
tcol.setCellEditor(new DefaultCellEditor(new JComboBox<>(values)));
}
private void initCommAdaptersComboBox(VehicleEntry vehicleEntry,
int rowIndex,
SingleCellEditor adapterCellEditor) {
final CommAdapterComboBox comboBox = new CommAdapterComboBox();
commAdapterRegistry.findFactoriesFor(vehicleEntry.getVehicle())
.forEach(factory -> comboBox.addItem(factory));
// Set the selection to the attached comm adapter, (The vehicle is already attached to a comm
// adapter due to auto attachment on startup.)
comboBox.setSelectedItem(vehicleEntry.getCommAdapterFactory());
comboBox.setRenderer(new AdapterFactoryCellRenderer());
comboBox.addPopupMenuListener(new BoundsPopupMenuListener());
comboBox.addItemListener((ItemEvent evt) -> {
if (evt.getStateChange() == ItemEvent.DESELECTED) {
return;
}
// If we selected a comm adapter that's already attached, do nothing.
if (Objects.equals(evt.getItem(), vehicleEntry.getCommAdapterFactory())) {
LOG.debug("{} is already attached to: {}", vehicleEntry.getVehicleName(), evt.getItem());
return;
}
int reply = JOptionPane.showConfirmDialog(
null,
bundle.getString("CommAdapterComboBox.confirmation.driverChange.text"),
bundle.getString("CommAdapterComboBox.confirmation.driverChange.title"),
JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.NO_OPTION) {
return;
}
attachManager.attachAdapterToVehicle(getSelectedVehicleName(),
(VehicleCommAdapterFactory) comboBox.getSelectedItem());
});
adapterCellEditor.setEditorAt(rowIndex, new DefaultCellEditor(comboBox));
vehicleEntry.addPropertyChangeListener(comboBox);
}
@Override
public void initialize() {
if (isInitialized()) {
return;
}
// Get a kernel reference.
try {
sharedPortal = portalProvider.register();
}
catch (ServiceUnavailableException exc) {
LOG.warn("Kernel unavailable", exc);
return;
}
objectService = (TCSObjectService) sharedPortal.getPortal().getPlantModelService();
Set<Vehicle> vehicles = new TreeSet<>(Comparators.objectsByName());
vehicles.addAll(objectService.fetchObjects(Vehicle.class));
JComboBox<TCSObjectReference<Vehicle>> vehiclesComboBox = new JComboBox<>();
vehiclesComboBox.addItem(null);
vehiclesComboBox.setRenderer(new StringListCellRenderer<>(x -> x == null ? "" : x.getName()));
for (Vehicle curVehicle : vehicles) {
vehiclesComboBox.addItem(curVehicle.getReference());
}
DefaultCellEditor vehicleEditor = new DefaultCellEditor(vehiclesComboBox);
toTable.setDefaultEditor(TCSObjectReference.class, vehicleEditor);
toTable.setDefaultRenderer(
TCSObjectReference.class,
new StringTableCellRenderer<TCSObjectReference<?>>(x -> x == null ? "" : x.getName()));
doTable.getSelectionModel().addListSelectionListener(event -> updateElementStates());
propertyTable.getSelectionModel().addListSelectionListener(event -> updateElementStates());
updateElementStates();
initialized = true;
}
@Override
protected TreeCellEditor createTreeCellEditor() {
JTextField tf = new JTextField() {
@Override
public String getName() {
return "Tree.cellEditor";
}
};
DefaultCellEditor editor = new DefaultCellEditor(tf);
// One click to edit.
editor.setClickCountToStart(1);
return editor;
}
@Override
protected TreeCellEditor createTreeCellEditor() {
JTextField tf = new JTextField() {
@Override
public String getName() {
return "Tree.cellEditor";
}
};
DefaultCellEditor editor = new DefaultCellEditor(tf);
// One click to edit.
editor.setClickCountToStart(1);
return editor;
}
@Override
protected TreeCellEditor createTreeCellEditor() {
JTextField tf = new JTextField() {
@Override
public String getName() {
return "Tree.cellEditor";
}
};
DefaultCellEditor editor = new DefaultCellEditor(tf);
// One click to edit.
editor.setClickCountToStart(1);
return editor;
}
@Override
protected TreeCellEditor createTreeCellEditor() {
JTextField tf = new JTextField() {
@Override
public String getName() {
return "Tree.cellEditor";
}
};
DefaultCellEditor editor = new DefaultCellEditor(tf);
// One click to edit.
editor.setClickCountToStart(1);
return editor;
}
@Override
protected TreeCellEditor createTreeCellEditor() {
JTextField tf = new JTextField() {
@Override
public String getName() {
return "Tree.cellEditor";
}
};
DefaultCellEditor editor = new DefaultCellEditor(tf);
// One click to edit.
editor.setClickCountToStart(1);
return editor;
}