Vaibhav's Blog Space

  • Java
    November 30, 2008

One scene to other - JavaFX

Guest Author

Any middle or big application demands to change one window to other at some point of time. A window type of thing in JavaFX is represented by Scene and its each to switch between scene or to run multiple scenes.

Here is a small application in which clicking on image will put you in another window, written "Hello World" 

package sample6;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
var s_new:Scene;
var s = Scene {
content: [
Text {
font: Font {
size: 24
x: 10,
y: 30
content: "HelloWorld"
var s1 = Scene {
content: [
ImageView {
image: Image {
url: "{__DIR__}im2.PNG"
onMouseClicked: function( e: MouseEvent ):Void {
s_new = s;
s_new = s1;
Stage {
title: "Application title"
width: 250
height: 280
scene: bind s_new

So, its simple, on mouse click, I have bind a scene variable with a new scene. That's it !

Join the discussion

Comments ( 13 )
  • CarlD Monday, December 1, 2008


    I am getting a Encountered "s_ne" at line 32, column 16 in JavaFX pad.

    Should you have the png image on you blog too?

    Looks easy but getting things to work is not so easy.


  • Vaibhav Monday, December 1, 2008

    Oh I thought some typo somewhere in the FXpad. it will be s_new in place of s_ne.

    Ya you can use this image :

    http://blogs.sun.com/vaibhav/resource/im2.PNG, so you can replace the complete line : url: "{__DIR__}im2.PNG"


    url: "http://blogs.sun.com/vaibhav/resource/im2.PNG"

    and yes imp thing for you: .PNG and .png, is different sometime. Though there is a bug raised for it but do take care of it.

  • CarlD Tuesday, December 2, 2008


    I'm still having trouble.

    What version of JavaFX Script language are you using?

    Could you provide the imports in your code?

    The Stage class in NetBeans is javafx.application.Stage not


    I cannot find the Scene object at all.

    I hope Sun fixes JavaFX Pad to be up to date with Version 1.0

    IMO, sweeping consistency needs to happen with JavaFX with JavaFXPad, API, Tutorials, Web sites.


  • Vaibhav Tuesday, December 2, 2008

    ah you have to use NB 6.5 and FX 1.0. I am really not sure about FX pad, actually I doubt :). NB has now those new drag and drop features and so FXpad is less in use. I guess on what you are working we still need to write Frame in place of Stage and thats why this application is paining you.

  • CarlD Tuesday, December 2, 2008


    I have NetBeans IDE 6.5 and it doesn't have the plug in for JavaFX. How do I get the pluggin for v1.0?

    Nice demos

    I just want to get all the JavaFX demos to work from yours and other sites.



  • Vaibhav Tuesday, December 2, 2008

    you have to wait for 2 more days :). JavaFX 1.0 going to release on 4th dec. Right hand side : http://javafx.com/

    thanks for appreciating the demo.

  • carlos Monday, April 13, 2009

    you have any Idea of how to go back from the "Hello world" scene to the first one?? I tried using the same "OnMouseClick" to change the scene, but it gives me some kind of error of a cycle.

    Any ideas?

  • Vaibhav Choudhary Monday, April 13, 2009

    Hmm it can cause some problem. The best way to do this is to add and delete component dynamically in gp. So, if you hello world on next screen and you want to go back on prev. screen, then add the element of prev screen and delete "hello world" stuff.

  • sivas Monday, April 20, 2009

    how can i generate a pop up window using java fx

  • Vaibhav Choudhary Monday, April 20, 2009

    pop up window is not in JAVAFX API yet. You can write your custom window like a pop up and can do various thing with it.. like you can close that in a timeline after sometime..

  • Tag Heuer Sunday, December 20, 2009
  • tsubaki Thursday, January 7, 2010

    THANK YOU SOOOO MUCH for this tutorial. It helped a lot. =)

  • tiago de almeida machado Wednesday, April 6, 2011

    How do I create a scene at a class or file, and separate these scenes of main.fx? has a knack of doing this?

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.