gomzee
gomzee

Reputation: 103

Is this right way to code for JavaFx application?

I am new to JavaFX. SO i would like to know is the code provided below is the good way to caode in for JavaFX application by declaring .fx files.

or

We should code using.java files. Help is needed on this regard.

   import javafx.stage.Stage;
   import javafx.scene.Scene;
   import javafx.scene.Group;
   import javafx.scene.text.Text;

   import javafx.ext.swing.SwingLabel;
   import javafx.ext.swing.SwingTextField;
   import javafx.ext.swing.SwingButton;

   var login = false;
   var userName = "";
   var systemUser = "test";
   Stage {
    title : "Login App"
    scene: Scene {
        width: 300
        height: 300
        content: bind if(not login) Group{
                          content: [
                             SwingLabel{
                                 text: "User Name:"
                             },


                             SwingTextField {
                                 text : bind userName with inverse;
                                 columns : 10;
                                 editable : true;


                             },
                             SwingButton{
                                 translateX: 50
                                 translateY: 50
                                 text: "Submit"
                                 action: function(){
                                     if((userName != systemUser)) {
                                            println("Invalid UserName");

                                 }
                                 login = (userName == systemUser);
                                 }
                            }

                         ]
                     } 
                     else Group{
                         content: [

                   Text {
                                 x: 10 y: 30
                                 content: "You have successfully logged in."
                              },
                              SwingButton{
                                 translateX: 10
                                 translateY: 50
                                 text: "Log out"
                                 action: function(){
                                  userName = "";
                                  login = false;
                                }
                              }

                      ]
                  }
                } 
    }

Upvotes: 1

Views: 241

Answers (1)

jewelsea
jewelsea

Reputation: 159291

The JavaFX script code (with .fx files) in your question is from the obsolete JavaFX 1.x branch. I strongly advise you not to use the JavaFX 1.x branch. Oracle will drop all support for it this month.

The script code in your question also integrates Swing controls inside JavaFX, which is not supported in current JavaFX 2.x releases.

Instead, use JavaFX 2+ and write your JavaFX code in Java (with .java files) using only JavaFX controls and no Swing controls.

There are some excellent tutorials to get you started:

Upvotes: 3

Related Questions