Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » radio button - setselection - listener
radio button - setselection - listener [message #465915] Thu, 22 December 2005 11:08 Go to next message
MP is currently offline MPFriend
Messages: 6
Registered: July 2009
Junior Member
I have some radio buttons in a group, which all have a selection listener
that change topcontrol in a composite on the form. When running the
application and I click a radio button the topcontrol does get changed
correctly.

However when I change the selected radio button with setselection nothing
happens. Doesn't the selectionlistener get fired when selection changes in
code?

What am I missing?
Re: radio button - setselection - listener [message #465918 is a reply to message #465915] Thu, 22 December 2005 14:06 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Merle.Evelyne.ordirope.com

I believe that the selectionListener dont' get fired where selection are
processed via setSelection.

Use notifyListeners : myButton.notifyListeners(SWT.Selection, new Event());
Re: radio button - setselection - listener [message #465919 is a reply to message #465918] Thu, 22 December 2005 14:14 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
The method notifyListeners() is not intended for this use. Note also that
there are fields in the event that need to be filled in and your code below
does not show this.

The method notifyListeners() is intended to be used when developing custom
widgets.

Rather than calling notifyListeners(), you should just call the same method
you call in the Selection event handler to do the work.


Listener listener = new Listener () {
public void handleEvent (Event e) {
doSelection();
}
};
button.addListener (SWT.Selection, listener);
....
button.setSelection(true);
doSelection();


"Evelyne Merle" <Merle.Evelyne@ordirope.com> wrote in message
news:doebqv$dgk$1@utils.eclipse.org...
>I believe that the selectionListener dont' get fired where selection are
>processed via setSelection.
>
> Use notifyListeners : myButton.notifyListeners(SWT.Selection, new
> Event());
>
>
Re: radio button - setselection - listener [message #465920 is a reply to message #465915] Thu, 22 December 2005 14:27 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
If radio button 1 is currently selected and the user selects radio button 2,
two selection events will be sent - a selection for radio button 2 and a
deselection for radio button1. Make sure you are handling the right case.
In the selection event handler, check the button state using
button.getSelection().

When setSelection() is called, no selection event is sent. You must invoke
the code that does the selection/deselection work yourself.

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
Listener listener = new Listener() {
public void handleEvent (Event e) {
doSelection((Button)e.widget);
}
};
for (int i = 0; i < 10; i++) {
Button b = new Button(shell, SWT.RADIO);
b.setText("radio "+i);
b.addListener(SWT.Selection, listener);
}
Button last = new Button(shell, SWT.RADIO);
last.setText("last");
last.addListener(SWT.Selection, listener);
last.setSelection(true);
doSelection(last);

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
static void doSelection(Button button) {
if (button.getSelection()){
System.out.println("do work for selection "+button);
} else {
System.out.println("do work for deselection "+button);
}
}

"MP" <pijnmar@home.nl> wrote in message
news:b0860b19af4d773b4857b805f2f07243$1@www.eclipse.org...
>I have some radio buttons in a group, which all have a selection listener
>that change topcontrol in a composite on the form. When running the
>application and I click a radio button the topcontrol does get changed
>correctly.
>
> However when I change the selected radio button with setselection nothing
> happens. Doesn't the selectionlistener get fired when selection changes in
> code?
> What am I missing?
>
Re: radio button - setselection - listener [message #465922 is a reply to message #465919] Thu, 22 December 2005 14:40 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Merle.Evelyne.ordirope.com

Veronika, I've noticed that calling notifyListeners is not intended for this
use.

Your solution works when the setSelection is done in the class that
implements Listener (or SelectionListener).

Is there another solution when the button listeners are not known by the
class calling setSelection ?


"Veronika Irvine" <veronika_irvine@oti.com> a
Re: radio button - setselection - listener [message #465924 is a reply to message #465915] Thu, 22 December 2005 15:04 Go to previous messageGo to next message
MP is currently offline MPFriend
Messages: 6
Registered: July 2009
Junior Member
Moved listener code into a procedure and called procedure again when
selecting radio button with setselection. That solved it :-)

Thank you
Re: radio button - setselection - listener [message #466041 is a reply to message #465920] Thu, 29 December 2005 18:26 Go to previous message
Sanjay Chaudhuri is currently offline Sanjay ChaudhuriFriend
Messages: 19
Registered: July 2009
Junior Member
Veronika,

For Radio buttons, I prefer to use the FocusListener, instead of SelectionListener, because then, you do not need to do a second check to see which of the radio button is currently selected.

I am not sure, if I am missing something.
Previous Topic:RowLayout and scrollable Table in a View?!?
Next Topic:CTabFolder focus on new tab
Goto Forum:
  


Current Time: Wed Apr 24 21:19:32 GMT 2024

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

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

Back to the top