下面列出了怎么用java.util.EventObject的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Add the event and vector of listeners to the queue to be delivered.
* An event dispatcher thread dequeues events from the queue and dispatches
* them to the registered listeners.
* Package private; used by NamingEventNotifier to fire events
*/
synchronized void queueEvent(EventObject event,
Vector<? extends NamingListener> vector) {
if (eventQueue == null)
eventQueue = new EventQueue();
/*
* Copy the vector in order to freeze the state of the set
* of EventListeners the event should be delivered to prior
* to delivery. This ensures that any changes made to the
* Vector from a target listener's method during the delivery
* of this event will not take effect until after the event is
* delivered.
*/
@SuppressWarnings("unchecked") // clone()
Vector<NamingListener> v =
(Vector<NamingListener>)vector.clone();
eventQueue.enqueue(event, v);
}
@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;
}
@Override
public void scaleModel(EventObject event) {
Origin origin = get(FigureConstants.ORIGIN);
if (origin != null) {
LocationFigure lf = getPresentationFigure();
Point2D exact
= origin.calculatePixelPositionExactly(lf.getModel().getPropertyLayoutPositionX(),
lf.getModel().getPropertyLayoutPositionY());
Point2D.Double anchor = new Point2D.Double(exact.getX(), exact.getY());
setBounds(anchor, anchor);
}
invalidate();
// Auch das Label aktualisieren
fireFigureChanged();
}
@Override
public boolean isCellEditable(EventObject o) {
if (o instanceof MouseEvent && o.getSource() instanceof JTree) {
JTree tree = (JTree)o.getSource();
MouseEvent e = (MouseEvent)o;
if (e.getClickCount() > 1) {
TreePath path = tree.getPathForLocation(e.getX(), e.getY());
Object leaf = path.getLastPathComponent();
if (leaf instanceof DefaultMutableTreeNode) {
Object content = ((DefaultMutableTreeNode)leaf).getUserObject();
return content instanceof ConfigEntryNode;
}
}
}
return false;
}
/**
* Overridden to return false, and if the event is a mouse event
* it is forwarded to the tree.<p>
* The behavior for this is debatable, and should really be offered
* as a property. By returning false, all keyboard actions are
* implemented in terms of the table. By returning true, the
* tree would get a chance to do something with the keyboard
* events. For the most part this is ok. But for certain keys,
* such as left/right, the tree will expand/collapse where as
* the table focus should really move to a different column. Page
* up/down should also be implemented in terms of the table.
* By returning false this also has the added benefit that clicking
* outside of the bounds of the tree node, but still in the tree
* column will select the row, whereas if this returned true
* that wouldn't be the case.
* <p>By returning false we are also enforcing the policy that
* the tree will never be editable (at least by a key sequence).
*/
@Override
public boolean isCellEditable(EventObject e) {
if (e instanceof MouseEvent) {
for (int counter = getColumnCount() - 1; counter >= 0;
counter--) {
if (getColumnClass(counter) == TreeTableModel.class) {
MouseEvent me = (MouseEvent)e;
MouseEvent newME = new MouseEvent(tree, me.getID(),
me.getWhen(), me.getModifiers(),
me.getX() - getCellRect(0, counter, true).x,
me.getY(), me.getClickCount(),
me.isPopupTrigger());
tree.dispatchEvent(newME);
break;
}
}
}
return false;
}
@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;
}
private void confirm(EventObject fe) {
JTextField tf = (JTextField) fe.getSource();
JComboBox combo = (JComboBox) tf.getParent();
if (combo==null)
return;
if (fe instanceof FocusEvent) {
combo.getEditor().getEditorComponent().removeFocusListener(this);
} else {
combo.getEditor().getEditorComponent().removeKeyListener(this);
}
Configuration config = configName==null ?
ConfigurationsManager.getDefault().duplicate(lastSelected, tf.getText(), tf.getText()):
ConfigurationsManager.getDefault().create(tf.getText(), tf.getText());
combo.setSelectedItem(config);
combo.setEditable(false);
}
@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;
}
/** Toggle the expanded state of a property set if either the event
* was a double click in the title area, a single click in the spinner
* area, or a keyboard event. */
private void maybeToggleExpanded(int row, EventObject e) {
boolean doExpand = true;
//If it's a mouse event, we need to check if it's a double click.
if (e instanceof MouseEvent) {
MouseEvent me = (MouseEvent) e;
doExpand = me.getClickCount() > 1;
//If not a double click, allow single click in the spinner margin
if (!doExpand) {
//marginWidth will definitely be initialized, you can't
//click something that isn't on the screen
doExpand = me.getPoint().x <= PropUtils.getMarginWidth();
}
}
if (doExpand) {
toggleExpanded(row);
}
}
public void confirm(EventObject fe) {
JTextField tf = (JTextField) fe.getSource();
JComboBox combo = (JComboBox) tf.getParent();
if (combo==null)
return;
if (fe instanceof FocusEvent) {
combo.getEditor().getEditorComponent().removeFocusListener(this);
} else {
combo.getEditor().getEditorComponent().removeKeyListener(this);
}
Configuration config = lastSelected;
config.setDisplayName(tf.getText());
combo.setSelectedItem(config);
combo.setEditable(false);
currentActiveItem = null;
}
/**
* Returns true if <code>event</code> is a <code>MouseEvent</code>
* and the click count is 1.
* @param event the event being studied
*/
protected boolean shouldStartEditingTimer(EventObject event) {
if((event instanceof MouseEvent) &&
SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
MouseEvent me = (MouseEvent)event;
return (me.getClickCount() == 1 &&
inHitRegion(me.getX(), me.getY()));
}
return false;
}
/**
* This is invoked if a <code>TreeCellEditor</code>
* is not supplied in the constructor.
* It returns a <code>TextField</code> editor.
* @return a new <code>TextField</code> editor
*/
protected TreeCellEditor createTreeCellEditor() {
Border aBorder = UIManager.getBorder("Tree.editorBorder");
DefaultCellEditor editor = new DefaultCellEditor
(new DefaultTextField(aBorder)) {
public boolean shouldSelectCell(EventObject event) {
boolean retValue = super.shouldSelectCell(event);
return retValue;
}
};
// One click to edit.
editor.setClickCountToStart(1);
return editor;
}
/**
* Returns true if <code>event</code> is <code>null</code>,
* or it is a <code>MouseEvent</code> with a click count > 2
* and <code>inHitRegion</code> returns true.
*
* @param event the event being studied
* @return whether editing can be started for the given {@code event}
*/
protected boolean canEditImmediately(EventObject event) {
if((event instanceof MouseEvent) &&
SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
MouseEvent me = (MouseEvent)event;
return ((me.getClickCount() > 2) &&
inHitRegion(me.getX(), me.getY()));
}
return (event == null);
}
/**
* Returns true if <code>event</code> is <code>null</code>,
* or it is a <code>MouseEvent</code> with a click count > 2
* and <code>inHitRegion</code> returns true.
*
* @param event the event being studied
* @return whether editing can be started for the given {@code event}
*/
protected boolean canEditImmediately(EventObject event) {
if((event instanceof MouseEvent) &&
SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
MouseEvent me = (MouseEvent)event;
return ((me.getClickCount() > 2) &&
inHitRegion(me.getX(), me.getY()));
}
return (event == null);
}
@Override
public boolean isCellEditable(EventObject e) {
if (e instanceof MouseEvent) {
return ((MouseEvent) e).getClickCount() > 1;
}
return true;
}
@Override
public boolean isCellEditable(EventObject e) {
if (e instanceof MouseEvent) {
return ((MouseEvent) e).getClickCount() > 1;
}
return true;
}
/**
* This is invoked if a <code>TreeCellEditor</code>
* is not supplied in the constructor.
* It returns a <code>TextField</code> editor.
* @return a new <code>TextField</code> editor
*/
protected TreeCellEditor createTreeCellEditor() {
Border aBorder = UIManager.getBorder("Tree.editorBorder");
DefaultCellEditor editor = new DefaultCellEditor
(new DefaultTextField(aBorder)) {
public boolean shouldSelectCell(EventObject event) {
boolean retValue = super.shouldSelectCell(event);
return retValue;
}
};
// One click to edit.
editor.setClickCountToStart(1);
return editor;
}
public ResultSetTableCellEditor(final JCheckBox checkBox) {
super(checkBox);
delegate = new EditorDelegate() {
@Override
public void setValue(Object value) {
val = value;
checkBox.setSelected((value instanceof Boolean) ? ((Boolean) value) : false);
}
@Override
public boolean isCellEditable(EventObject evt) {
if (evt instanceof MouseEvent) {
return ((MouseEvent) evt).getClickCount() >= 2;
}
return true;
}
@Override
public Object getCellEditorValue() {
Boolean bolVal = checkBox.isSelected();
if (val == null && !checkBox.isSelected()) {
return null;
} else {
return bolVal;
}
}
};
checkBox.addActionListener(delegate);
}
/**
* This is overriden to forward the event to the tree. This will
* return true if the click count >= 3, or the event is null.
*/
public boolean isCellEditable(EventObject e) {
if (e instanceof MouseEvent) {
MouseEvent me = (MouseEvent)e;
// If the modifiers are not 0 (or the left mouse button),
// tree may try and toggle the selection, and table
// will then try and toggle, resulting in the
// selection remaining the same. To avoid this, we
// only dispatch when the modifiers are 0 (or the left mouse
// button).
if (me.getModifiers() == 0 ||
me.getModifiers() == InputEvent.BUTTON1_MASK) {
for (int counter = getColumnCount() - 1; counter >= 0;
counter--) {
if (getColumnClass(counter) == TreeTableModel.class) {
MouseEvent newME = new MouseEvent
(JTreeTable.this.tree, me.getID(),
me.getWhen(), me.getModifiers(),
me.getX() - getCellRect(0, counter, true).x,
me.getY(), me.getClickCount(),
me.isPopupTrigger());
JTreeTable.this.tree.dispatchEvent(newME);
break;
}
}
}
if (me.getClickCount() >= 3) {
return treeEditable;
}
return false;
}
if (e == null) {
return treeEditable;
}
return false;
}
/**
* Constructs a <code>DefaultCellEditor</code> object that uses a
* combo box.
*
* @param comboBox a <code>JComboBox</code> object
*/
public DefaultCellEditor(final JComboBox<?> comboBox) {
editorComponent = comboBox;
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
delegate = new EditorDelegate() {
public void setValue(Object value) {
comboBox.setSelectedItem(value);
}
public Object getCellEditorValue() {
return comboBox.getSelectedItem();
}
public boolean shouldSelectCell(EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
MouseEvent e = (MouseEvent)anEvent;
return e.getID() != MouseEvent.MOUSE_DRAGGED;
}
return true;
}
public boolean stopCellEditing() {
if (comboBox.isEditable()) {
// Commit edited value.
comboBox.actionPerformed(new ActionEvent(
DefaultCellEditor.this, 0, ""));
}
return super.stopCellEditing();
}
};
comboBox.addActionListener(delegate);
}
/**
* Overriden to invoke repaint for the particular location if
* the column contains the tree. This is done as the tree editor does
* not fill the bounds of the cell, we need the renderer to paint
* the tree in the background, and then draw the editor over it.
*/
public boolean editCellAt(int row, int column, EventObject e){
boolean retValue = super.editCellAt(row, column, e);
if (retValue && getColumnClass(column) == TreeTableModel.class) {
repaint(getCellRect(row, column, false));
}
return retValue;
}
@Override
public boolean editCellAt(int row, int column, EventObject e) {
if ((e != null) && (!isEditIfNullEvent())) {
return false;
}
boolean res = super.editCellAt(row, column, e);
if ((res) && (cellEditor instanceof DefaultCellEditor)) {
((DefaultCellEditor) cellEditor).getComponent().requestFocus();
}
return res;
}
@Override
public boolean isCellEditable(EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
return ((MouseEvent) anEvent).getClickCount() >= 2;
}
return super.isCellEditable(anEvent);
}
/** Getter for the target listener.
* @param ev the event the we want to distribute
* @return null if there is no listener because it has been finalized
*/
protected final java.util.EventListener get(java.util.EventObject ev) {
Object l = ref.get(); // get the consumer
// if the event consumer is gone, unregister us from the event producer
if (l == null) {
ref.requestCleanUp((ev == null) ? null : ev.getSource());
}
return (EventListener) l;
}
/**
* Returns true if <code>event</code> is a <code>MouseEvent</code>
* and the click count is 1.
* @param event the event being studied
*/
protected boolean shouldStartEditingTimer(EventObject event) {
if((event instanceof MouseEvent) &&
SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
MouseEvent me = (MouseEvent)event;
return (me.getClickCount() == 1 &&
inHitRegion(me.getX(), me.getY()));
}
return false;
}
private TableCellEditor createFilesColumnCellEditor() {
JComboBox<Object> comboBox = new GComboBox<>();
DefaultCellEditor cellEditor = new DefaultCellEditor(comboBox) {
@Override
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
@Override
public Component getTableCellEditorComponent(JTable jtable, Object value,
boolean isSelected, int row, int column) {
comboBox.setSelectedItem("");
comboBox.removeAllItems();
BatchGroup rowVal = tableModel.getRowObject(row);
comboBox.addItem("" + rowVal.size() + " files...");
for (BatchLoadConfig batchLoadConfig : rowVal.getBatchLoadConfig()) {
comboBox.addItem(batchLoadConfig.getPreferredFileName());
}
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
};
cellEditor.setClickCountToStart(2);
return cellEditor;
}
/**
* If the <code>realEditor</code> returns true to this
* message, <code>prepareForEditing</code>
* is messaged and true is returned.
*/
public boolean isCellEditable(EventObject event) {
boolean retValue = false;
boolean editable = false;
if (event != null) {
if (event.getSource() instanceof JTree) {
setTree((JTree)event.getSource());
if (event instanceof MouseEvent) {
TreePath path = tree.getPathForLocation(
((MouseEvent)event).getX(),
((MouseEvent)event).getY());
editable = (lastPath != null && path != null &&
lastPath.equals(path));
if (path!=null) {
lastRow = tree.getRowForPath(path);
Object value = path.getLastPathComponent();
boolean isSelected = tree.isRowSelected(lastRow);
boolean expanded = tree.isExpanded(path);
TreeModel treeModel = tree.getModel();
boolean leaf = treeModel.isLeaf(value);
determineOffset(tree, value, isSelected,
expanded, leaf, lastRow);
}
}
}
}
if(!realEditor.isCellEditable(event))
return false;
if(canEditImmediately(event))
retValue = true;
else if(editable && shouldStartEditingTimer(event)) {
startEditingTimer();
}
else if(timer != null && timer.isRunning())
timer.stop();
if(retValue)
prepareForEditing();
return retValue;
}
/**
* If the <code>realEditor</code> returns true to this
* message, <code>prepareForEditing</code>
* is messaged and true is returned.
*/
public boolean isCellEditable(EventObject event) {
boolean retValue = false;
boolean editable = false;
if (event != null) {
if (event.getSource() instanceof JTree) {
setTree((JTree)event.getSource());
if (event instanceof MouseEvent) {
TreePath path = tree.getPathForLocation(
((MouseEvent)event).getX(),
((MouseEvent)event).getY());
editable = (lastPath != null && path != null &&
lastPath.equals(path));
if (path!=null) {
lastRow = tree.getRowForPath(path);
Object value = path.getLastPathComponent();
boolean isSelected = tree.isRowSelected(lastRow);
boolean expanded = tree.isExpanded(path);
TreeModel treeModel = tree.getModel();
boolean leaf = treeModel.isLeaf(value);
determineOffset(tree, value, isSelected,
expanded, leaf, lastRow);
}
}
}
}
if(!realEditor.isCellEditable(event))
return false;
if(canEditImmediately(event))
retValue = true;
else if(editable && shouldStartEditingTimer(event)) {
startEditingTimer();
}
else if(timer != null && timer.isRunning())
timer.stop();
if(retValue)
prepareForEditing();
return retValue;
}
public boolean shouldSelectCell(EventObject anEvent) {
return false;
}
@Override
public boolean isCellEditable(EventObject e) {
return true;
}