This button mainly combines SWT. Toggle and SWT. Radio to achieve the desired result!
Code:
Import Org. eclipse. SWT. SWT; <br/> Import Org. eclipse. SWT. events. focusevent; <br/> Import Org. eclipse. SWT. events. focuslistener; <br/> Import Org. eclipse. SWT. events. mouseevent; <br/> Import Org. eclipse. SWT. events. mouselistener; <br/> Import Org. eclipse. SWT. graphics. color; <br/> Import Org. eclipse. SWT. graphics. image; <br/> Import Org. eclipse. SWT. widgets. button; <br/> Import Org. eclipse. SWT. widgets. composite; <Br/> Import Org. eclipse. SWT. widgets. control; </P> <p> public class imagebutton extends button implements focuslistener, mouselistener {<br/> private Boolean isselected = false; <br/> private Boolean isusecolorflat = false; </P> <p> Public imagebutton (composite parent, image) {<br/> This (parent, image, false ); <br/>}</P> <p> Public imagebutton (composite parent, image, Boolean flat) {<br/> super (Parent, SWT. toggle); <br/> setimage (image); <br/> addfocuslistener (this); <br/> addmouselistener (this); <br/> isusecolorflat = flat; <br/>}</P> <p> Public void setselection (Boolean selected) {<br/> // todo auto-generated method stub <br/> super. setselection (selected); <br/> If (selected) <br/> clearothersselection (selected ); <br/>}</P> <p>/** <br/> * create data: 2009-3-20 </br> <br/> * This Is method is used to clear the others, when you selected current <br/> * <code> imagebutton <code> <br/> * @ author xiangjie <br/> * @ Param parent <br/> * <br/> */<br/> private void clearothersselection (Boolean selected) {<br/> composite parent = getparent (); <br/> If (parent = NULL) <br/> return; <br/> Control [] controls = parent. getchildren (); <br/> for (INT I = 0; I <controls. length; I ++) {< Br/> If (controls [I] instanceof imagebutton) {<br/> If (! Controls [I]. equals (this) {<br/> (imagebutton) controls [I]). setselection (false); <br/>}</P> <p> Public void focusgained (focusevent E) {<br/> // todo auto-generated method stub <br/> // setselection (true); <br/> If (! Isselected) {<br/> isselected = true; <br/> setselection (true); <br/>}< br/> setselectioncolor (); <br/>}</P> <p> Public void focuslost (focusevent E) {<br/> // todo auto-generated method stub <br/> // setselection (false); <br/> isselected = false; <br/> setselectioncolor (); <br/> // setselection (false); <br/>}</P> <p> Public void mousedoubleclick (mouseevent E) {<br/> // todo auto-generated method stub </P> <p >}</P> <p> Public void mousedown (mouseevent E) {<br/> // todo auto-generated method stub <br/> If (isselected) {<br/> setselection (true); <br/> setselectioncolor (); <br/> return; <br/>}< br/> setselectioncolor (); <br/>}</P> <p> Public void mouseup (mouseevent E) {<br/> // todo auto-generated method stub <br/> If (isselected) {<br/> setselection (true); <br/> setselectioncolor (); <br/> return; <br/>}< br/> setselectioncolor (); <br/>}</P> <p>/** <br/> * create data: 2009-3-13. <br> <br/> * when it's selected, the color of it will be set red, else Is null <br/> * @ author xiangjie <br/> */<br/> protected void setselectioncolor () {<br/> If (isusecolorflat) {<br/> If (isselected) <br/> This. setbackground (new color (null, 255,128, 0); <br/> // [R = 189, G = 149, B = 66] <br/> else <br/> This. setbackground (null ); <br/>}</P> <p>/** <br/> * @ return the isusecolorflat <br/> */<br/> Public boolean isusecolorflat () {<br/> return isusecolorflat; <br/>}</P> <p>/** <br/> * @ Param isusecolorflat <br/> * The isusecolorflat to set <br/> */<br /> Public void setusecolorflat (Boolean isusecolorflat) {<br/> This. isusecolorflat = isusecolorflat; <br/>}</P> <p>/** <br/> * You must inherent this method, please see <a herf = <br/> * "http://www.eclipselight.org/rcp/83/"> inherit the cause of widget error </a> <br/> * @ author Jie. xiang <br/> */<br/> protected void checksubclass () {<br/>}</P> <p>}
Demo:
This article is the original article of eclipselight.org (eclipse light). For more information, see the source.
Fixed Link: http://www.eclipselight.org/swt-jface/329/