ProvingBard
ProvingBard

Reputation: 383

How to get the correct text value from a button javafx

I have been trying to use the name of a button in a conditional , checking if the string generated from .getText()is or is not equal to "New Project:" in the case below, but every time i try to run this code it always gives me a result of 'not equal' to "New Project:" which is its starting value, and runs the first block. Even at #1 where it prints of the value of this string i have copied and pasted the value printed out from this but it does not work. I have also proven by replacing the "New Project" with newProjectButton.getText() but this then makes the statement always use the else block. I am 99% sure that i have not made a spelling mistake on the "New Project:" bit and i am unsure of what i have done wrong here so it would be a great help if someone knew what is wrong

 if(event.getTarget() == newProjectButton1){
        if(newProjectButton.getText() != "New Project:"){
            System.out.println(newProjectButton.getText());//#1
            mainSplitPane.getItems().set(1, projectLayout);
            newProjectButton.setText(project.getProjectName());
        }else{
            projectLayout = project.initLayouts(loader, projectLayout, this);
            mainSplitPane.getItems().set(1, projectLayout);
        }

Cheers

Upvotes: 2

Views: 8136

Answers (1)

Akash Rajbanshi
Akash Rajbanshi

Reputation: 1593

!newProjectButton.getText().equals("New Project:") should do.

Here you are comparing reference equality but what you are trying to do is value equality.

So, equals() will check the value while == checks the references.

Upvotes: 3

Related Questions