Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Type ahead in a Combo--Modified solution
Type ahead in a Combo--Modified solution [message #445780] Thu, 11 November 2004 03:29 Go to next message
Eclipse UserFriend
Originally posted by: ukalumni.hotmail.com

Searching through this newsgroup, I found someone who had a class that
would do type aheads in a Combo (sorry, can't remember who). The class
_almost_ worked for me, but not quite. One of the problems that I still
haven't figured out is how to select part of the text in a Combo. On GTK
linux, I just can't get that to work.

So, I cleaned up the original class and modified it to work without
having to do partial selection of the text of the current item. It might
be slow on Combos with a huge list of entries, but for Combos that do
not have huge lists of entries, it seems to work pretty well.

Although I'm open to any suggestions, that's not why I'm posting it. I'm
just hopeful that it will be useful to someone else. I appreciate all of
the help I'm getting from this forum.

--Michael

import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.*;
/**
* @author mmolloy
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class ComboModifyListener implements ModifyListener
{
boolean programmaticallyChanging = false;
Combo combo;

public void modifyText(ModifyEvent e)
{
combo = (Combo)e.getSource();
if (!programmaticallyChanging) {
int index = indexOfClosestMatch(combo.getText());
int length = combo.getText().length();
switch (index) {
case -1:
programmaticallyChanging = true;
combo.setText("");
programmaticallyChanging = false;
break;
default:
programmaticallyChanging = true;
combo.select(index);
combo.setText(combo.getItem(index));
combo.setFocus();
// combo.setSelection(new Point(length,
combo.getText().length()));
programmaticallyChanging = false;
break;
}
}
}

public int indexOfClosestMatch (String text)
{
int index = -1;
for(int j = 0; j < text.length(); j++ )
{
String[] items = combo.getItems();
for (int i = 0; i < items.length; ++i)
{
String substring = text.substring(0,j);
if(j == 0)
substring = text.substring(0,1);
if (items[i].toUpperCase().startsWith(substring.toUpperCase()))
{
index = i;
break;
}
}

}
return index;
}
}
Re: Type ahead in a Combo--Modified solution [message #446141 is a reply to message #445780] Wed, 17 November 2004 19:47 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ukalumni.hotmail.com

Sorry if this has gotten annoying, but I needed to make a correction.
The last class I posted works fine on linux, but does not work correctly
on Windows. This version works on Windows.

--Michael

public class ComboModifyListener implements ModifyListener
{
boolean programmaticallyChanging = false;
Combo combo;

public void modifyText(ModifyEvent e)
{
combo = (Combo)e.getSource();
if (!programmaticallyChanging) {
int index = indexOfClosestMatch(combo.getText());
int length = combo.getText().length();
switch (index) {
case -1:
programmaticallyChanging = true;
combo.setText("");
programmaticallyChanging = false;
break;
default:
programmaticallyChanging = true;
combo.setText(combo.getItem(index));
combo.select(index);

combo.setSelection(new
org.eclipse.swt.graphics.Point(length, combo.getText().length()));
programmaticallyChanging = false;
break;
}
}
}

public int indexOfClosestMatch (String text)
{
int index = -1;
// for(int j = 0; j < text.length(); j++ )
// {
String[] items = combo.getItems();
for (int i = 0; i < items.length; ++i)
{
if(items[i].toUpperCase().startsWith(text.toUpperCase()))
{
index = i;
break;
}
// String substring = text.substring(0,j);
// if(j == 0)
// substring = text.substring(0,1);
// if (items[i].toUpperCase().startsWith(substring.toUpperCase()))
// {
// index = i;
// break;
// }
}

// }
return index;
}
}
Re: Type ahead in a Combo--Modified solution [message #446167 is a reply to message #446141] Thu, 18 November 2004 16:04 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
What was the problem?

"Michael Molloy" <ukalumni@hotmail.com> wrote in message
news:cnga3t$qdl$1@www.eclipse.org...
> Sorry if this has gotten annoying, but I needed to make a correction.
> The last class I posted works fine on linux, but does not work correctly
> on Windows. This version works on Windows.
>
> --Michael
>
> public class ComboModifyListener implements ModifyListener
> {
> boolean programmaticallyChanging = false;
> Combo combo;
>
> public void modifyText(ModifyEvent e)
> {
> combo = (Combo)e.getSource();
> if (!programmaticallyChanging) {
> int index = indexOfClosestMatch(combo.getText());
> int length = combo.getText().length();
> switch (index) {
> case -1:
> programmaticallyChanging = true;
> combo.setText("");
> programmaticallyChanging = false;
> break;
> default:
> programmaticallyChanging = true;
> combo.setText(combo.getItem(index));
> combo.select(index);
>
> combo.setSelection(new
> org.eclipse.swt.graphics.Point(length, combo.getText().length()));
> programmaticallyChanging = false;
> break;
> }
> }
> }
>
> public int indexOfClosestMatch (String text)
> {
> int index = -1;
> // for(int j = 0; j < text.length(); j++ )
> // {
> String[] items = combo.getItems();
> for (int i = 0; i < items.length; ++i)
> {
> if(items[i].toUpperCase().startsWith(text.toUpperCase()))
> {
> index = i;
> break;
> }
> // String substring = text.substring(0,j);
> // if(j == 0)
> // substring = text.substring(0,1);
> // if (items[i].toUpperCase().startsWith(substring.toUpperCase()))
> // {
> // index = i;
> // break;
> // }
> }
>
> // }
> return index;
> }
> }
Re: Type ahead in a Combo--Modified solution [message #446192 is a reply to message #446167] Fri, 19 November 2004 20:34 Go to previous message
Eclipse UserFriend
Originally posted by: ukalumni.hotmail.com

Haven't had time to debug it. The behavior on linux gtk is that no text
is ever selected. If I have a list of states including Texas and
Tennessee, pressing 't' (sans quotes) brings up Tennesse, as expected,
although no text is selected.

Typing 'e' deletes the entire line, so there's never a point in typing 'x'.

Typing 't' a second time also deletes the entire line. Typing 't' a
third time brings up 'Tennessee' again.

However, this exact class behaves exactly as expected on Windows.
Because my users are all running windows, the linux behavior is only
annoying to me, and I don't have to maintain two versions. I simply
don't use the type ahead feature when I'm testing it.

Thanks,
--Michael

Steve Northover wrote:
> What was the problem?
>
> "Michael Molloy" <ukalumni@hotmail.com> wrote in message
> news:cnga3t$qdl$1@www.eclipse.org...
>
>>Sorry if this has gotten annoying, but I needed to make a correction.
>>The last class I posted works fine on linux, but does not work correctly
>>on Windows. This version works on Windows.
>>
>>--Michael
Previous Topic:Content of List disappear after click Finish
Next Topic:eclipse Preferences
Goto Forum:
  


Current Time: Mon Oct 19 22:36:45 GMT 2020

Powered by FUDForum. Page generated in 0.01985 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top