Vaibhav's Blog Space

One scene to other - JavaFX

Monday Dec 01, 2008

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 !


[10] Comments
Like this post? del.icio.us | furl | slashdot | technorati | digg
Comments:

Vaibhav,

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.

-Carl

Posted by CarlD on December 01, 2008 at 11:58 PM IST #

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"

from

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.

Posted by Vaibhav on December 02, 2008 at 12:39 AM IST #

Vaibhav,

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
javafx.stage.Stage

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.

-Carl

Posted by CarlD on December 02, 2008 at 08:42 PM IST #

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.

Posted by Vaibhav on December 02, 2008 at 08:57 PM IST #

Vaibhav,
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.
Thanks!

-Carl

Posted by CarlD on December 02, 2008 at 09:19 PM IST #

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.

Posted by Vaibhav on December 02, 2008 at 09:23 PM IST #

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?

Posted by carlos on April 13, 2009 at 08:39 PM IST #

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.

Posted by Vaibhav Choudhary on April 13, 2009 at 10:50 PM IST #

how can i generate a pop up window using java fx

Posted by sivas on April 20, 2009 at 04:17 PM IST #

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

Posted by Vaibhav Choudhary on April 20, 2009 at 04:18 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed