• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Couple Basic JavaFX Questions  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - brand new to JavaFAX, and for that matter, to Java period.

I started out building a Swing GUI, and almost had my head around that when I learned that JavaFX was the "future". So, I switched to trying to learn and use that and have been quite frustrated. Most of the examples I find and copy/paste into my editor don't work - error all over the place. I think a lot of that is because the people writing the samples/example never bother to talk about context or required imports, etc.

Even if I get the imports sorted out, I still get a lot of errors, and I THINK maybe I'm beginning to understand why, but want to verify this.  It seems that the JavaFX forms (scenes? stages? windows? whatever they're called) created by FXML/Scene builder, don't want to work except in the context of a JavaFX "Application" - that is, a class that extends "Application". Is this correct?  In addition, I can't get it to work except within (or called from) the "start" method. Is that right?

If so, that means there is no way to simply do a quick alert dialog w/o it all being wrapped up in this stuff? This seems awfully clunky IMO. Can I mix FX with the Swing stuff? All in the world I want to do is have a simple dialog box come up with Ok/Cancel buttons. In many/most other languages, this is as simple as something like:



which can be called from anywhere , anytime. Does Java have something like that? Please advise. THANKS!
 
Greenhorn
Posts: 16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken --

 I was in your shoes a couple years ago!

 What saved me was the book, Learn Java FX 8 by Kishori Sharan.  It is totally marvelous!

 A decision I made, which you may or may not, was to NOT use the SceneBuilder until I had a clue what was happening under the hood.  (Here is where the Sharan book was really good for me).  I still, a couple years later, do not use the SceneBuilder, but I suppose I might in the future.

 -- Chris

 
 
Ken Matson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chris R Olsen wrote:Ken --

 I was in your shoes a couple years ago!

 What saved me was the book, Learn Java FX 8 by Kishori Sharan.  It is totally marvelous!

 A decision I made, which you may or may not, was to NOT use the SceneBuilder until I had a clue what was happening under the hood.  (Here is where the Sharan book was really good for me).  I still, a couple years later, do not use the SceneBuilder, but I suppose I might in the future.

 -- Chris

 



Thanks - I'll take a look. At the moment - I've just kind of looked at all this as a "black box" and am putting my code in it. At least I'm making progress on my app that way!
 
Ranch Hand
Posts: 374
24
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> trying to learn and use that and have been quite frustrated. Most of the examples I find and copy/paste into my editor don't work - error all over the place.

The following tutorial from Makery may help:

* http://code.makery.ch/library/javafx-8-tutorial/

>  All in the world I want to do is have a simple dialog box come up with Ok/Cancel buttons

Sample code for showing a Confirmation Alert:


Makery provide further examples:

* http://code.makery.ch/blog/javafx-dialogs-official/

> It seems that the JavaFX forms (scenes? stages? windows? whatever they're called) created by FXML/Scene builder, don't want to work except in the context of a JavaFX "Application" - that is, a class that extends "Application". Is this correct?

No, that's not really right.  The FXMLLoader loads FXML to create a hierarchy of nodes which can be placed in the Scene Graph (the Oracle tutorial "Working with the Scene Graph" explains what that is).  FXML does not create scenes, stages or windows.  The code that backs an FXML form would go in a controller class.  A controller class would never extend Application.  An Application could create invoke the FXMLLoader to load an FXML (which generates the associated nodes and invokes initialization logic in the controller).  

You can see the structure used in the Oracle JavaFX tutorial:

* https://docs.oracle.com/javase/8/javafx/get-started-tutorial/fxml_tutorial.htm#CHDCCHII "Using FXML to Create a User Interface"

That tutorial links to https://docs.oracle.com/javase/8/javafx/sample-apps/FXMLExample.zip which contains full code for the tutorial.

> In addition, I can't get it to work except within (or called from) the "start" method. Is that right?

In a way.  Generally an Application with a start method is the entry point for a JavaFX app, in the same way that a main class in Java with a static main method is the entry point for a standard Java app.  This is demonstrated in the Oracle tutorial https://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm "Hello World, JavaFX Style".  That is pretty much the JavaFX equivalent of a typical Java hello world program: https://introcs.cs.princeton.edu/java/11hello/HelloWorld.java.html .  A JavaFX program only has a single Application class which is invoked when the program is run (the same way the Java launcher only invokes the static main method once).  Once the application is launched it can instantiate other classes and execute code in them, so not all code needs to be in the start method.

There are other ways you can launch JavaFX functionality (e.g. a JFXPanel embedded in a Swing application), but as a beginner, you should not be using them.

> Can I mix FX with the Swing stuff?

Yes, you can, but don't do it.  Mixing JavaFX and Swing only makes sense if you have a large Swing application in which you want to embed a bit of JavaFX functionality or if you have a large JavaFX application in which you want to embed a complex existing Swing component (such as a Swing based PDF viewer for example).  As you are just starting out, neither of those situations will apply to you.  There are numerous considerations in mixing JavaFX and Swing code, which can tend to make it tricky (e.g. the UI of JavaFX and Swing components will have a different look and feel, care must be taken with threading as JavaFX and Swing run on different threads, etc).

Of course, you don't need to use Java to use JavaFX, you can use a scripting language if you prefer (which in some cases, some people may feel requires less unnecessary syntax and overhead).  For example here is the code example from above, but written in JavaScript:

You run it with the following command (assuming the code is saved to a file named confirm.js):

That runs the Nashorn JavaScript launcher and interpreter, passing the -fx argument to launch a JavaFX script.  The launcher knows it is a JavaFX app and runs the script inline.  It needs no start() method because the -fx switch already told the launcher that it is starting a JavaFX app.  For more info see: https://www.developer.com/java/data/leverage-a-javafx-application-with-nashorn-script.html

I don't really recommend you write your JavaFX apps in JavaScript though... you won't get a lot of forum support for doing that if you have any questions about it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!