Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Widget enable event
Widget enable event [message #514533] Mon, 15 February 2010 17:21 Go to next message
antonio is currently offline antonioFriend
Messages: 22
Registered: July 2009
Junior Member
Hi,

is it possible get event when a widget is enabled/disabled (it becomes "gray" and not clickable):

widget.addListener(SWT.XXX, new Listener() {
public void handleEvent(Event event) {
if (event==YYY) do something();
});

//somewhere...
widget.setEnabled(false);

Eventually I would like to know what are XXX and YYY (for false/true enable) in the code.


Thanks,
Julio
Re: Widget enable event [message #514630 is a reply to message #514533] Tue, 16 February 2010 06:31 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
setEnabled method is in Control class...

And enabling and disabling in most cases will only be done programmatically,

Hence where ever you do enable or disable you can also notify about the same to somebody or something...

Whats your actual requirement??



---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Widget enable event [message #514632 is a reply to message #514630] Tue, 16 February 2010 06:39 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
I think SWT.Deactivate...

is the listener you are looking for...


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Widget enable event [message #514679 is a reply to message #514632] Tue, 16 February 2010 10:29 Go to previous messageGo to next message
antonio is currently offline antonioFriend
Messages: 22
Registered: July 2009
Junior Member
Hi thx for replies.

SWT.Deactivate seems not working. It catches signal only if widget was been clicked before (in my test it was a button).

public class SnippetCrap {

public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
Button ok = new Button (shell, SWT.PUSH);
final Button cancel = new Button (shell, SWT.PUSH);

ok.setText ("Enable");
ok.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Enable pressed");
cancel.setEnabled(!cancel.getEnabled());
}
});
cancel.setText ("Enabled");
cancel.addListener(SWT.Deactivate, new Listener() {
public void handleEvent(Event event) {
System.out.println("change status");
}
});
shell.setDefaultButton (cancel);
shell.setLayout (new RowLayout ());
shell.pack ();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}

So at the moment i solved with:

widget.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
if (event.data.equals("DISABLE")) doSomething();
if (event.data.equals("ENABLE")) ....;
}
}

and somewhere to activate listener above:

Event e = new Event();
e.data = "DISABLE; // ENABLE
widget.notifyListeners(SWT.Selection, e);

but I don't know if it's correct way to do this, because I'm using SWT.Selection (or whatever I choose).

Any idea?


Thanks,
Julio
Re: Widget enable event [message #514694 is a reply to message #514679] Tue, 16 February 2010 11:26 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
Why can not you do the task where ever you are desabling the control....

why to desable/enable it and then listen for the same and do your task???


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Widget enable event [message #514712 is a reply to message #514694] Tue, 16 February 2010 12:35 Go to previous messageGo to next message
antonio is currently offline antonioFriend
Messages: 22
Registered: July 2009
Junior Member
cos it need to do something when is enabled/disabled and to avoid coupling class

Thanks,
Julio
Re: Widget enable event [message #514762 is a reply to message #514712] Tue, 16 February 2010 14:59 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
SWT does not send notification when a control is enabled/disabled. If you
want the participants to be decoupled then you can use swt's event mechanism
to send events of your own, like in the snippet below. You would define two
new event types (make them large enough to not collide with current or
future swt event types), and every invocation of Control.setEnabled(...)
would be followed by Control.notifyListeners(...).

public static void main(String[] args) {
final int CUSTOM_EVENT_TYPE = 1234567890;
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setBounds(10,10,200,200);
shell.addListener(CUSTOM_EVENT_TYPE, new Listener() {
public void handleEvent(Event event) {
System.out.println("notified!");
}
});
shell.open();
Runnable runnable = new Runnable() {
public void run() {
Event event = new Event();
event.widget = shell;
event.type = CUSTOM_EVENT_TYPE;
shell.notifyListeners(CUSTOM_EVENT_TYPE, event);
display.timerExec(999, this);
}
};
runnable.run();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}

HTH,
Grant


"antonio" <antongiulio05@gmail.com> wrote in message
news:hle3e7$evn$1@build.eclipse.org...
> cos it need to do something when is enabled/disabled and to avoid coupling
class
> --
> Thanks,
> Julio
Re: Widget enable event [message #514802 is a reply to message #514762] Tue, 16 February 2010 16:52 Go to previous message
antonio is currently offline antonioFriend
Messages: 22
Registered: July 2009
Junior Member
Thanks Grant, so I wasn't completely wrong

Thanks,
Julio
Previous Topic:mouseExit event missing.
Next Topic:radio button group with no default selection
Goto Forum:
  


Current Time: Sun Dec 08 07:36:32 GMT 2024

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

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

Back to the top