Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Problem forcing focus in combo text field(Problem forcing focus in combo text field)
Problem forcing focus in combo text field [message #869760] Fri, 04 May 2012 04:49 Go to next message
hortiz Mising name is currently offline hortiz Mising name
Messages: 82
Registered: July 2009
Member
Hi,
I use a combo to list files from a directory.
As the number of files can be important, I allow the user to enter characters in the combo text field to filter combo content, by keeping only matching file names.

I also automatically open the drop-down list to help the user finding the right file.
My problem is that once the list is open, the combo text field lost focus and the user has to click again in the text field to continue entering characters.

I tried to solve this with a call to the forceFocus method but it doesn't work.
You can find a snippet showing the problem.
(I work on Linux / GTK)

Thanks,
Helene


The main class:

import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class Main {

	public static void main(String[] args) {
		Main main = new Main();
		main.init();
	}

	private void init() {

		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new RowLayout());
		MyCombo myCombo = new MyCombo(shell);
		
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep ();
			}
		}
		display.dispose();
	}
}



MyCombo class:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Shell;


public class MyCombo {
	private Combo combo;
	private String[] items;

	
	
	public MyCombo(Shell shell) {
		combo = new Combo(shell, SWT.NONE);
		initItems();
		combo.setItems(items);
		combo.addModifyListener(new MyComboModifyListener());
	}
	
	private void initItems() {
		items = new String[] {"aaa", "aab", "aac", "abb", "abc", "bbb", "bbc", "bbd"};
	}
	
	
	private class MyComboModifyListener implements ModifyListener {
		public void modifyText(ModifyEvent event) {
			filter();
		}
	}
	
	
	private void filter() {
		for (int i = 0; i < items.length; i++) {
			if (items[i].startsWith(combo.getText()) && !containsItem(items[i])) {
				combo.add(items[i]);
			} else if (!items[i].startsWith(combo.getText()) && containsItem(items[i])) {
				combo.remove(items[i]);
			}
		}
		if (combo.getItemCount() > 0) {
			combo.setListVisible(true);
			combo.forceFocus();			// <----- this doesn't seem to work
		}
	}
	
	
	private boolean containsItem(String item) {
		for (int i = 0; i < combo.getItems().length; i++) {
			if (combo.getItem(i).equals(item)) {
				return true;
			}
		}
		return false;
	}
}

Re: Problem forcing focus in combo text field [message #870524 is a reply to message #869760] Tue, 08 May 2012 10:25 Go to previous messageGo to next message
Grant Gayed is currently offline Grant Gayed
Messages: 2148
Registered: July 2009
Senior Member
Hi Helene,

Showing the list while focus is in the text entry seems to go against
the behaviour of the native GTK Combo control. I've tried in standalone
gtk-demo to drop down a combo and then put focus in its text entry and
this always fails. It looks like the Combo wants the list to always
have focus if it's visible, and to hide the list whenever it loses
focus. So I don't think you'll get this to work.

An alternate implementation would be to show a Text field (possibly with
a "drop down" Button placed beside it), and show the "drop down" content
manually in a pop-up shell. See
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet320.java
for an example of this.

HTH,
Grant


On 5/4/2012 4:49 AM, hortiz Mising name wrote:
> Hi,
> I use a combo to list files from a directory.
> As the number of files can be important, I allow the user to enter
> characters in the combo text field to filter combo content, by keeping
> only matching file names.
>
> I also automatically open the drop-down list to help the user finding
> the right file.
> My problem is that once the list is open, the combo text field lost
> focus and the user has to click again in the text field to continue
> entering characters.
> I tried to solve this with a call to the forceFocus method but it
> doesn't work.
> You can find a snippet showing the problem.
> (I work on Linux / GTK)
>
> Thanks,
> Helene
>
>
> The main class:
>
>
> import org.eclipse.swt.layout.RowLayout;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
>
>
> public class Main {
>
> public static void main(String[] args) {
> Main main = new Main();
> main.init();
> }
>
> private void init() {
>
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new RowLayout());
> MyCombo myCombo = new MyCombo(shell);
>
> shell.pack();
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) {
> display.sleep ();
> }
> }
> display.dispose();
> }
> }
>
>
>
> MyCombo class:
>
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.ModifyEvent;
> import org.eclipse.swt.events.ModifyListener;
> import org.eclipse.swt.widgets.Combo;
> import org.eclipse.swt.widgets.Shell;
>
>
> public class MyCombo {
> private Combo combo;
> private String[] items;
>
>
>
> public MyCombo(Shell shell) {
> combo = new Combo(shell, SWT.NONE);
> initItems();
> combo.setItems(items);
> combo.addModifyListener(new MyComboModifyListener());
> }
>
> private void initItems() {
> items = new String[] {"aaa", "aab", "aac", "abb", "abc", "bbb", "bbc",
> "bbd"};
> }
>
>
> private class MyComboModifyListener implements ModifyListener {
> public void modifyText(ModifyEvent event) {
> filter();
> }
> }
>
>
> private void filter() {
> for (int i = 0; i < items.length; i++) {
> if (items[i].startsWith(combo.getText()) && !containsItem(items[i])) {
> combo.add(items[i]);
> } else if (!items[i].startsWith(combo.getText()) &&
> containsItem(items[i])) {
> combo.remove(items[i]);
> }
> }
> if (combo.getItemCount() > 0) {
> combo.setListVisible(true);
> combo.forceFocus(); // <----- this doesn't seem to work
> }
> }
>
>
> private boolean containsItem(String item) {
> for (int i = 0; i < combo.getItems().length; i++) {
> if (combo.getItem(i).equals(item)) {
> return true;
> }
> }
> return false;
> }
> }
>
>
Re: Problem forcing focus in combo text field [message #870890 is a reply to message #870524] Thu, 10 May 2012 03:47 Go to previous message
hortiz Mising name is currently offline hortiz Mising name
Messages: 82
Registered: July 2009
Member
Hi Grant,

Thanks for your answer !
It helps me a lot.

Helene
Previous Topic:Can't catch CTRL-C if the user releases the keys in the wrong order.
Next Topic:How can a widget be disposed without a call to dispose()?
Goto Forum:
  


Current Time: Mon Jul 28 10:34:43 EDT 2014

Powered by FUDForum. Page generated in 0.02045 seconds