12 Replies - 713 Views - Last Post: 26 May 2019 - 06:53 AM Rate Topic: -----

#1 aldus17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-September 18

ChangeListener

Posted 25 May 2019 - 06:52 AM

Hello, I am working on a method that is planned to take a ToggleGroups, listen for a change in that toggleGroup and aftwards get the radioButton.getText() and return that. The problem is that if I use the method with two togglegroups or more it only gives the selected value of the first togglegroup. My question is it possible to do this with only one changeListener?:

Here is my code


The method:
private String radionButtonchangeListener(ToggleGroup tg) {
        //outputText = "";
        tg.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {

                RadioButton rb = (RadioButton) observable.getValue().getToggleGroup().getSelectedToggle(); // Cast object to radio button
                outputText = rb.getText();

            }
        });

        return outputText;
    }


Where I test it TG stands for ToggleGroup and socialPedagogicalHelpTG and carriageTG and instantiated in initilize method of the controller, and I am putting it in as a String value into the contentsMap:
contentsMap.put("socialpedogogicalhelp", radionButtonchangeListener(socialPedagogicalHelpTG));
            contentsMap.put("carriage", radionButtonchangeListener(carriageTG));

System.out.println("Social: " + contentsMap.get("socialpedogogicalhelp"));
            System.out.println("Carriage: " + contentsMap.get("carriage"));



Result:
Posted Image

Social: gets what is expected but
Carriage: is the same value as socialped.... and that should be another value with paragraf 85.

Is This A Good Question/Topic? 0
  • +

Replies To: ChangeListener

#2 aldus17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-September 18

Re: ChangeListener

Posted 25 May 2019 - 06:57 AM

This where I want the values from, every titled pane has radiobuttons under it and each are added to its own ToggleGroup:

Posted Image
Was This Post Helpful? 0
  • +
  • -

#3 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,836
  • Joined: 25-December 13

Re: ChangeListener

Posted 25 May 2019 - 07:12 AM

What value is expected to be returned by the statement at line 13?
line 8 won't be executed until after the user's action.
If you want something done with the value set by the user, the listener will have to take care of it.

This post has been edited by NormR: 25 May 2019 - 07:15 AM

Was This Post Helpful? 1
  • +
  • -

#4 aldus17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-September 18

Re: ChangeListener

Posted 25 May 2019 - 07:57 AM

Line 13 returns the value of the radioButton getText(), from the selected RadionButton from the listener.
The problem is that I wish to get more values from this single method, instead of writing the same listener and changeListener for every 10 togglegroup under one action. So I tried to use that method to simplify the code by reducing several repeated code blocks and have one method call that changeListener method. But the problem is if I select one Radiobutton under Socialpædagogiskstøtte, and select one from befordring, then the listener only picks one of them and does not return one value from one ToggleGroup and one value from the second ToggleGroup.

I hope this explained it a bit? :)

contentsMap.put("socialpedogogicalhelp", radionButtonchangeListener(socialPedagogicalHelpTG));
2
            contentsMap.put("carriage", radionButtonchangeListener(carriageTG));
3
 
4
System.out.println("Social: " + contentsMap.get("socialpedogogicalhelp"));
5
            System.out.println("Carriage: " + contentsMap.get("carriage"));


Was This Post Helpful? 0
  • +
  • -

#5 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,836
  • Joined: 25-December 13

Re: ChangeListener

Posted 25 May 2019 - 08:07 AM

Quote

if I select one Radiobutton under Socialpædagogiskstøtte, and select one from befordring

You should get one value for each selection that is made. If each selection is saved in the same variable, the variable will hold the value from the last selection.

The radionButtonchangeListener method will return the value that is in outputText when it is called, not after the user's action changes the value.

Try adding some print statements to the different methods that print out the values of variables as the code is executed so you can see what the values in the variables are when the code is executed.
Was This Post Helpful? 1
  • +
  • -

#6 aldus17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-September 18

Re: ChangeListener

Posted 25 May 2019 - 10:21 AM

I tried something different, maybe bad coding style, but, I added 10 implementations of that method in one actionHandler. This works for almost every section, but it returns null in some cases which I dont understand. Is it becuase it is running on different threads in regards of which listener runs at the current time?

carriageTG.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {

                RadioButton rb = (RadioButton) newValue.getToggleGroup().getSelectedToggle();
                carriageText = rb.getText();

            }
        });
        treatmentTG.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {

                RadioButton rb = (RadioButton) newValue.getToggleGroup().getSelectedToggle();
                treatmentText = rb.getText();

            }
        });
        controlTG.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {

                RadioButton rb = (RadioButton) newValue.getToggleGroup().getSelectedToggle();
                controlText = rb.getText();

            }
        });
        stayTG.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {

                RadioButton rb = (RadioButton) newValue.getToggleGroup().getSelectedToggle();
                stayText = rb.getText();

            }
        });
        socialPedagogicalHelpTG.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {

                RadioButton rb = (RadioButton) newValue.getToggleGroup().getSelectedToggle();
                socialPedagogicalHelpText = rb.getText();

            }
        });
        outpatientOffersTG.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {

                RadioButton rb = (RadioButton) newValue.getToggleGroup().getSelectedToggle();
                outpatientoffersText = rb.getText();

            }
        });
        personalHelpSchemeTG.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {

                RadioButton rb = (RadioButton) newValue.getToggleGroup().getSelectedToggle();
                personalhelpschemeText = rb.getText();

            }
        });
        housingOfferTG.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
                RadioButton rb = (RadioButton) newValue.getToggleGroup().getSelectedToggle();
                housingofferforadultsText = rb.getText();

            }
        });
        extendedhousingofferTG.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
                RadioButton rb = (RadioButton) newValue.getToggleGroup().getSelectedToggle();
                extendedhousingofferforadultsText = rb.getText();

            }
        });
        otherOffersAndParagraphsTG.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
                RadioButton rb = (RadioButton) newValue.getToggleGroup().getSelectedToggle();
                otheroffersandparagrafsText = rb.getText();

            }
        });

    }
    

   


This post has been edited by aldus17: 25 May 2019 - 10:22 AM

Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,836
  • Joined: 25-December 13

Re: ChangeListener

Posted 25 May 2019 - 10:51 AM

Quote

it returns null in some cases

Which cases are null?

Quote

I added 10 implementations

Instead of having 10 blocks of almost identical code, you could use a class that takes the target field as an argument. Doesn't work!!!!

Quote

which listener runs at the current time?

A listener would be executed in response to a user's action. You could see that if there were print statements that printed a message when a listener was executed.

This post has been edited by NormR: 25 May 2019 - 04:20 PM

Was This Post Helpful? 1
  • +
  • -

#8 aldus17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-September 18

Re: ChangeListener

Posted 25 May 2019 - 12:40 PM

The values that return null are Treatment and carriage. But sometimes it gives them values, so that is what is strange. But the solution must be another class instead of a single method, for such an method. I will try to implement with a class. And as I said, I know it was a bad solution to have repetetive code, that is why I wanted to implement af method to take care of that problem, but I will try with a class and see if that solves it. Thank you for your help. I will post the solution if it is gonna work, else if it is okay, I will post any errors that I would get :).
Was This Post Helpful? 0
  • +
  • -

#9 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,836
  • Joined: 25-December 13

Re: ChangeListener

Posted 25 May 2019 - 12:51 PM

Quote

The values that return null are Treatment and carriage. But sometimes it gives them values

I have no idea why that would happen sometimes for just those 2 values.

It is hard to see what is happening with only small parts of the code.
Was This Post Helpful? 1
  • +
  • -

#10 aldus17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-September 18

Re: ChangeListener

Posted 25 May 2019 - 03:47 PM

Okay I have followed your advice and created a class, under that class I can getInstance of it and I can set the Togglegroup to the private attribute under that class and the method uses that private attribute to listen to any change. The result is now that I get the result for only one of the Selected toggleGroups here is the code and output + GUI:

This is the ActionHandler for the all RadionButtons that are under benefitsParagraphs titled pane under the section that is called Ydelser og paragraffer (benefitsParagraphs). This is only to test if it works for more Toggle groups.

private String carriageText, treatmentText,  controlText;
@FXML
    private void benefitsParagraphs(ActionEvent event) {
        RadioButtonchangeListener rbcl1 = RadioButtonchangeListener.getInstance();
        rbcl1.setToggleGroup(carriageTG);
        carriageText = rbcl1.radionButtonchangeListener();
        RadioButtonchangeListener rbcl2 = RadioButtonchangeListener.getInstance();
        rbcl2.setToggleGroup(treatmentTG);
        treatmentText = rbcl2.radionButtonchangeListener();
        RadioButtonchangeListener rbcl3 = RadioButtonchangeListener.getInstance();
        rbcl3.setToggleGroup(controlTG);
        controlText = rbcl3.radionButtonchangeListener();
}


The class I have created:

package mmmi.UI.Main.CreateCase;

import javafx.beans.value.ObservableValue;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;

public class RadioButtonchangeListener {

    private ToggleGroup toggleGroup;
    private String outputText;
    private static RadioButtonchangeListener rbcl;

    public RadioButtonchangeListener() {

    }

    public static RadioButtonchangeListener getInstance() {
        if (rbcl == null) {
            rbcl = new RadioButtonchangeListener();
        }
        return rbcl;
    }

    public ToggleGroup getToggleGroup() {
        return toggleGroup;
    }

    public void setToggleGroup(ToggleGroup toggleGroup) {
        this.toggleGroup = toggleGroup;
    }

    public String getOutputText() {
        return outputText;
    }

    public void setOutputText(String outputText) {
        this.outputText = outputText;
    }

    public String radionButtonchangeListener() {

        this.toggleGroup.selectedToggleProperty().addListener((ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) -> {
            RadioButton rb = (RadioButton) newValue.getToggleGroup().getSelectedToggle(); // Cast object to radio button
            outputText = rb.getText();
        });

        return outputText;
    }

}


The saveActionHandler handles input from the different parts of gui.

 @FXML
    private void saveBtnOnAction(ActionEvent event) {
        if (event.getSource() == fxBT_save) {
            System.out.println(carriageText + treatmentText + controlText); // TEST OUTPUT


            if (!fxTF_firstnameRegarding.getText().isEmpty()) {
                cRegardingMap.put("firstName", fxTF_firstnameRegarding.getText());
                cRegardingMap.put("lastName", fxTF_lastnameRegarding.getText());
                cRegardingMap.put("streetName", fxTF_streetNameRegarding.getText());
                cRegardingMap.put("cityName", fxTF_citynameRegarding.getText());
                cRegardingMap.put("zipCode", String.valueOf(fxTF_zipcodeRegarding.getText()));
                cRegardingMap.put("houseNo", String.valueOf(fxTF_zipcodeRegarding.getText()));
                cRegardingMap.put("regardingCitizen", "true");
                cRegardingMap.put("requestingCitizen", "false");
                cRegardingMap.put("cpr", String.valueOf(fxTF_cprNoRegarding.getText()));
                String[] regfd = fxTF_floorAndFloorDirectionRegarding.getText().split(", ", 2);
                if (regfd.length != 2) {
                    cRegardingMap.put("floor", "");
                    cRegardingMap.put("floorDirection", "");
                } else {
                    String regfloor = regfd[0];
                    String regfloorDir = regfd[1];
                    cRegardingMap.put("floor", regfloor);
                    cRegardingMap.put("floorDirection", regfloorDir);
                }
                System.out.println("her er jeg");
            }
            if (!fxTF_firstnameRequesting.getText().isEmpty()) {
                cRequestingMap.put("firstName", fxTF_firstnameRegarding.getText());
                cRequestingMap.put("lastName", fxTF_lastnameRequesting.getText());
                cRequestingMap.put("streetName", fxTF_streetNameRequesting.getText());
                cRequestingMap.put("cityName", fxTF_citynameRegarding.getText());
                cRequestingMap.put("zipCode", String.valueOf(fxTF_zipcodeRequesting.getText()));
                cRequestingMap.put("houseNo", String.valueOf(fxTF_zipcodeRequesting.getText()));
                cRequestingMap.put("regardingCitizen", "false");
                cRequestingMap.put("requestingCitizen", "true");
                cRequestingMap.put("cpr", String.valueOf(fxTF_cprNoRequesting.getText()));
                String[] reqfd = fxTF_floorAndFloorDirectionRequesting.getText().split(", ", 2);
                if (reqfd.length != 2) {
                    cRegardingMap.put("floor", "");
                    cRegardingMap.put("floorDirection", "");
                } else {
                    String reqfloor = reqfd[0];
                    String reqfloorDir = reqfd[1];
                    cRequestingMap.put("floor", reqfloor);
                    cRequestingMap.put("floorDirection", reqfloorDir);
                }
            }

            String text = "";
            contentsMap.put("caseID", "-1");
            contentsMap.put("regardinginquiry", fxTA_RegardingInquiry.getText());
            contentsMap.put("rightsanddutiestext", !fxTA_rightsAndDuties.getText().isEmpty() ? fxTA_rightsAndDuties.getText() : "");
            contentsMap.put("rightsandduties", agreeToElectronicRegistrationTG.getSelectedToggle() == fxRB_agreeToElectronicRegistrationYes1
                    ? "yes"
                    : (agreeToElectronicRegistrationTG.getSelectedToggle() == fxRB_agreeToElectronicRegistrationNo1)
                    ? "no" : "");
            contentsMap.put("agreementswithcitizentext", !fxTA_agreementsWithCitzen.getText().isEmpty()
                    ? fxTA_agreementsWithCitzen.getText() : "");

            String getTextCB = "";
            getTextCB = fxCB_getInfoAKasse.isSelected() ? fxCB_getInfoAKasse.getText() : "";
            getTextCB += fxCB_getInfoDoctor.isSelected() ? fxCB_getInfoDoctor.getText() : "";
            getTextCB += fxCB_getInfoHospital.isSelected() ? fxCB_getInfoHospital.getText() : "";
            getTextCB += fxCB_getInfoEmployer.isSelected() ? fxCB_getInfoEmployer.getText() : "";
            getTextCB += fxCB_getInfoOffer.isSelected() ? fxCB_getInfoOffer.getText() : "";
            getTextCB += fxCB_getInfoOtherAdministrations.isSelected() ? fxCB_getInfoOtherAdministrations.getText() : "";
            getTextCB += fxCB_getInfoPreviousCommune.isSelected() ? fxCB_getInfoPreviousCommune.getText() : "";
            getTextCB += fxCB_getInfoSpecialDoctor.isSelected() ? fxCB_getInfoSpecialDoctor.getText() : "";

            contentsMap.put("getinfo", getTextCB);
            contentsMap.put("getinfotext", !fxTA_obtainOfInformation.getText().isEmpty() ? fxTA_obtainOfInformation.getText() : "");
            contentsMap.put("citizenspecialcircumstancestext", !fxTA_citizenSpecialCircumstances.getText().isEmpty() ? fxTA_citizenSpecialCircumstances.getText() : "");

            contentsMap.put("guardianship", guadianText != null ? guadianText : "");
            contentsMap.put("guardianshiptext", !fxTA_guadianshipText.getText().isEmpty() ? fxTA_guadianshipText.getText() : "");
            contentsMap.put("representation", representationText != null ? representationText : "");
            contentsMap.put("representationtext", !fxTA_representationText.getText().isEmpty() ? fxTA_representationText.getText() : "");

            contentsMap.put("consent", whichConsentTG.getSelectedToggle() == fxRB_consentOral
                    ? fxRB_consentOral.getText()
                    : (agreeToElectronicRegistrationTG.getSelectedToggle() == fxRB_consentWritten)
                    ? fxRB_consentWritten.getText() : "");

            contentsMap.put("agreementswithcitizentext", !fxTA_agreementsWithCitzen.getText().isEmpty()
                    ? fxTA_agreementsWithCitzen.getText() : "");
            contentsMap.put("otheractingcommune", communePayingOrActingTG.getSelectedToggle() == fxRB_OtherActingCommune
                    ? fxRB_OtherActingCommune.getText()
                    : (agreeToElectronicRegistrationTG.getSelectedToggle() == fxRB_otherPayingCommune)
                    ? fxRB_otherPayingCommune.getText() : "");
            contentsMap.put("otheractingcommunetext", fxTA_CommunePayingOrActing.getText().isEmpty() ? fxTA_CommunePayingOrActing.getText() : "");

            contentsMap.put("socialpedogogicalhelp", socialPedagogicalHelpText != null ? socialPedagogicalHelpText : "");
            contentsMap.put("carriage", carriageText != null ? carriageText : "");
            contentsMap.put("personalhelpscheme", personalhelpschemeText != null ? personalhelpschemeText : "");
            contentsMap.put("otheroffersandparagrafs", otheroffersandparagrafsText != null ? otheroffersandparagrafsText : "");
            contentsMap.put("stay", stayText != null ? stayText : "");
            contentsMap.put("treatment", treatmentText != null ? treatmentText : "");
            contentsMap.put("control", controlText != null ? controlText : "");

            contentsMap.put("ambulanttreatment", fxRB_ambulantTreatment.isSelected() ? fxRB_ambulantTreatment.getText() : "");
            contentsMap.put("outpatientoffers", outpatientoffersText != null ? outpatientoffersText : "");
            contentsMap.put("housingofferforadults", housingofferforadultsText != null ? housingofferforadultsText : "");
            contentsMap.put("extendedhousingofferforadults", extendedhousingofferforadultsText != null ? extendedhousingofferforadultsText : "");

            System.out.println("Social: " + contentsMap.get("socialpedogogicalhelp"));
            System.out.println("Carriage: " + contentsMap.get("carriage"));
            System.out.println(contentsMap);
            nf.addContents(contentsMap);
            nf.addmap(cRegardingMap, "cRegarding");
            nf.addmap(cRequestingMap, "cRequesting");

        }
    }


Where I toggle in gui and the result of the system.print.out under saveButtonActionHandler:

Posted Image

The systemPrintOut:
@FXML
    private void saveBtnOnAction(ActionEvent event) {
        if (event.getSource() == fxBT_save) {
            System.out.println(carriageText + treatmentText + controlText); // TEST OUTPUT


The first output in CLI is the sout from above test:
Posted Image

Expected result is: Befordring til og fra et tilbud (Lov om ungdomsuddannelse for unge med særligeTerapi (§ 102)Fastholdelse (§ 128)

This post has been edited by aldus17: 25 May 2019 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#11 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,836
  • Joined: 25-December 13

Re: ChangeListener

Posted 25 May 2019 - 04:29 PM

I have just seen that the code is written for JavaFX. I don't know JavaFX. Sorry to have misled you.
My earlier description of a class to replace the 10 repeated listeners also won't work. To return a value, one way would need to use an array to receive the value. The index to the array's slot would be passed to the class's constructor to be used by the listener method.

This post has been edited by NormR: 25 May 2019 - 05:39 PM

Was This Post Helpful? 1
  • +
  • -

#12 aldus17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-September 18

Re: ChangeListener

Posted 26 May 2019 - 05:49 AM

Do you know anyone here on the forum that could give a hint or show an example of how it could be done for several ToggleGroups? :)/>

This post has been edited by aldus17: 26 May 2019 - 05:49 AM

Was This Post Helpful? 0
  • +
  • -

#13 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,836
  • Joined: 25-December 13

Re: ChangeListener

Posted 26 May 2019 - 06:53 AM

Can you make and post here a small, complete program that compiles, executes and shows the problem?
Probably no more than two groups with 2-3 toggles per group.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1