1. #1
    Ext GWT Premium Member
    Join Date
    Jun 2010
    Location
    Kyrksæterøra, Norway
    Posts
    63
    Vote Rating
    2
    stigrv is on a distinguished road

      0  

    Default ToggleGroup with ToggleButton widgets

    ToggleGroup with ToggleButton widgets


    When I add several ToggleButtons to a ToggleGroup there will be only one selected ToggleButton at any time. But when I click a selected button this button will be deselected, and no ToggleButtons appear to be selected. Are there a simple way to prevent this, and always have one button to appear selected?

  2. #2
    Sencha - GXT Dev Team
    Join Date
    Jan 2012
    Location
    Arlington, WA
    Posts
    364
    Vote Rating
    14
    branflake2267 will become famous soon enough

      0  

    Default


    There isn't a built in function to control that behavior with the toggle group. But adding a select handler to the button could be one way to control the behavior you'd like.

    Code:
       t1.addSelectHandler(new SelectHandler() {
          @Override
          public void onSelect(SelectEvent event) {
            t1.setValue(true);
          }
        });
    
    
        t2.addSelectHandler(new SelectHandler() {
          @Override
          public void onSelect(SelectEvent event) {
            t2.setValue(true);
          }
        });
    Code:
    private Widget createToggleGroupButtons() {
        final ToggleButton t1 = new ToggleButton("1");
        final ToggleButton t2 = new ToggleButton("2");
    
    
        FlowLayoutContainer flc = new FlowLayoutContainer();
        flc.add(t1);
        flc.add(t2);
    
    
        final ToggleGroup toggleGroup = new ToggleGroup();
        toggleGroup.add(t1);
        toggleGroup.add(t2);
    
    
        t1.addSelectHandler(new SelectHandler() {
          @Override
          public void onSelect(SelectEvent event) {
            t1.setValue(true);
          }
        });
    
    
        t2.addSelectHandler(new SelectHandler() {
          @Override
          public void onSelect(SelectEvent event) {
            t2.setValue(true);
          }
        });
    
    
        final TextButton tbGetValue = new TextButton("Get");
        tbGetValue.addSelectHandler(new SelectEvent.SelectHandler() {
          @Override
          public void onSelect(SelectEvent event) {
            String toggleSelected = "";
            HasValue<Boolean> value = toggleGroup.getValue();
            if (value != null && value.equals(t1)) {
              toggleSelected = "t1";
            } else if (value != null && value.equals(t2)) {
              toggleSelected = "r2";
            }
    
    
            String s = "t1=" + t1.getValue() + " r2=" + t2.getValue() + " toggleValueSelected=" + toggleSelected;
            html3.setHTML(s);
          }
        });
    
    
        final TextButton tbReset = new TextButton("Reset");
        tbReset.addSelectHandler(new SelectEvent.SelectHandler() {
          @Override
          public void onSelect(SelectEvent event) {
            toggleGroup.reset();
          }
        });
    
    
        html3 = new HTML("&nbsp;");
    
    
        flc.add(tbGetValue);
        flc.add(tbReset);
        flc.add(html3);
    
    
        ContentPanel cp = new ContentPanel();
        cp.setHeadingHtml("With toggle Group");
        cp.add(flc);
    
    
        return cp;
      }

  3. #3
    Ext GWT Premium Member
    Join Date
    Jun 2010
    Location
    Kyrksæterøra, Norway
    Posts
    63
    Vote Rating
    2
    stigrv is on a distinguished road

      1  

    Default


    Thanks for your help. I ended up taking this one step further, and created an extension of the ToggleGroup class:

    Code:
    public class SelectedToggleGroup extends ToggleGroup {
    
    
        protected SelectHandler selectHandler = new SelectHandler() {
            @Override
            @SuppressWarnings("unchecked")
            public void onSelect(final SelectEvent event) {
                ((HasValue<Boolean>) event.getSource()).setValue(true, false);
            }
        };
    
    
        @Override
        public final boolean add(final HasValue<Boolean> toggle) {
            if (this.toggles.containsKey(toggle)) {
                return false;
            } else {
                final GroupingHandlerRegistration handlerRegistration = new GroupingHandlerRegistration();
                handlerRegistration.add(toggle.addValueChangeHandler(this.handler));
    
    
                if (toggle instanceof HasSelectHandlers) {
                    ((HasSelectHandlers) toggle).addSelectHandler(this.selectHandler);
                }
    
    
                return this.toggles.put(toggle, handlerRegistration) == null;
            }
        }
    }

  4. #4
    Sencha - GXT Dev Team
    Join Date
    Jan 2012
    Location
    Arlington, WA
    Posts
    364
    Vote Rating
    14
    branflake2267 will become famous soon enough

      0  

    Default


    Nice job.

Thread Participants: 1