Use modena.css with JavaFX2 and Java 7 - java

I am developing a JavaFX application. Unfortunately, I am restricted to Java 7, thus also restricted to JavaFX2. However, I would like to include the default theme from JavaFX 8 Modena. Hence, I downloaded the modena.css and put it into my src/main/resources/ folder. Now I would like to set this css as my default theming.
Seeing How to set JavaFX default skin I tried setting
System.setProperty( "javafx.userAgentStylesheetUrl","src/main/resources/modena.css")
but the styling wasn't any different.
How can I set another default css file other than caspian using Java 7 and JavaFx 2?

src part of the path looks wrong. You need to provide the path which is valid at runtime, check your build or target folder to see what is it.
Most probably right path is main/resources/modena.css
You can also write next in your code to see if path is correct:
System.out.println(getClass().getResource("main/resources/modena.css"));
if it's null — your path is wrong.

Related

Java getResource is in wrong path

I hope someone can help me here, becouse I'm fighting with a problem for some time. In my main class I use this command:
System.out.println(getClass().getClassLoader().getResource("org"));
The problem I've got is that it returns:
file:/E:/Tmp/ExamplePr/PROJEKT/proj/build/classes/java/main/org
instead of:
file:/E:/Tmp/ExamplePr/PROJEKT/proj/build/resources/java/main/org
The problem is that it goes into classes directory instead of resources dir. As a result I can't have access to my .fxml files I need. I'm using gradle for build and currently working with JavaFX. I've tried something like:
System.out.println(getClass().getClassLoader().getResource("/resources/java/main/org"));
But I just got null :(
Do you know any method to force him to use absolute path or to look for resources in resource filder or even use something like to use "../" from linux to go up. I dodn;t find any of this
The root of your resources tree is defined by the classloader (as described in the JavaDoc). You can define the root by explicitely setting it in your classpath or preferably by using a build tool like maven and following the conventions set and used by the tool. For maven projects the root would usually be at main/java/resources.
getResource will always return the first match in the class path. So if you specify E:/Tmp/ExamplePr/PROJEKT/proj/build/resources/java/main before E:/Tmp/ExamplePr/PROJEKT/proj/build/classes/java/main in your classpath, you will get what you want.
That said, the resources are usually meant to be copied with the classes, and sometimes both are packed in a jar file, so you shouldn't worry about it.
With JavaFX use FXMLLoader;
FXMLLoader.load(new URL(getClass().getResource("/fxml/myfxml.fxml").toExternalForm()));
Make sure to pass the platform appropriate separator and use a relative path.

How to use SceneBuilder's DarkTheme in my application

Firstly, is it legal to use the dark theme of JavaFX's SceneBuilder 2 in my application? Since it is open source now here: SceneBuilder/css
Secondly, how to do this if legal? Or just for training purposes if not legal?
I tried to download the ThemeDark.css file from the link above and add it to my fxml file, but i see no change applied.
Any ideas ?
Edit (what I did):
I have downloaded the .css file and pasted it in package css.
Then I added these lines in my .fxml file (with the <> symbols but I removed them in this question as they hid the text if present) :
stylesheets
URL value="#/css/ThemeDark.css"
/stylesheets
See below resulting screenshot: (themeDark not applied)
If you are using SceneBuilder you can add a global css sheet to it and be happy with the new look.
Alternative, if you want to set it within your code you can use
scene.getStylesheets().clear();
scene.getStylesheets().add("path/stylesheet.css"); // Modify to your path
to add a style-sheet. Notice that you should put your style-sheet in a resource folder in your application, just to keep everything cleaned up.

Eclipse: Use environment variable for external jar location

After hours of unsuccessful googling, I ask you:
Situation:
My Eclipse Project uses "nedded.jar" so I added it to the build path. No problem.
C:/dev/development/my_needed/nedded.jar
But the location of "nedded.jar" is relative to the environment variable DEVELOPMENT ( =C:/dev/development/ ) and therefore may change. So, I need my Referenced Library path to be:
%DEVELOPMENT%/my_needed/nedded.jar
I could not find the syntax to accomplish that. Ideas?
EDIT:
Maybe I did not make myself clear enough: This Project is developed by MSVisual Studio(C++) and Eclipse(Java). Both are started from the Console. A prior executed script sets 3 major variables: DEVELOPMENT, RUNTIME, SOURCES to certain, changing paths.
If I then start Eclipse from this shell, the path to my external libs shall be defined by %DEVELOPMENT%\my_needed\nedded.jar.
I have found a half way solution (somewhere): added a new folder --> advanced --> link to alternate location (linked folder).
this adds you an entry in your .project, which I also get, when I checkout my project:
<linkedResources>
<link>
<name>lib/RXTXcomm.jar</name>
<type>1</type>
<locationURI>PARENT-4-PROJECT_LOC/Development/rxtx/RXTXcomm.jar</locationURI>
</link>
</linkedResources>
Nice so far, but it still does not depend on %DEVELOPMENT%. What I need is:
%DEVELOPMENT%/rxtx/RXTXcomm.jar
Hope this is now clearer.
You should declare a variable (Java Build Path -> Add Variable... -> Configure Variable ... -> New) to set the changing path on each system (e.g. FOO_BAR_HOME).
Then you can add the variable to the Libraries section and edit it to point to your library, like:
%FOO_BAR_HOME%/lib/foobar.jar
Take a look at the existing variables for usage.
Alternatively you can place the library inside the project (e.g. subfolder 'lib'). If you add the library from this location ('Add Jars...' NOT 'Add External Jars...') it will be added by relative path.
I was searching for an answer to this as well with ant. Seems you can reference windows environment variables like so
<property environment="env"/>
Provide all environment variables as Ant properties prefixed by "env.". For example, CLASSPATH would be accessible in Ant as ${env.CLASSPATH}.

Intellij can't find .png

My intellij can't find local .png images.
private String craft = "craft.png";
ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
The .png is located in the same directory as the java files. I don't understand why it isn't working. Using maven build, tried alternating from resources to java, but still no luck :(
craft.png must be placed into src/main/resources, otherwise it will be not copied to the classpath according to the Maven rules. See this answer for more details.
Your code should be also changed to:
private String craft = "/craft.png";
Here is the sample working project.
Go to your IntelliJ Preferences and search for "resource patterns" (or just go straight to the "Compiler" settings).
IntelliJ will only copy certain resources to the output directory. Make sure the resource pattern includes *.png.
I have my resource pattern set to !*.java (copy everything that's not a source file) which seems to work fine (and should really be the default, in my opinion).
tried alternating from resources to java
So at first you tried putting craft.png into src/main/resources. That is where it must be put according to Maven (not in src/main/java).
But it didn't work because
this.getClass().getResource("craft.png") tries to find "craft.png" relative to the this.class's package. If your this.class is in package foo.bar then you must put craft.png in src/main/resources/foo/bar/
You can also provide an absolute path in getResource() by using a leading slash /. For example put craft.png into a custom folder under resources src/main/resources/customFolder/ and read it with the leading slash / in front of customFolder:
this.getClass().getResource("/customFolder/craft.png")
If you don't use leading slash in getResource() method then internally class's package name is prepended to the resource name to make it absolute.
This behavior is explained in Class.getResource()

how to overcome this exception

i am using a third part SDK with my java application.The providers of sdk provided me exe file that i installed and one java project.I installed the exefile.
Now when i run the code i get a dialog box showing error
Excepting a absulut path for library AKSSDK.dll
No AKSSDK in java.library.path
could not load load library AKSSDK
how do i resolve it?
You need to run java with the following configuration:
java -Djava.library.path={where your library is}
Note the above is the directory where your library is, not the full path name of the library!
You have to add AKSSDK.dll to your PATH environment variable.
It would look like this:
echo %PATH%
C:\xyz\;C:\other\etc\etc;C:\Your\Path\To\AKSSDK.dll
EDIT
To modify your environment variable you have to go to:
MyComputer/RightClick/Properties/Advanced/EnvironmentVariables
(source: vlaurie.com)
And modify the existing Path under System variables
See this tutorial for more details: http://vlaurie.com/computers2/Articles/environment.htm
I have had problems with the white space of ( Program files ) in the past. If possible install your SDK on something like C:\SondaSDK or C:\You\SondaSDK
That way you shouldn't have problems.
You can manually set the path to this value by starting with
java -Djava.library.path=PATH_TO_LIBRARY

Categories

Resources