John Damien Smith

Ranch Hand
+ Follow
since Jan 26, 2012
Cows and Likes
Cows
Total received
24
In last 30 days
2
Total given
0
Likes
Total received
95
Received in last 30 days
0
Total given
30
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Damien Smith

There are some really poor suggestions on this thread.

Don't run a busy loop with a counter that goes to a million on the JavaFX thread, it will hang your app periodically.
Similarly, don't call Thread.sleep on the JavaFX application thread, it will also hang your app periodically.

For most operations in JavaFX, you need to relinquish control of the JavaFX application thread, so that the system can take action on the commands which you have issued.  Not doing so just hangs the UI and application and, in addition, it doesn't do what you expect.  It doesn't do the first thing, then pause, then do the next thing.  Instead, the system just buffers up all the things you asked it to do, and only executes them all at once after you release control back to the system.

-----

Knute's suggestion to use a Task which runs a separate thread which does the pauses, then a runLater call to execute the event at each period, does at least fix one of the main problems occurring here by shifting the pause into its own thread rather than in the JavaFX application thread.

-----

If you want a delay for a period in JavaFX, where something happens after a period, one way to accomplish that is to use a PauseTransition with a setOnFinished call:
  https://docs.oracle.com/javase/8/javafx/api/javafx/animation/PauseTransition.html
  https://stackoverflow.com/questions/30543619/how-to-use-pausetransition-method-in-javafx


An advantage of this over a task-based approach which spawns another thread is that there is no other user thread involved, so you have less concurrent code to manage and worry about, which is pretty much always a good thing.

-----

For more complex interactions, a Timeline can be used rather than a PauseTransition.

The key thing about timeline and pause transition is that they are event-based, so you provide a callback event handler function in each case whenever the event you wish (the pause for a certain period of time) is triggered.
Due to the event handling nature of the system, there is no busy waiting or sleeping the JavaFX thread, instead, you get notified and can take action when the event is fired.

There is an example of a timeline based approach here:
https://stackoverflow.com/questions/9966136/javafx-periodic-background-task


-----

But, because there are higher-level APIs built for media processing, this is even simpler.

-----

First, for playing beep type sounds, you can use a simpler API than Media, AudioClip is built for that:
  https://docs.oracle.com/javase/9/docs/api/javafx/scene/media/AudioClip.html

If you want to repeat the same sound more than once, then you can set the cycle count on the audio clip:



-----

Now, sometimes it is a good idea to use the richer API and functionality of the Media classes rather than an AudioClip.  For example, when the Media being played is pretty long and should be buffered (unlike an AudioClip which can load the entire media into memory), or when you need the extra functionality provided by the Media API.

One such example is the double beep situation you provide in your follow-up question, where you are playing one sound, then playing another different sound after the first sound completes.
Rather than trying to do a busy wait pause of the JavaFX application thread which hangs your app, listen for an event which signals that the first media has finished playing, and use that event to trigger playing the second media.



-----

You note:
> I also consider using "aplay" system call cause so far this approach is working best on Linux
My guess is that using system calls for this kind of thing is probably not a good idea. 
You lose platform independence and you end up re-inventing the wheel because there is already decent media playback support in JavaFX.

4 days ago
Please provide the code you have .
That will make it easier to answer.
2 weeks ago
See:
  https://gist.github.com/jewelsea/4955598 "Sample for accessing a local database from JavaFX."
2 weeks ago
> How do i change this to work outside events because i need to use there event to getSource ?

Can you provide an example of where you would like to do this outside the context of the event?
Perhaps a description of the use-case or a code snippet or mcve (https://stackoverflow.com/help/mcve) with a comment in it, "// how do I change scene here...".
3 weeks ago
Take a look at how the standard JavaFX alerts handle this.
Perhaps you can use a similar construct of returning an Optional ButtonType from a showAndWait method defined on the alert.

Here is an example with an Alert:

Code is from:
http://code.makery.ch/blog/javafx-dialogs-official

Or you could instantiate or extend Dialog and make use of its existing ButtonType returning logic and other functionality if that is appropriate for you.
Instantiating a Dialog is what I would recommend if that fits well into your application.
To see how to do this, look at the "Custom Login Dialog" section provided in the makery dialog tutorial link I provided.
And, yeah, I realize the Dialog API is a bit strange and would take some time to get used to.  But it might be worth investing that time in this case in order to be able to make use of the built-in functionality which Dialogs provide.
1 month ago
> 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.
1 month ago
Find out which java you are using by running:

$ java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

I showed the sample output from my Mac, which is using Oracle JDK 9 by default.

If the output shows openjdk, rather than "Java HotSpot", then you are using an openjdk distribution provided for Ubuntu and not Oracle JDK.
By default (as far as I know), the Ubuntu openjdk installation does not include JavaFX, but instead uses a separate package which it calls openjfx:
  https://stackoverflow.com/questions/34243982/why-is-javafx-is-not-included-in-openjdk-8-on-ubuntu-wily-15-10
You can install this using:
  sudo apt-get install openjfx
NOTE: you don't need to run that command if you are using Oracle JDK as Oracle JDK should have JavaFX included as long as you have installed the standard Oracle JDK
and not the server only version (which would be unlikely).

I'd advise using an IDE over basic VIM.  You can certainly get stuff done with VIM and the command line, but you will probably be more productive (in the end ;-) with an IDE.
One thing an IDE will do is make it easier to run basic builds on projects as it knows all of the code in the project and can build it for you.  Without an IDE, you will want to look
into using build tools such as Maven as soon as you start developing projects which are more than one or two files and are split across multiple packages.  There is a bit of learning
curve to tools like Maven, which I would advise you avoid until you really need them.

I'd advise using Intellij Idea over Netbeans.  I just personally like it a little better.
  https://www.jetbrains.com/idea/
If you use Intellij Idea, you can configure the JDK it uses for development:
  https://www.jetbrains.com/help/idea/working-with-sdks.html#define-sdk
When you do that, for development, make sure you configure the IDE to use a JDK installation which includes JavaFX (for example Oracle JDK 9).

However, Netbeans is perfectly fine if you wish to use that.

Note that the Netbeans release binaries currently don't run on Java 9.  So, if you are using Netbeans, it is probably best to stick with Java 8 for now.
For context see:
  https://jaxenter.com/netbeans/using-apache-netbeans-incubating-jdk-9
That talks about building an incubating version which runs on JDK 9.  I wouldn't recommend that, as you don't need it.
Instead, just use the standard NetBeans JavaSE based download which would include an Oracle Java 8 version bundled.  This would have JavaFX support included.
  https://netbeans.org/downloads/


1 month ago
I advise running a google search "rogue dungeon generator code java"
It will turn up lots of useful links, tutorials and code samples which should assist you in this task.

I also advise just getting the system to work with a text string or text file output that spits out your sample generated dungeons.
Once you have verified that that is working, then try integrating it into a graphical display system such as JavaFX and rendering as you wish.
1 month ago
> Yes I know with scene.Text and TextBoundsType.VISUAL but i need some css propertys that is not supported by scene.text control

There is no such thing, you need to do it in code.

> I don't understand your tip with text property listener, because i think scene.Text doesn't support TextBoundsTypes respectively nothing found in code javafx.scene.control.Labeled and javafx.scene.control.Label

Yeah, that wasn't very clear, sorry about that.

Here is some sample code.  It probably doesn't do exactly what you want, but it should demonstrate what I meant:
3 months ago
Perhaps try using Text instead of Label and set the TextBoundsType on the Text to VISUAL.
  https://docs.oracle.com/javase/9/docs/api/javafx/scene/text/TextBoundsType.html
You also might be able to stick with a label, add a listener on its text property and set the TextBoundsType appropriately whenever the text property changes (not sure, I didn't try that).
3 months ago
I don't understand your question well enough to offer a solution.

Perhaps an application of the Mediator may help you solve your issues:
Review the code from the Java implementation from the link.
Though, as you are just beginning programming UI stuff, I would caution against some of the abstract design patterns like Mediator as it may just confuse you more than it would help you . . . still, it may be an a reasonable approach to your problem.

As an aside, go to grammarly.com, set up a free account there and copy and paste your text into it, correct any errors it flags before you post it, then post the corrected text. 
It will make your writing much more professional and you will be able to communicate more clearly and have a better chance of getting good responses (just my opinion).
4 months ago
> Why wouldn't it do that? HtmlUnit supports quite a few JavaScript libraries, especially jQuery, and has no principal problems executing JavaScript.

I probably should have looked into HtmlUnit a bit more closely before posting earlier, as I didn't know that :-).  Based upon what you write, HtmlUnit would likely suffice for many tasks.  My guess (and it is only a guess) is that WebView might be a bit more functional and compatible  than HtmlUnit as far as implementation of html5 style features, because WebView is based upon a WebKit implementation which has likely undergone more extensive compatibility testing against the html5 specs and various websites than HtmlUnit.  But yes, both approaches would probably be fine depending upon the actual application required.  HtmlUnit would probably be preferred if you wanted to do evaluation and testing of the page load results because I think it was created to directly support those kind of scenarios.  WebView would be good if you want an actual rendering that you can snapshot to an image or something like that.
4 months ago
One reason something like WebView may be better than something like HtmlUnit is that WebView is a full browser environment, so it will execute any javascript or stuff related to html5 extensions to Java which something like HtmlUnit will not do.  If the page is loading stuff with Ajax or dynamically adding stuff to the HTML DOM using JavaScript, which is pretty common nowadays for a lot of sites, then WebView would correctly render this to create the DOM, but HtmlUnit would not.  If the purpose is to create something like a library of reasonably accurate screenshots of rendered pages, the WebView would be reasonable for that, but HtmlUnit would not.  However... I have no idea what Niti is really trying to do, so I couldn't say whether one approach or the other would be helpful for Niti or not...
4 months ago
Change to the directory where the jar is located.

In your log message that is:
  C:/Users/Removed/Documents/NetBeansProjects/SoftwareOneProject/dist/run102474145/SoftwareOneProject.jar
But, I guess it could change between runs depending upon what NetBeans is doing.

Run jar tvf SoftwareOneProject.jar

Post the output of to this forum.

See if the css file is actually where you expect it to be in the jar.
4 months ago