Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Detecting Combo Events
Detecting Combo Events [message #447883] Tue, 21 December 2004 01:00 Go to next message
Tyrone Hed is currently offline Tyrone HedFriend
Messages: 79
Registered: July 2009
Member
How do you get the text that was on an SWT Combo box when an event was
triggered? I can trigger the event but I can't for the life of me figure
out how to capture the information I see contained in the event.

Any suggestions?

I have a combo box within a table that is itself within a CTabFolder. I
want to pull out the event but nothing seems to produce the text.


Thank you,
Ty
Re: Detecting Combo Events [Code Included] [message #447909 is a reply to message #447883] Tue, 21 December 2004 18:58 Go to previous messageGo to next message
Tyrone Hed is currently offline Tyrone HedFriend
Messages: 79
Registered: July 2009
Member
Here's my problem. No matter what I do, I cannot get access to the text
or index on the Combo box. Although, when I debug, the event shows it
carries the text that occurred with the event, I cannot find out how to
pull out this information. The event always pulls back "" (blank) text.

Does anyone know how to correctly interpret events from a Combo box? I
have two examples below. I have gotten both to trigger events but I have
never been able to figure out how to grab that event information.

Thank you,
Ty


Combo claimsDrop = new Combo( table, SWT.DROP_DOWN | SWT.READ_ONLY |
SWT.SIMPLE );
claimsDrop.addModifyListener(
new ModifyListener()
{
public void modifyText(ModifyEvent e)
{
int i = claimsDrop.getSelectionIndex();
String text = claimsDrop.getItem( i );
System.out.println("your selection is " + text );
}
}
);

claimsDrop.addSelectionListener(
new SelectionAdapter()
{
public void widgetSelected( SelectionEvent se )
{
String text = claimsDrop.getText();
System.out.println( "text=" + text );
int i = claimsDrop.getSelectionIndex();
String text2 = claimsDrop.getItem( i );
System.out.println( "text2=" + text2 );
}
}
);
ANSWERED: Detecting Combo Events [message #447910 is a reply to message #447883] Tue, 21 December 2004 19:55 Go to previous messageGo to next message
Tyrone Hed is currently offline Tyrone HedFriend
Messages: 79
Registered: July 2009
Member
Folks: I answered my own question. If there is a cleaner way, I would
still prefer to hear it.

PROBLEM: A user clicks on a drop down (Combo) box. I want to trap these
events and determine the text that was on the drop down when the user
clicked on it.

SOLUTION: It's easy to trigger the events. The hard part is grabbing the
events. To grab the event, I discover if the type of the event was Combo.
If so, I cast it to a Combo box type and execute the Combo's getText()
method. It works!

Combo claims = new Combo( shell, SWT.DROP_DOWN | SWT.READ_ONLY |
SWT.SIMPLE );
...
claims.addSelectionListener(

new SelectionAdapter()
{
public void widgetSelected( SelectionEvent se )
{
Object ev = se.getSource();

if( ev instanceof Combo )
{
Combo c = (Combo) ev;
String text = c.getText();
System.out.println( "text=" + text );
}
}
}
);
Re: Detecting Combo Events [Code Included] [message #447911 is a reply to message #447909] Tue, 21 December 2004 19:56 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
WORKSFORME

Here is my output when I use the arrow keys and select with the mouse:

your selection is Item 1
text=Item 1
text2=Item 1
your selection is Item 2
text=Item 2
text2=Item 2
your selection is Item 3
text=Item 3
text2=Item 3
your selection is Item 4
text=Item 4
text2=Item 4

Here is the test code:

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

public class EclipseCorner {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
final Combo claimsDrop = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
for (int i=0; i<32; i++) claimsDrop.add ("Item " + i);
claimsDrop.select(0);
claimsDrop.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
int i = claimsDrop.getSelectionIndex();
String text = claimsDrop.getItem(i);
System.out.println("your selection is " + text);
}
});
claimsDrop.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent se) {
String text = claimsDrop.getText();
System.out.println("text=" + text);
int i = claimsDrop.getSelectionIndex();
String text2 = claimsDrop.getItem(i);
System.out.println("text2=" + text2);
}
});
claimsDrop.pack ();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

"Tyrone Hed" <tyronehed@earthlink.net> wrote in message
news:cq9rpb$c1h$1@www.eclipse.org...
> Here's my problem. No matter what I do, I cannot get access to the text
> or index on the Combo box. Although, when I debug, the event shows it
> carries the text that occurred with the event, I cannot find out how to
> pull out this information. The event always pulls back "" (blank) text.
>
> Does anyone know how to correctly interpret events from a Combo box? I
> have two examples below. I have gotten both to trigger events but I have
> never been able to figure out how to grab that event information.
>
> Thank you,
> Ty
>
>
> Combo claimsDrop = new Combo( table, SWT.DROP_DOWN | SWT.READ_ONLY |
> SWT.SIMPLE );
> claimsDrop.addModifyListener(
> new ModifyListener()
> {
> public void modifyText(ModifyEvent e)
> {
> int i = claimsDrop.getSelectionIndex();
> String text = claimsDrop.getItem( i );
> System.out.println("your selection is " + text );
> }
> }
> );
>
> claimsDrop.addSelectionListener(
> new SelectionAdapter()
> {
> public void widgetSelected( SelectionEvent se )
> {
> String text = claimsDrop.getText();
> System.out.println( "text=" + text );
> int i = claimsDrop.getSelectionIndex();
> String text2 = claimsDrop.getItem( i );
> System.out.println( "text2=" + text2 );
> }
> }
> );
>
>
Re: Detecting Combo Events [Code Included] [message #447913 is a reply to message #447911] Tue, 21 December 2004 20:53 Go to previous message
Tyrone Hed is currently offline Tyrone HedFriend
Messages: 79
Registered: July 2009
Member
Steve,
Thank you kindly for replying. Your example indeed works. I guess my
problem is slightly different. In your example you're using an anonymous
handler. I was using a separate class as the handler.
In my separate-class handler, I have the following code:

else if( event == tabs.claimsDrop )
{
String text = tabs.claimsDrop.getText();
System.out.println( "text=" + text );
}
'tabs' is a reference to a Shell that displays a table.
'claimsDrop' is a Combo box that appears in a Table that itself lives
in a TabFolder on the 'tabs' screen. Although I can see the references
above ('tabs' and 'claimsDrop' have scope that is visible), I'm not,
apparently, referencing them correctly. Although the Combo box triggers
the event correctly, the reference of the event does not match the event
I'm referencing above. Perhaps I'm asking too much, but do you have any
insights why it does not recognise my handler reference?

thank you,
Ty




Steve Northover wrote:

> WORKSFORME

> Here is my output when I use the arrow keys and select with the mouse:

> your selection is Item 1
> text=Item 1
> text2=Item 1
> your selection is Item 2
> text=Item 2
> text2=Item 2
> your selection is Item 3
> text=Item 3
> text2=Item 3
> your selection is Item 4
> text=Item 4
> text2=Item 4

> Here is the test code:

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

> public class EclipseCorner {
> public static void main(String[] args) {
> final Display display = new Display();
> final Shell shell = new Shell(display);
> final Combo claimsDrop = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
> for (int i=0; i<32; i++) claimsDrop.add ("Item " + i);
> claimsDrop.select(0);
> claimsDrop.addModifyListener(new ModifyListener() {
> public void modifyText(ModifyEvent e) {
> int i = claimsDrop.getSelectionIndex();
> String text = claimsDrop.getItem(i);
> System.out.println("your selection is " + text);
> }
> });
> claimsDrop.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(SelectionEvent se) {
> String text = claimsDrop.getText();
> System.out.println("text=" + text);
> int i = claimsDrop.getSelectionIndex();
> String text2 = claimsDrop.getItem(i);
> System.out.println("text2=" + text2);
> }
> });
> claimsDrop.pack ();
> shell.pack();
> shell.open();
> while (!shell.isDisposed()) {
> if (display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
> }

> "Tyrone Hed" <tyronehed@earthlink.net> wrote in message
> news:cq9rpb$c1h$1@www.eclipse.org...
>> Here's my problem. No matter what I do, I cannot get access to the text
>> or index on the Combo box. Although, when I debug, the event shows it
>> carries the text that occurred with the event, I cannot find out how to
>> pull out this information. The event always pulls back "" (blank) text.
>>
>> Does anyone know how to correctly interpret events from a Combo box? I
>> have two examples below. I have gotten both to trigger events but I have
>> never been able to figure out how to grab that event information.
>>
>> Thank you,
>> Ty
>>
>>
>> Combo claimsDrop = new Combo( table, SWT.DROP_DOWN | SWT.READ_ONLY |
>> SWT.SIMPLE );
>> claimsDrop.addModifyListener(
>> new ModifyListener()
>> {
>> public void modifyText(ModifyEvent e)
>> {
>> int i = claimsDrop.getSelectionIndex();
>> String text = claimsDrop.getItem( i );
>> System.out.println("your selection is " + text );
>> }
>> }
>> );
>>
>> claimsDrop.addSelectionListener(
>> new SelectionAdapter()
>> {
>> public void widgetSelected( SelectionEvent se )
>> {
>> String text = claimsDrop.getText();
>> System.out.println( "text=" + text );
>> int i = claimsDrop.getSelectionIndex();
>> String text2 = claimsDrop.getItem( i );
>> System.out.println( "text2=" + text2 );
>> }
>> }
>> );
>>
>>
Previous Topic:Screen capture how-to's
Next Topic:mouse over clickable region in TableTreeViewer
Goto Forum:
  


Current Time: Mon Oct 26 14:54:34 GMT 2020

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

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

Back to the top