How enable and disable buttons using event in SWT. [message #1006467] |
Thu, 31 January 2013 12:16 |
|
I'm creating one swt project . In that i have 1 dialog with 4 buttons.
What i need:
With one Button which is enable if i click then other will enable and this will disable.
And again from other button if i click all other should disable and 1st one should enable.
How I'm doing
Simple way to do this is create one function which invert the visibility. and call that method from widget Selection event.
What i want
Is there is any other way (Standard) to do this like using fireevent.
Thanks
|
|
|
Re: How enable and disable buttons using event in SWT. [message #1006475 is a reply to message #1006467] |
Thu, 31 January 2013 12:45 |
|
Hi sumit,
the JFace Databinding might be an alternative :
DataBindingContext dbc = new DataBindingContext();
Button button1 = new Button(parent, SWT.PUSH);
Button button2 = new Button(parent, SWT.PUSH);
ISWTObservableValue observeEnabledButton1 = SWTObservables.observeEnabled(button1);
ISWTObservableValue observeEnabledButton2 = SWTObservables.observeEnabled(button2);
UpdateValueStrategy updateValueStrategy = new UpdateValueStrategy();
updateValueStrategy.setConverter(new IConverter() {
@Override
public Object getToType() {
return Boolean.TYPE;
}
@Override
public Object getFromType() {
return Boolean.TYPE;
}
@Override
public Object convert(Object fromObject) {
if(fromObject instanceof Boolean){
// return inverse to disable the other button
return ((Boolean) fromObject).booleanValue() ? Boolean.FALSE : Boolean.TRUE;
}
return Boolean.FALSE;
}
});
dbc.bindValue(observeEnabledButton1, observeEnabledButton2, updateValueStrategy, updateValueStrategy);
With that solution the enabled state of button1 is always the oppisite of the enabled state of button2 and the other way round.
So with that binding you only need to disable the button you just clicked and the other button will be enabled automatically by the binding.
@see JFace Databinding
Hope that helps and it is what you have asked for.
Best regards,
Simon
|
|
|
|
|
Re: How enable and disable buttons using event in SWT. [message #1006653 is a reply to message #1006586] |
Fri, 01 February 2013 10:34 |
|
Hi Simon,
Sorry for asking you again.
I tried your code, at starting time it works means button1 is disable and button2 is enable. But if i click button2 and its listener am disabling it but other button is still in disable mode . I'm putting my code:
Button button1 = new Button(parent, SWT.NONE);
button1.setText("hello");
final Button button2 = new Button(parent, SWT.NONE);
button2.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
button2.setEnabled(false);
}
});
button2.setText("world");
ISWTObservableValue observeEnabledButton1 = SWTObservables.observeEnabled(button1);
ISWTObservableValue observeEnabledButton2 = SWTObservables.observeEnabled(button2);
UpdateValueStrategy updateValueStrategy = new UpdateValueStrategy();
updateValueStrategy.setConverter(new IConverter() {
@Override
public Object getToType() {
return Boolean.TYPE;
}
@Override
public Object getFromType() {
return Boolean.TYPE;
}
@Override
public Object convert(Object fromObject) {
if (fromObject instanceof Boolean) {
// return inverse to disable the other button
return ((Boolean) fromObject).booleanValue() ? Boolean.FALSE : Boolean.TRUE;
}
return Boolean.FALSE;
}
});
dbc.bindValue(observeEnabledButton1, observeEnabledButton2, updateValueStrategy, updateValueStrategy);
[Updated on: Fri, 01 February 2013 10:34] Report message to a moderator
|
|
|
Re: How enable and disable buttons using event in SWT. [message #1007235 is a reply to message #1006653] |
Tue, 05 February 2013 08:16 |
|
Hi sumit,
Quote:Sorry for asking you again.
It is my fault.
Actually there is no enabled listener available for controls, so that you need to bind the selection of the button to the other´s enabled observable.
So this code should do the job
Button button1 = new Button(parent, SWT.NONE);
button1.setText("hello");
Button button2 = new Button(parent, SWT.NONE);
button2.setText("world");
ISWTObservableValue observeSelectionButton1 = SWTObservables.observeSelection(button1);
ISWTObservableValue observeEnabledButton2 = SWTObservables.observeEnabled(button2);
dbc.bindValue(observeSelectionButton1, observeEnabledButton2);
Best regards,
Simon
|
|
|
Powered by
FUDForum. Page generated in 0.05724 seconds