1

I would like to ask is retrieving user profile image from UserClass and display the image on a button possible in JavaFX?

I have tried using setStyle to set the image but when I run the program it shows some error.

Here is my FXML controller file

private Button button1;
UserClass user = new UserClass();
public void initialize(URL location, ResourceBundle resources) 
{
   button.setStyle("-fx-background-image:'"+user.Photopath()+"'");
}

Here is my simplified UserClass class code

public class UserClass
{
   private String photopath;
   public String PhotoPath() 
   {
      return "src/Pictures/womanprofile.png";
   }
}

Here is the error log

WARNING: CSS Error parsing '*{-fx-background image:'src/Pictures/womanprofile.png'}:

15
  • What error are you getting? Commented Oct 24, 2019 at 5:15
  • I think "-fx-background-image:"+user.Photopath()+" instead of "-fx-background-image:"+user.Photopath.+" Commented Oct 24, 2019 at 5:19
  • Please update your post with the actual error message Commented Oct 24, 2019 at 5:27
  • @ShekharRai oops forget to add(), edited the code Commented Oct 24, 2019 at 5:28
  • 1
    @ShekharRai please add that as an answer Commented Oct 24, 2019 at 5:42

2 Answers 2

1

You need to add url in your css.

private Button imageButton;

private UserClass userClass = new UserClass();

public void initialize(URL location, ResourceBundle resources) {
   imageButton.setStyle("-fx-background-image: url('"+userClass.PhotoPath()+"')");
}
Sign up to request clarification or add additional context in comments.

7 Comments

By the way that is my simplified UserClass so the my question is easier to be understand, actually its a class to retrieve user data such as name, id , and profile picture path from text database. I can upload full userclass code if its neccessary.
I have tried your solution! The good news is it show no error. The bad news is the button display no picture.
I have updated the code! And showing no-picture means invalid-path of the picture.
it says "setStylesetStyle is undefined for the type button"
@SherwinChia setStylesetStyle? or setStyle ... are you sure that Button is from javafx.scene.control.Button ? Maybe your path is not matching - so add / on you image path -> /'src/Pictures/womanprofile.png
|
0

You should check if your button size are too small. Hence, it only shows your background transparent image. Try enlarge your button and see how it solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.