3

I just started to write my first simple gui in javafx and Im trying to add css file to javafx application. It works fine when it isnt maven project. When it comes to maven it cant`t find the file. I have also tried to copy whole path to file, and it was no difference.

I also used scene.getStylesheets().add("SceneStyle.css"); and it was just another error.

I would also appreciate all opinions about design. Is it fine to contain all scenes in 1 class or should I divide it to more files ? If so how to communicate between them.

MainWindow

public class MainWindow extends Application
{
private Stage window;
private MenuBar menuBar;

private BorderPane mainLayout;
private VBox entryLayout;
private HBox lobbyLayout;


public static void main(String[] args)
{
    launch(args);
}

public void start (Stage primaryStage) throws Exception
{
    window = primaryStage;

    mainLayout = new BorderPane();

    prepareMenuBar();
    mainLayout.setTop(menuBar);
    BorderPane.setMargin(menuBar,new Insets(0,0,20,0));

    prepareEntryLayout();
    mainLayout.setCenter(entryLayout);

    Scene scene = new Scene(mainLayout,300,200);
    scene.getStylesheets().add(getClass().getResource("SceneStyle.css").toExternalForm());


    window.setScene(scene);
    window.show();
}

private void prepareMenuBar()
{
    menuBar = new MenuBar();
    Menu options, gameRules, info;

    options = new Menu("Opcje");
    MenuItem display = new MenuItem("Wyświetlanie");
    MenuItem connection = new MenuItem("Połączenie");
    options.getItems().addAll(display,connection);

    gameRules = new Menu("Zasady Gry");
    info = new Menu("Info");

    menuBar.getMenus().addAll(options, gameRules, info);
}

private void prepareEntryLayout()
{
    entryLayout = new VBox();
    Label nicknameLabel = new Label("Pseudonim :");
    TextField userInput = new TextField();
    Button nextScene = new Button("nextScene");

    Board b = new Board();
    b.createPieces();

    nextScene.setOnAction(e -> {
        mainLayout.setCenter(boardPrint(b));
    });

    entryLayout.getChildren().addAll(nicknameLabel, userInput, nextScene);

}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>pl.chinesecheckers.client</groupId>
    <artifactId>client</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>

</project>

SceneStyle.css

.root {
    -fx-background-color: #FFF8DC;
}

EDIT: Im using Intellij and this is projects structure

-src
--main
---java
----GUI
-----MainWindow.java
-----SceneStyle.css
-pom.xml
2
  • try putting main/java/GUI/SceneStyle.css instead of just SceneStyle.css Commented Jan 4, 2018 at 16:58
  • No difference between main/java/GUI/SceneStyle.css and SceneStyle.css Commented Jan 4, 2018 at 17:06

1 Answer 1

4

Have you checked the resulting jar if the .css file is inside it?

I put the .css in resourcess

Your application

The ideal is to create a class for each scene, but if there is a need for more than one in a class, I do not see why not.

Sign up to request clarification or add additional context in comments.

2 Comments

You are right - jar doesnt have it. How can i add .css file to it ?
follow his structure, you need to add resources directory and inside you need to create the same directory structure of your package

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.