2

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

ProvingBard
  • 333
  • 4
  • 12
  • 1
    This will help you understand the difference between == and equals better: [== vs equals](http://stackoverflow.com/questions/767372/java-string-equals-versus) – NDY May 18 '15 at 09:20

1 Answers1

3

!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.

Akash Rajbanshi
  • 1,553
  • 11
  • 23