Skip to main content



      Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Widget enable event
Widget enable event [message #514533] Mon, 15 February 2010 12:21 Go to next message
Eclipse UserFriend
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.
Re: Widget enable event [message #514630 is a reply to message #514533] Tue, 16 February 2010 01:31 Go to previous messageGo to next message
Eclipse UserFriend
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??

Re: Widget enable event [message #514632 is a reply to message #514630] Tue, 16 February 2010 01:39 Go to previous messageGo to next message
Eclipse UserFriend
I think SWT.Deactivate...

is the listener you are looking for...
Re: Widget enable event [message #514679 is a reply to message #514632] Tue, 16 February 2010 05:29 Go to previous messageGo to next message
Eclipse UserFriend
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?
Re: Widget enable event [message #514694 is a reply to message #514679] Tue, 16 February 2010 06:26 Go to previous messageGo to next message
Eclipse UserFriend
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???
Re: Widget enable event [message #514712 is a reply to message #514694] Tue, 16 February 2010 07:35 Go to previous messageGo to next message
Eclipse UserFriend
cos it need to do something when is enabled/disabled and to avoid coupling class
Re: Widget enable event [message #514762 is a reply to message #514712] Tue, 16 February 2010 09:59 Go to previous messageGo to next message
Eclipse UserFriend
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 11:52 Go to previous message
Eclipse UserFriend
Thanks Grant, so I wasn't completely wrong
Previous Topic:mouseExit event missing.
Next Topic:radio button group with no default selection
Goto Forum:
  


Current Time: Thu Jul 03 18:57:25 EDT 2025

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

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

Back to the top