java - SWT Resize Error -
i've been using swt , have found issue.
when selecting , deselecting check boxes , maximizing window cause icon of first button set though null. layout of text on buttons can end incorrect.
to recreate error:
- select checkbox 1
- deselect checkbox 1
- select checkbox 3
- maximize window
the icon checkbox 1 should null (empty) instead has cross icon.
public static void main(string[] args) { final display d = new display(); shell s = new shell(d); s.setlayout(new gridlayout(2, false)); s.setsize(500, 500); new label(s, swt.none).settext("c"); final button c = new button(s, swt.check); new label(s, swt.none).settext("l1"); final button b = new button(s, swt.push | swt.flat); b.setlayoutdata(new griddata(swt.fill, swt.center, true, false)); b.settext("button 1"); b.setenabled(false); new label(s, swt.none).settext("c2"); final button c2 = new button(s, swt.check); new label(s, swt.none).settext("l2"); final button b2 = new button(s, swt.push | swt.flat); b2.setlayoutdata(new griddata(swt.fill, swt.center, true, false)); b2.setimage(null); b2.settext("button 2"); b2.setenabled(false); new label(s, swt.none).settext("c3"); final button c3 = new button(s, swt.check); new label(s, swt.none).settext("l3"); final button b3 = new button(s, swt.push | swt.flat); b3.setlayoutdata(new griddata(swt.fill, swt.center, true, false)); b3.settext("button 3"); b3.setenabled(false); c.addselectionlistener(new selectionadapter() { @override public void widgetselected(selectionevent e) { if (!b.isenabled()) { b.setimage(d.getsystemimage(swt.icon_error)); b.setenabled(true); } else { b.setimage(null); b.setenabled(false); } } }); c2.addselectionlistener(new selectionadapter() { @override public void widgetselected(selectionevent e) { if (!b2.isenabled()) { b2.setimage(d.getsystemimage(swt.icon_error)); b2.setenabled(true); } else { b2.setimage(null); b2.setenabled(false); } } }); c3.addselectionlistener(new selectionadapter() { @override public void widgetselected(selectionevent e) { if (!b3.isenabled()) { b3.setimage(d.getsystemimage(swt.icon_error)); b3.setenabled(true); } else { b3.setimage(null); b3.setenabled(false); } } }); s.open(); while (!s.isdisposed()) { if (!d.readanddispatch()) d.sleep(); } d.dispose(); }
this isn't ideal means, if need workaround, 1 thing try wrapping button
, using stacklayout
flip between enabled button
, disabled button
. way you're not adding/removing image
. fixes layout issues because actual button
instances not changing state.
for example:
public static class mybutton { private final button buttonenabled; private final button buttondisabled; private final composite stackcomposite; private final stacklayout stacklayout; public mybutton(final composite parent, final string text) { stackcomposite = new composite(parent, swt.none); stacklayout = new stacklayout(); stackcomposite.setlayout(stacklayout); stackcomposite.setlayoutdata(new griddata(swt.fill, swt.fill, true, false)); buttonenabled = createbutton(stackcomposite, text, true); buttondisabled = createbutton(stackcomposite, text, false); stacklayout.topcontrol = buttonenabled; } public void setenabled(final boolean enabled) { stacklayout.topcontrol = enabled ? buttonenabled : buttondisabled; stackcomposite.layout(); } public boolean isenabled() { return stacklayout.topcontrol.equals(buttonenabled) ? true : false; } private static button createbutton(final composite parent, final string text, final boolean enabled) { final button button = new button(parent, swt.push | swt.flat); button.setlayoutdata(new griddata(swt.fill, swt.center, true, false)); button.settext(text); button.setenabled(enabled); if (enabled) { button.setimage(parent.getdisplay().getsystemimage(swt.icon_error)); } return button; } }
again, isn't ideal, can add methods (addlistener(...)
, etc.) mybutton
class call methods on enabled button
, implementation detail.
changes main
method:
public static void main(final string[] args) { // ... new label(s, swt.none).settext("c"); final button c = new button(s, swt.check); new label(s, swt.none).settext("l1"); final mybutton b = new mybutton(s, "button 1"); // ... c.addselectionlistener(new selectionadapter() { @override public void widgetselected(final selectionevent e) { b.setenabled(!b.isenabled()); } }); // ... }
result: (note changed first button)
Comments
Post a Comment