Cross-posted: https://forums.oracle.com/forums/thread.jspa?threadID=2512538&tstart=0
Hello, everybody.
Is there a way to flush pending AWT events programmatically? I would like to be able to use it in a code like this:
if (comp.requestFocusInWindow())
{
// flush pending AWT events...
KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
if (focusManager.getPermanentFocusOwner() == comp)
{
// do something...
}
}
Thank you.
Marcos
PS: I know I can use a FocusListener in comp, but it is not an option in my case.
UPDATE:
The essence of my problem is this: to focus the actual editor componente of a JTable before the key that starts the editing is dispatched to it. So I came out with this solution:
private class TextFieldColumnEditor extends MyCustomTextField
{
// TODO
private static final long serialVersionUID = 1L;
private FocusListener _keyDispatcher;
#Override
protected boolean processKeyBinding(final KeyStroke ks, final KeyEvent e, int condition, boolean pressed)
{
InputMap bindings = getInputMap(condition);
ActionMap actions = getActionMap();
if (bindings != null && actions != null && isEnabled())
{
Object binding = bindings.get(ks);
final Action action = binding == null ? null : actions.get(binding);
if (action != null)
{
if (!isFocusOwner() && requestFocusInWindow())
{
// In case something went wrong last time and the
// listener wasn't unregistered.
removeFocusListener(_keyDispatcher);
_keyDispatcher =
new FocusListener()
{
#Override
public void focusGained(FocusEvent evt)
{
removeFocusListener(this);
SwingUtilities.invokeLater(
new Runnable()
{
#Override
public void run()
{
SwingUtilities.notifyAction(action, ks, e, CampoTextoDadosEditorColuna.this, e.getModifiers());
}
}
);
}
#Override
public void focusLost(FocusEvent e)
{
}
};
addFocusListener(_keyDispatcher);
return true;
}
else
{
return SwingUtilities.notifyAction(action, ks, e, this, e.getModifiers());
}
}
}
return false;
}
The class TextFieldColumnEditor is the componente I use in a custom cell editor. As you can see the solution is not perfect for a lot of reasons:
I had to copy the processKeyBinding code from JComponent and change it.
I don't know if SwingUtilities.notifyAction had success, because it is executed in another block of code and I can't return it from the processKeyBinding method. I assume it had. That's why I needed some kind of synchronous focus handling.
I'm sticking with this solution for the time being. Your comments and suggestions would be appreciated.
Marcos
UPDATE 2
Final (simplified) version, after suggestion by #VGR:
private class CellEditorTextField extends JTextField
{
#Override
protected boolean processKeyBinding(final KeyStroke ks, final KeyEvent e, int condition, boolean pressed)
{
InputMap bindings = getInputMap(condition);
ActionMap actions = getActionMap();
if (bindings != null && actions != null && isEnabled())
{
Object binding = bindings.get(ks);
final Action action = binding == null ? null : actions.get(binding);
if (action != null)
{
if (e.getSource() == YourJTable.this && action.isEnabled() && requestFocusInWindow())
{
SwingUtilities.invokeLater(
new Runnable()
{
#Override
public void run()
{
SwingUtilities.notifyAction(action, ks, e, CellEditorTextField.this, e.getModifiers());
}
}
);
return true;
}
else
{
return SwingUtilities.notifyAction(action, ks, e, this, e.getModifiers());
}
}
}
return false;
}
}
Comments and improvements are appreciated.
Related
I would like to create an event handler that listens for multiple key combinations such as holding Ctrl and C at the same time.
Why doesn't something like if((... == Control) && (... == C)) work?
Here is the code I trying to work with:
textField.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
if ((event.getCode() == KeyCode.CONTROL) && (event.getCode() == KeyCode.C)) {
System.out.println("Control pressed");
}
};
});
You can try this solution, it worked for me!
final KeyCombination keyCombinationShiftC = new KeyCodeCombination(
KeyCode.C, KeyCombination.CONTROL_DOWN);
textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (keyCombinationShiftC.match(event)) {
logger.info("CTRL + C Pressed");
}
}
});
One way to tackle this problem is to create a KeyCombination object and set some of its properties to what you see below.
Try the following:
textfield.getScene().getAccelerators().put(new KeyCodeCombination(
KeyCode.C, KeyCombination.CONTROL_ANY), new Runnable() {
#Override public void run() {
//Insert conditions here
textfield.requestFocus();
}
});
This would be of some help. KeyCombination.
final KeyCombination keyComb1=new KeyCodeCombination(KeyCode.C,KeyCombination.CONTROL_DOWN);
https://code.google.com/p/javafx-demos/source/browse/trunk/javafx-demos/src/main/java/com/ezest/javafx/demogallery/KeyCombinationDemo.java?r=27
A bit more concise (avoids new KeyCombination()):
public void handle(KeyEvent event) {
if (event.isControlDown() && (event.getCode() == KeyCode.C)) {
System.out.println("Control+C pressed");
}
};
There are methods of the type KeyEvent.isXXXDown() for the other modifier keys as well.
I need a customized TextArea component, where I'd like disable some keys (f.e. backspace and del keys). For this I created a vaadin-archetype-widget artifact, and I created two subclasses (MyTextArea and VMyTextArea), and I overrode the onKeyDown method in class VMyTextArea:
#Override
public void onKeyDown(KeyDownEvent event) {
int kc = event.getNativeKeyCode();
if (kc == KeyCodes.KEY_BACKSPACE || kc == KeyCodes.KEY_DELETE) {
return;
}
super.onKeyDown(event);
}
Unfortunately this solution doesn't solve my problem, the backspace and delete keys work normally. My question how to do this?
The solution:
public class VMyTextArea extends VTextArea {
VMyTextArea() {
super();
addKeyDownHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
int kc = event.getNativeKeyCode();
if (kc == KeyCodes.KEY_BACKSPACE || kc == KeyCodes.KEY_DELETE) {
event.preventDefault();
}
}
});
}
}
Can I make my textField in JTable acts like a cell in Excel?
Clear the text when typing in but can editing when get into the cell.
I think these 2 operations will goes to the same event. Am I wrong?
I try to use the keyPressed but nothing work. TT-TT
Here is my code
private JTable getTblMaster() {
if (tblMasterData == null) {
tblMasterData = new JTable() {
private static final long serialVersionUID = 1L;
public TableCellEditor getCellEditor(int row, int column) {
TableColumn tableColumn = getColumnModel()
.getColumn(column);
TableCellEditor editor = tableColumn.getCellEditor();
try {
if (editor == null) {
final JTextField text = new JTextField();
/*
text.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e){
}
});
SwingUtilities.invokeLater(new Runnable(){
public void run(){
}
});
*/
editor = new DefaultCellEditor(text);
;
return editor;
}
} catch (Exception e) {
LogWriter.error(e);
}
return editor;
}
};
}
return tblMasterData;
}
Any suggestion?
better could be select all text in the JTable cell
text.setText(text.getText())
text.selectAll
by wrapping into invokeLater()
great workaround Table Select All Editor by #camickr
Ohh! trashgod. Your example saved my life :D
All I needed was the code below and it worked. How easy! Thank you very very much.
private JTable getTblMaster() {
if (tblMasterData == null) {
tblMasterData = new JTable() {
public boolean editCellAt(int row, int column, EventObject e){
boolean result = super.editCellAt(row, column, e);
final Component editor = getEditorComponent();
if (editor == null || !(editor instanceof JTextComponent)) {
return result;
}
if (e instanceof KeyEvent) {
((JTextComponent) editor).selectAll();
}
return result;
} ....
I did it like this. First I am using the event keyReleased and then getting the row and column number on which I am working and then setting the value at that row. Code goes like this.
private void purchases_TBLKeyReleased(java.awt.event.KeyEvent evt) {
int rowWorking = purchases_TBL.getSelectedRow();
int columnWorking = purchases_TBL.getSelectedColumn();
if(columnWorking==3){
model.setValueAt(null, rowWorking, columnWorking);
}
}
This makes the third column of the table null as soon as I focus move on to that using keyboard.
Note: The same piece of code can be placed in MouseClicked event.
I have an editable JTable and have set a DefaultCellEditor like so:
colModel.getColumn( 1 ).setCellEditor( new DefaultCellEditor( txtEditBox ) {
// ...
#Override
public void cancelCellEditing() {
super.cancelCellEditing();
// handling the event
}
// ...
}
However, when pressing escape while editing a cell in this column, though the editing mode is finished, this method is not called. Any ideas why? Am I doing something wrong? Is there a way to handle this (other than manually adding a KeyListener that is)?
The official way: You can register a CellEditorListener: AbstractCellEditor.addCellEditorListener(...). If the editing is canceled, editingCanceled(ChangeEvent e) should be called. Due to a SUN bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6788481, editingCanceled is not called :(
As workaround you can register your own action for the ESCAPE key and handle it yourself. But it will not work for resize events.
Another solution (quick and dirty;-)): Overwrite the methode JTable.removeEditor() and insert your code after the super call.
I had this problem too. I wrote another workaround that involves ActionListener and FocusListener. This is it:
public class TableEditorListenerHelper {
// dealing with events
private final EventListenerList listeners = new EventListenerList();
private ChangeEvent changeEvent;
// cell editor that we're helping
private CellEditor editor;
// transient state
private boolean editing = false;
private JTable table;
public TableEditorListenerHelper(CellEditor editor, JTextField field) {
this.editor = editor;
field.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
field.addFocusListener(new FocusListener() {
#Override public void focusGained(FocusEvent e) {
editing = true;
}
#Override public void focusLost(FocusEvent e) {
JTable table = TableEditorListenerHelper.this.table;
if (editing && isEditing(table)) {
fireEditingCanceled();
}
}
private boolean isEditing(JTable table) { // a hack necessary to deal with focuslist vs table repaint
return table != null && table.isEditing();
}
});
}
public void setTable(JTable table) {
this.table = table;
}
public void addCellEditorListener(CellEditorListener l) {
listeners.add(CellEditorListener.class, l);
}
public void removeCellEditorListener(CellEditorListener l) {
listeners.remove(CellEditorListener.class, l);
}
public CellEditorListener[] getCellEditorListeners() {
return listeners.getListeners(CellEditorListener.class);
}
protected void fireEditingCanceled() {
for (CellEditorListener l : getCellEditorListeners()) {
l.editingCanceled(getOrCreateEvent());
}
resetEditingState();
}
protected void fireEditingStopped() {
for (CellEditorListener l : getCellEditorListeners()) {
l.editingStopped(getOrCreateEvent());
}
resetEditingState();
}
private void resetEditingState() {
table = null;
editing = false;
}
private ChangeEvent getOrCreateEvent() {
return changeEvent = changeEvent == null ? new ChangeEvent(editor) : changeEvent;
}
Here you can find a little more complete solution.
Another way fix this bug:
jTable.addPropertyChangeListener("tableCellEditor", e -> {
Object o = e.getOldValue();
if (o instanceof DefaultCellEditor) {
((DefaultCellEditor) o).cancelCellEditing();
}
});
Per accessibility requirements, Shift+F10 is supposed to open right-click context menus.
In Swing, one approach is to just add the key binding to every component you make. However, I've experimented with extending the EventQueue to handle all Shift+F10 presses. In particular, I've overridden dispatchEvent(AWTEvent) to convert Shift+F10 KeyEvents into right-click mousePresses:
protected void dispatchEvent(AWTEvent event) {
if (event instanceof KeyEvent) {
KeyEvent ev = (KeyEvent) event;
if ((ev.getKeyCode() == KeyEvent.VK_F10) &&
(ev.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK) > 0) {
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component comp = kfm.getFocusOwner();
Point mouse = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(mouse, comp);
eventToDispatch = new MouseEvent(comp,
MouseEvent.MOUSE_RELEASED, ev.getWhen(), 0, mouse.x, mouse.y,
1, true);
}
}
}
However, this prevents Shift+F10 from being able to close any JPopupMenus that get launched. Any idea if this solution is workable, or are there better ways to accomplish meeting this requirement?
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
try {
int dotPosition = textField.getCaretPosition();
Rectangle popupLocation = textField
.modelToView(dotPosition);
popup.show(textField, popupLocation.x, popupLocation.y);
} catch (BadLocationException badLocationException) {
System.out.println("Oops");
}
}
};
KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_F10,
InputEvent.SHIFT_MASK);
textField.registerKeyboardAction(actionListener, keystroke,
JComponent.WHEN_FOCUSED);