Javafx Project Structure

JavaFX Project Structure

IMHO, you shouldn't create package's depending on your views.

My approach towards such applications

  • A package for corresponding controllers of these views
  • Different packages for service(business) and dao(persistence) layer, if exists
  • A directory for resources such as images, css etc
  • A directory for FXML files called view in the resources

    src/main
    ├──java
    ├── controllers
    ├──Screen1controller.java
    ├──Screen2controller.java
    ├── service
    ├──Service1.java
    ├── dao(persist)
    ├── SaveProducts.java
    ├──resources
    ├──view
    ├──screen1.fxml
    ├──screen2.fxml
    ├──css
    ├──style.css
    ├──images
    ├──img1.jpg
    ├──img2.jpg

The above implementation can be considered for a Maven project.

For a simple project, you can view a structure here. It is a maven project!

Are there any standard for JavaFX directory/package structure?

There is no best Solution for this.

But an example way is:

Create a properly named package structure for java files.

Main java files (example: de.kogs.testapp)

Controller java files (example de.kogs.testapp.controller)
and so on.

For fxml files:

example

fxml/main.fxml

fxml/slideouts/userSlide.fxml

Your pictures and css Files:

css/main.css

css/images/logo.png

How to create a JavaFX project within another project and open it in IntelliJ

Choose File | New Module... instead of File | New Project....

The setup of the new module after that will be similar to the new project, so you can just follow the instructions you linked for creating a new JavaFX project.

If you want to link the multiple modules together with a Maven build, that is much more complex (especially if you use both maven and java platform modules), so I won't describe that process here.

JavaFX best practise guide for small/medium/big desktop applications?

The first thing you should do is NOT trying to re-invent the wheel like so many people here. Have a look at one of the existing application frameworks for JavaFX, e.g. MVVMFX https://github.com/sialcasa/mvvmFX/wiki
Such an application framework will give you a solid structure for your project which you can build on.

How to create an empty JavaFX project in IntelliJ

When creating the new project, choose "Java" instead of "JavaFX".

A JavaFX application is just a Java application, so if you don't want the additional things which IntelliJ is doing when you choose to create a JavaFX project (e.g. supplying example code and associating with a build system like Maven or Gradle), you can just choose a basic Java application project from the wizard and it won't do those other things.

See the section in openjfx.io documentation titled "JavaFX and IntelliJ IDEA" for other steps you need to take:

  1. Set the project JDK

    • File -> Project Structure -> Project
  2. Create a JavaFX library in Idea

    • File -> Project Structure -> Libraries
    • Point to the lib folder of the JavaFX SDK.
  3. Add VM options for the module path

    • Run -> Edit Configurations...

      --module-path /path/to/javafx-sdk-15.0.1/lib --add-modules javafx.controls,javafx.fxml

    For windows use quotes around the path and \ rather than /.

  4. Run the project

    • Run -> Run...

You might also need to take the actions identified in the accepted answer to:

  • How to convert a normal java project in intellij into a JavaFx project

But that answer was written a while back and setting the resource copy configuration to include JavaFX fxml and css files might not be needed anymore.

Now, you might think that is annoying amount of things to do, and I might agree with you.

JavaFx 2.0 FXML project How to access variables and project structure

Here is one possible way to do this:

Give your ServerConnection class a callback to call when it receives a message:

public class ServerConnection {

private Consumer<String> messageCallback ;

public void setMessageCallback(Consumer<String> messageCallback) {
this.messageCallback = mesasgeCallback ;
}

// other fields and methods as before...

public void startCommunication() throws IOException {

// code as before ...

while ((servResp = reader.readLine()) !=null) {
if (messageCallback != null) {
messageCallback.accept(servResp);
}
// etc....
}

// etc
}
}

Now instantiate your ServerConnection class from the controller and pass it a callback:

public class FXMLDocumentController {

ServerConnection serverConnection ;

// ...

public void initialize() {
serverConnection = new ServerConnection();
serverConnection.setMessageCallback(message ->
Platform.runLater(() -> txt.appendText(message+"\n")));
Thread serverThread = new Thread(() -> serverConnection.startListening());
serverThread.setDaemon(true); // thread will not stop application from exiting
serverThread.start();
}

// ...
}


Related Topics



Leave a reply



Submit