0

I have a long running data processing application. Depending on the data it sees, I may want to start up a JavaFX application window (or it may never start-up one). How do I pass in my cached data?

e.g. in Swing, I'd have had a constructor that might look:

class MyFrame extends JFrame{
  public MyFrame(BlobOfInMemoryCachedData manyMegabytesOfData){
    this.data = manyMegabytesOfData; 
    // create JPanel, etc. using that data
    ...
  }
}

Based on this answer: https://stackoverflow.com/a/24611918/155631, I can imagine non-standard-looking-java workarounds. Before I bake the usage of such workarounds into the application design, I want to verify: Is there really no cleaner way to directly pass the object handle?

Community
  • 1
  • 1
Matt S.
  • 878
  • 10
  • 21
  • What are you trying to do? Is your long running data processing application a Java application? When it is running do you want to occasionally pop up a JavaFX window running in the same VM? And do you want your JavaFX window to access data referenced by an object entirely in the memory space of the long running process? And that data is in the same VM as the JavaFX application, so the JavaFX application also has access to it? If so, why doesn't a simple method call which passes an object reference suffice? – jewelsea Feb 11 '15 at 01:15
  • @jewelsea: the answer to all your questions before the last the last one is 'Yes'. On the last one, what do you have in mind? (I've used swing, but I'm brand new to javafx). If I extend javafx.application.Application, I'm seeing the static method 'launch', but no non-static launching methods expect 'start' which takes a javafx.stage.Stage object, something I'm assuming the framework creates. I was hoping some 'simple method call' would work, but it's not yet obvious to me how to create/make that call – Matt S. Feb 11 '15 at 02:15

1 Answers1

1

Very quick because I haven't got time to write a proper answer with sample code:

  1. In the main for your long running Java application, call the JavaFX application launch method once (and only once for the entire lifetime of your application).
  2. Call Platform.setImplicitExit(false).
  3. In the start method of your JavaFX application don't show a window.
  4. Provide a static accessor on your JavaFX application show(data) which passes your blob data.
  5. The show(data) method displays a JavaFX window for relaying data processing info in a UI.
  6. When necessary call a static hide() method on your application which hides the JavaFX application window.
  7. Continue processing, performing steps 5 and 6 (showing and hiding the window as needed).
  8. When everything is finished call Platform.exit().

Key thing is that the JavaFX application is only launched once and you invoke a static accessor on it as needed to show the window. The set implicit exit false stuff prevents the default behavior of the JavaFX runtime shutting down when the last window of the application is hidden (so it just chugs along in the background waiting for a signal to show something again).

You could simplify things a little bit by having the your data processing application just extend the JavaFX Application class, but you might want to keep them separate for ease of testing or other design reasons.

Your other option is to use JFXPanel, but that adds an unnecessary Swing dependency, so I'd advise against that.

jewelsea
  • 150,031
  • 14
  • 366
  • 406