0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaFX(ローカルアプリ)でJARにしてもcssを読めるようにするには。

Last updated at Posted at 2019-07-27

経緯と試したこと

毎回JARにする度にeclipse上で動くものが動かず、パスかURLに苦しめられます。

SceneBuilder.java
scene.getStylesheets().add(getClass().getResource("../application/application.css").toExternalForm());

上のコードがそうで、eclipseの再生ボタンでテストすると正常にスタイルされます。
でも、実行可能JARにビルドしてクライアントに渡したら動きません。
(だったら開発環境でも動かないでよ・・:sob:
割愛しますが、他にも色々試して、他のサイトの方の成功事例をかたっぱしから試しましたが今度は開発環境ですら動かなかったり。

そもそも、eclipse上でもsystem.out.printlnで調べたパスをそのままターミナルにcat [調べたパス]でエンター押したらcssの中身が表示されるのに、そのパスをそのまま文字列リテラルで代入してもファイルが見つからないと怒られる。何故なのか。Javaのパス本当に難解です。

解決法

で、私の場合の成功事例。
Javaコードには何も書きません。

まず、該当のcssファイルを、そのcssを適用したいfxmlがあるディレクトリに移動します。

次に、**そのcssを適用したいfxmlをeclipseからSceneBuilderを開いて、画面左端のDocumentペインでBorderPaneなどのルートコンテナを選択し、右端のInspectorペインのPropertiesタブでStylesheetsの項目に入力します。**この時入力欄の右の+ボタンからファイラーを開いて選ぶのが無難。

できればcssはcssフォルダにまとめたいのですが、ちょっと動かしただけでパスがズレたりするのでどこまで動かせるかはtry&errorになりますね。詳しい人、コメントください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?