Thursday, June 7, 2012

JavaFX 2: Add Image to Button

This is a tutorial about how to add image to button with JavaFX 2. As you can see from the image below, we will have 3 buttons with images. Two of them will have just only images, but one of  them will have image and text.



Important: Do not forget to add images to /src folder of our project.
 Here are the pictures:  

Here is the code of our example:


20 comments:

  1. Here is my Code, how it goes with fxml,

    http://pastebin.com/embed_js.php?i=2FGzjLpy

    ReplyDelete
  2. I haven't tried do this with CSS, but if you want, you are free to try it. If you succeed to do that, let me know. I could write a blogpost, because that sounds very interesting.

    ReplyDelete
  3. On trying the same code i get this error,how to clear it ?
    Exception in Application start method
    Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:722)
    Caused by: java.lang.NullPointerException: Input stream must not be null
    at javafx.scene.image.Image.validateInputStream(Image.java:997)
    at javafx.scene.image.Image.(Image.java:624)
    at imageToButton.start(imageToButton.java:50)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)

    ReplyDelete
  4. You have null pointer on 50th line of the code.

    ReplyDelete
  5. yes the place where i add the image to the button, i have the image at the right path but i don't know why do i get such error..

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. I have tried to add an image in this way:
    Image img1 = new Image("file:///C:/Users/prem/IdeaProjects/imageToButton/about.png"); then it works perfect but i get nullpointerexception on using this code: Image img1 = new Image(getClass().getResourceAsStream("about.png")); may i know why ?

    ReplyDelete
  8. in order to allow to work also in Eclipse running under Windows OS you have to put the .png files under the base home path of the project, for example C:\develop.eclipse\imageToButton\about.png where C:\develop.eclipse\imageToButton is the home path of the project, the parent of src/ and /bin

    ReplyDelete
  9. Sorry,... the quickest, simplest and better hint his to place the .png files in the same directory of imageToButton.jar executable ... it was almost obvious ;-) to workaround

    ReplyDelete

  10. For those who have NullPointException try to add a slash before image name

    Image(getClass().getResourceAsStream("/about.png")); if the images are located in src folder .

    ReplyDelete
  11. Here is my code:
    Image locki = new Image(getClass().getResourceAsStream("/lock.png"));
    ImageView lock = new ImageView(locki);

    Button b1 = new Button("1"); b1.setOnAction(e -> select(1));
    Button b2 = new Button(lock); b2.setOnAction(e -> select(1));

    Eclipse underlines my call for the ImageView, and when I compile my project it prints out a list of errors.

    ReplyDelete
  12. Vital Progenix Apart from natural male enhancement pills, there are also other methods that can help enhance men's sexual performance as well as the size of their manhood in the form of male enhancement exercises. A lot of these exercises had been around for centuries and these are specifically designed to increase the size of the penis the natural way without the need to undergo surgical procedures or take any medicines in order to enhance penile size.

    https://fitose.com/vital-progenix/

    ReplyDelete
  13. Biofluxe Keto In addition to the physical benefits, exercise also brings mental benefits. Exercise can really help a woman who is wrestling with PCOS to feel like she is taking charge of her situation. Exercise can really help her to feel powerful and truly contradict any feelings of being a victim to her body and the bothersome symptoms of PCOS.

    https://fitose.com/biofluxe-keto/

    ReplyDelete
  14. Rapid Trim 24/7 Rapid weight loss diets can be safe and effective if you just use some common sense and follow the suggestions outlined above. I wish you all the best in your efforts towards a thinner waist and a healthier lifestyle. As you can see, a moderate diet plan and moderate exercise program combined with solid emotional support is the key to succeeding with rapid weight loss.

    https://fitose.com/rapid-trim-247/

    ReplyDelete
  15. Vexgen Keto It's best to focus in on the types of exercise that you truly enjoy or can at least do on a regular basis. You want to have fun with your exercise whether it is at home, in the health club, at a gym, or outdoors. You want to keep it fun and make it work for you and your needs.

    https://fitose.com/vexgen-keto/

    ReplyDelete
  16. Pure Primal Keto Plenty of those weightloss centres have weekly meet-ups wherever each individual discusses and weighs in to examine their progress. several of those weight loss centres embody exercise instrumentality that may assist you progress and lose the load that you simply need to lose.

    https://fitose.com/pure-primal-keto/

    ReplyDelete
  17. keto genesis advanced is specially formulated to get your body to BURN FAT while also INCREASING PERFORMANCE. Our fact acting formula is designed for the specific needs of a ketogenic diet and the related lifestyle, to help you get into and stay in ketogenesis so your body burns fat while also lowering blood glucose levels.

    ReplyDelete
  18. hi
    thanks for giving great information. keep posting.
    bestslimming centre in hyderabad

    ReplyDelete