Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » How enable and disable buttons using event in SWT.(How enable and disable buttons using event in SWT.)
How enable and disable buttons using event in SWT. [message #1006467] Thu, 31 January 2013 12:16 Go to next message
sumit singh is currently offline sumit singhFriend
Messages: 133
Registered: October 2012
Location: Bangalore
Senior Member

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 Go to previous messageGo to next message
Simon Scholz is currently offline Simon ScholzFriend
Messages: 73
Registered: April 2012
Location: Germany
Member
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 #1006480 is a reply to message #1006475] Thu, 31 January 2013 13:07 Go to previous messageGo to next message
sumit singh is currently offline sumit singhFriend
Messages: 133
Registered: October 2012
Location: Bangalore
Senior Member

Thnaks Simon,

I'll try it.
Re: How enable and disable buttons using event in SWT. [message #1006586 is a reply to message #1006467] Thu, 31 January 2013 23:43 Go to previous messageGo to next message
Simon Scholz is currently offline Simon ScholzFriend
Messages: 73
Registered: April 2012
Location: Germany
Member
Hey sumit,

you´re welcome.

If you have any further questions do not hesitate to ask Wink

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 Go to previous messageGo to next message
sumit singh is currently offline sumit singhFriend
Messages: 133
Registered: October 2012
Location: Bangalore
Senior Member

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 Go to previous message
Simon Scholz is currently offline Simon ScholzFriend
Messages: 73
Registered: April 2012
Location: Germany
Member
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 Wink
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
Previous Topic:E4 RCP Part createContents gets executed multiple times
Next Topic:Help Home depending on user language
Goto Forum:
  


Current Time: Wed Nov 26 09:27:37 GMT 2014

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

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