Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Del-accelerator conflicts with del-"action" in text field
Del-accelerator conflicts with del-"action" in text field [message #778591] Fri, 13 January 2012 07:53 Go to next message
Thomas Singer is currently offline Thomas Singer
Messages: 75
Registered: July 2009
Member
In the following sample, a shell is displayed with a text input field and a button. The menu contains a menu item with the accelerator <Del>. Unfortunately, the menu item is also invoked if the user pressed the del key in the input field. How to prevent that?

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class DelKeyTest {

	public static void main(String[] args) {
		final Display display = new Display();

		final Shell shell = new Shell(display, SWT.SHELL_TRIM);
		final Menu menuBar = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menuBar);
		final Menu fileMenu = new Menu(menuBar);
		final MenuItem fileMenuItem = new MenuItem(menuBar, SWT.CASCADE);
		fileMenuItem.setText("&File");
		fileMenuItem.setMenu(fileMenu);
		final MenuItem delItem = new MenuItem(fileMenu, SWT.PUSH);
		delItem.setText("Delete\tDel");
		delItem.setAccelerator(SWT.DEL);
		delItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.out.println("Menu item selected");
			}
		});

		shell.setLayout(new FillLayout());

		final Text textField = new Text(shell, SWT.SINGLE | SWT.BORDER);
		textField.setText("ab");
		textField.setSelection(1);

		new Button(shell, SWT.PUSH).setText("Here Del should invoke the menu item");

		shell.pack();
		shell.setMinimumSize(shell.getSize());
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		display.dispose();
	}
}


Thanks in advance.
Thomas
Re: Del-accelerator conflicts with del-"action" in text field [message #778615 is a reply to message #778591] Fri, 13 January 2012 09:14 Go to previous messageGo to next message
Lakshmi Shanmugam
Messages: 274
Registered: July 2009
Location: India
Senior Member
Hi,

I think the existing behavior is as expected.
To prevent the menuitem from being selected, you could disable it when the focus is in the input field.
But, in this case, since the delete action is valid in the input field, you could probably implement a delete action for the input field and call it on menu item's selection.




Lakshmi P Shanmugam
Re: Del-accelerator conflicts with del-"action" in text field [message #778625 is a reply to message #778615] Fri, 13 January 2012 10:03 Go to previous message
Thomas Singer is currently offline Thomas Singer
Messages: 75
Registered: July 2009
Member
Thank you for your feed-back.

Lakshmi Shanmugam wrote on Fri, 13 January 2012 09:14
I think the existing behavior is as expected.

I would not expect it. I would expect that the focused component could handle the key first, then all parent controls and if it was not processed, then the accelerator would be used. At least that's how Swing did it.

Lakshmi Shanmugam wrote on Fri, 13 January 2012 09:14
To prevent the menuitem from being selected, you could disable it when the focus is in the input field.

That would be VERY hard in our application, because the menu item (aka its belonging action) does not (and should not) know anything about input fields or a focused control.

Lakshmi Shanmugam wrote on Fri, 13 January 2012 09:14
But, in this case, since the delete action is valid in the input field, you could probably implement a delete action for the input field and call it on menu item's selection.

The delete key was just a sample. The user of our application is free to change the accelerators to their needs. Hence similar conflicts may happen for any key which is used by input fields.

Thomas
Previous Topic:Embedded ant versus external ant
Next Topic:Drag & Drop table - dragging items down past window too slow
Goto Forum:
  


Current Time: Thu Apr 17 17:35:41 EDT 2014

Powered by FUDForum. Page generated in 0.02579 seconds