1

I have a question.

How does one initialize the JavaFX toolkit with the method I found in an earlier StackOverflow Question? The topic can be found here: JavaFX 2.1: Toolkit not initialized

I am trying to use a solution similar to this solution from that thread:

Problem: Non-trivial Swing GUI application needs to run JavaFX components. Application's startup process initializes the GUI after starting up a dependent service layer.

Solutions Subclass JavaFX Application class and run it in a separate thread e.g.:*

public class JavaFXInitializer extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        // JavaFX should be initialized
        someGlobalVar.setInitialized(true);
    }
}

The only problem I have is: What do I do with

someGlobalVar.setInitialized(true); ?

I don't know what to fill in there, and some tips would be appreciated :)

Community
  • 1
  • 1
ImJustACowLol
  • 826
  • 2
  • 9
  • 27
  • 3
    Can't you just use a [`JFXPanel`](http://docs.oracle.com/javase/8/javafx/api/javafx/embed/swing/JFXPanel.html)? It will start the toolkit on instantiation, if it's not already started. – James_D Jun 24 '16 at 13:50
  • @James_D I could, but the thing with JFXPanel is that it uses up roughly 10mb of RAM, even when set to null it requires quite some time before the GC picks up on it. I know 10MB does not sound like a big deal, but I don't like unnessecary resource usage. – ImJustACowLol Jun 24 '16 at 13:54
  • 1
    I suspect the vast majority of that is used because it starts the FX Toolkit... – James_D Jun 24 '16 at 14:06
  • You can read my answer to http://stackoverflow.com/questions/32739199/javafx-software-design for a way to start the FX toolkit; but honestly your reason for not using `JFXPanel` makes no sense (you have no way to know that the resource usage is "unnecessary") – James_D Jun 24 '16 at 14:18
  • @James_D I managed to solve it a little longer back, and you are right: The JavaFX toolkit uses all of this memory. Do you know any ways to reduce this? – ImJustACowLol Jun 24 '16 at 14:41
  • 1
    No. I'm surprised it only uses 10MB. For an entire UI toolkit that seems pretty small. – James_D Jun 24 '16 at 14:55
  • @James_D Ahh okay. The ironical thing is I only use the MediaPlayer of JavaFX and nothing that has to do with the GUI-side of things. Java without JavaFX does not properly support a wide variety of audiofiles, and I am yet to find a library that supports mp3, .wav, .flac, .aac, .ogg all at the same time consistently. Meanwhile I'm stuck using JavaFX. – ImJustACowLol Jun 24 '16 at 15:15
  • If you only need the FX toolkit to start and aren't showing any UI controls, it makes sense not to create a `JFXPanel`. I would use the technique in the question I linked earlier (which basically actually answers your question about setting a flag to show the toolkit has started). – James_D Jun 24 '16 at 15:18

2 Answers2

1
import com.sun.javafx.application.PlatformImpl;

public class JavaFXInitializer
{

        public JavaFXInitializer()
        {
            initFx();
        }
        private synchronized static void initFx() {
            PlatformImpl.startup(() -> {
            });
        }
}
ImJustACowLol
  • 826
  • 2
  • 9
  • 27
1

I am writing this answer for the comment you have done about how to support (.mp3,.wav,.flac,.ogg) etc in java.For .mp3 you can use JLayer http://www.javazoom.net/projects.html search on web for examples.

About (.mp3,.wav.flac,.ogg) and some more you can use JavaZoom BasicPlayer which uses some external libraries to support them you can download the zip folder here(download without installer and you open the zip folder).

Then go on the folder lib and copy all the .jars except kj_dsp which can be used for visual representation on audio data and contains also a class about fast fourier transform(FFT).Also change MP3_SPI1.9.3 with MP3SPI1.9.4

Then add these .jars into your project libraries and just use:

BasicPlayer player = new BasicPlayer();

The whole thing uses Service Provider Interface (SPI) mechanism.

It runs on a separate thread so you don't have to worry.It works really well but the project is a little bit old.It's a good start!About docs check the website.

GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • I will look deeper into this tomorrow. As far as I can see the BasicPlayer lacks some functionality that I'll probalby have to add myself such as getting the current position in the audio clip, and getting the maximum position. But I'll figgure out how to do this eventually ;) I'm off to bed. Thanks for the help so far bud! – ImJustACowLol Jun 24 '16 at 21:01
  • @ImJustACowLol accepted as answer? :)...When you are using java sound api you can go as low as you want.I mean you can control the sound bit by bit. – GOXR3PLUS Jun 24 '16 at 23:01
  • I understand that, but I am rather new to audioprocessing, hence the reason that I rather use libraries for now :) – ImJustACowLol Jun 25 '16 at 12:27