niko
niko

Reputation: 1

How to reset everything on label in onclick method

I am printing multiple labels and on click I am changing the clicked label color. I want to reset the clicked label color when I click on another label.

final Label functionLabel = new Label(FDTO.getFunctionName());
functionLabel.addClickHandler(new ClickHandler() {
    @Override
    public void onClick(ClickEvent event) {
        functionLabel.getElement().getStyle().setColor("red");
        // checkChild(FDTO.getFunctionCode(), functions, qaDTO, val);
    }
});

childPanel.add(functionLabel);

Upvotes: 0

Views: 72

Answers (1)

walen
walen

Reputation: 7273

Unlike RadioButtons which can be grouped together, Labels are stand-alone elements, and there's no out-of-the-box way of doing what you want.

The most straightforward way to do so would be to manually change the color of every other label from within this label's onClick method, either one by one or iterating through some list/array where you keep them all.
Something along the lines of:

List<Label> myLabels = Arrays.asList(functionLabel, errorLabel, someOtherLabel);
//...
functionLabel.addClickHandler(new ClickHandler() {
    @Override
    public void onClick(ClickEvent event) {
        for (Label l : myLabels) {
            l.getElement().getStyle().setColor("black");
        }
        functionLabel.getElement().getStyle().setColor("red");
        // checkChild(FDTO.getFunctionCode(), functions, qaDTO, val);
    }
});

Upvotes: 1

Related Questions