I want to share this little tip with you because it was a big pain in the ass for me in the beginning.
When adding a new FXMLLoader in your code that should load a new file, it doesn't work in the beginning if you've created that FXML file with an external application, like SceneBuilder for example.
The simple solution is to refresh your project directory or at least the folder/package where you stored your FXML file(s). Right-click -> Refresh or simple F5 (but you'll have to click the package/folder first).
Assuming that nothing else is responsibly for your Exception, now run it and it will load the new FXML file correctly.
Related
(I am very new to coding so let me know if any wording is too vague). I am using Visual Studio Code 1.67.2. I did not use Maven or any other similar tools, I just manually pointed the JavaFX lib folder to my project. I successfully was able to "connect" a css file to my main.java application by creating a .css file and then using scene.getStylesheets().addAll("Main.css"). However, I do not see fxml as an option in the language drop down like I did with css (I just see xml). How can I create a .fxml file? Every tutorial I can find already has a blank .fxml file in the project folder ready to be edited.
Scene Builder
The easiest solution is to download and use Scene Builder, which is a WYSIWYG editor for FXML files. It can create new FXML files for you, and you rarely, if ever, have to deal with editing the FXML file yourself. You just create the UI that you want, and Scene Builder will write the FXML file for you.
Create & Write FXML File Manually
There may be an VS Code extension for this, but I'm not currently aware of one. Though it is not hard to "manually" create an FXML file. Just go to create a new file, type in the name of the file, and then add a .fxml extension. You'll end up with a completely blank FXML file. Then you just need to add two things to make it a minimally valid FXML file:
An XML header.
A root element (with namespaces + any needed import instructions).
<!-- XML header -->
<?xml version="1.0" encoding="UTF-8"?>
<!-- Add/remove/change import instructions as needed -->
<?import javafx.scene.layout.StackPane?>
<!-- Change root element type as needed -->
<StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<!-- Define properties and rest of scene graph -->
</StackPane>
Note: Keep in mind that an FXML file is syntactically just an XML file.
And then you can continue to modify the FXML file as needed. It may help to read Introduction to FXML in order to understand what you're doing.
SceneBuilder
I agree with previous answer left by Slaw. And most specifically the part about getting SceneBuilder from Gluon. It'd be recommended. There really isn't a reason why you should need to edit the FXML file yourself. However, if you do need to generate a new FXML file and do not want to do it through the tools mentioned for whatever reason then you are free to create a file with the .fxml extension and modify it manually.
Ease of Use
In addition, for persons coming here new to VSCode, something else I recommend getting is the
Scene Builder extension, which streamlines navigation from VSCode to Scene Builder.
While this doesn't provide a whole lot more functionality it makes VSCode feel more similar to other Java editors.
I've seen similar questions on Stack Overflow for this issue and have tried their solutions but still did not work. Whenever I try to open a .fxml file with scene builder using the "Open with Scene Builder" option, nothing shows up. Not an error or anything, but the Scene Builder won't show up.
Using:
Eclipse Neon
Windows 10
What I've tried based on Stack Overflow Answers:
Open Scene Builder from desktop application,
Click File --> Preferences --> Reset to Builtin Default Values
then --> Open Recent --> Clear Menu
Still though, I would have the same problem.
One possible solution is to set the path of the executable file of SceneBuilder.exe so Eclipse recognizes where to search the program that will open the .fxml file.
Also is seems strange using SceneBuilder 2.0 when you can have the briefly new SceneBuilder 8 that has more magic into it.
Setting the path on the preferences for JavaFX:
After that you can open the .fxml file just right clicking on it:
The solution was rather silly and quite simple, GoXR3Plus' answer helped me with that. I had the paths already set but I didn't include the Scene Builder 2.0.exe program itself in the path, rather only the folder containing it.
If you're encountering the same problem, make sure your path contains the File.exe.
Thank you everyone for your help.
I have a project made, but I had to copy and move the java source file to another location in our clear case. If I want that file to be changed when I make further changes how would I do that? It is now a new name so I will probably have to make a new project but I'm not sure how to just import that one source file into the new project or even the existing project.
Update:
I have a project with a package that has some classes in it. I moved the java source file somewhere else and now need to use that file in that location instead of the original. I created a new project and imported the source file. Now it doesn't recognize the package or the class. Its says class "example" is public, should be declared in a file named "example". And the package error says "incorrect package". I'm not sure how to fix this to where The project runs just like the other except uses the file from the other location, so it can stay updated
Click on the Projects tab to open up your projects. Right click the project you wish to import source code from and choose Properties down at the bottom. You will arrive at the Sources tab automatically.
Under Source Package Folders click the Add Folder button to add the folder containing your source. Once it's been added, you will see it appended in the table. You can then double click the row's text under the Label column to edit the way it appears in the browsing window.
Importing this way means that you don't need a new project just to include files from other places on your computer. You can simply browse for the files you wish to include!
I've got a small homework project, and for whatever reason Eclipse has decided to stop highlighting syntax or allowing intellisense or goto definitions or anything in this Main.java file. I've tried rebooting it, and the computer, but had no luck.
The thing that really has me confused is that it's still compiling and running the code correctly (eg, changes I make in Main.java are compiled and run - it's not running old Main.class files), so it's still in the build path ... right?
Any ideas?
Right click on the file in the Package Explorer and choose the Open With menu. You will probably find that Text Editor is selected, choose Java Editor to open the file with the correct editor.
The Open With menu selection is remembered so you should get the Java Editor in future, but equally if you choose the plain text editor that is also remembered.
I suggest finding a way to reset any setting you might have set by accident. Not saying that you did it. But just to make sure, just try it out. If you customized the preferences already, you could make some kind of note of the settings, then change it. If that doesn't work, let me know. Good Luck!
Copy the text somewhere, delete the file, and then add a new class (of the same name) and paste the text back.
I wrote some custom composite widgets for a project and compiled them into a separate jar file. I added this jar file to the build path for the GWT project. When I try to add the widgets to the palette (by pressing the "Choose component" button), I can pick the composite widget's class in the list, but I get the following error:
WindowBuilder was unable to load com.test.common.ui.LocationBox. This may be caused by
ClassLoader problems. Do you want to refresh editor and try again?
What am I doing wrong here? Is there something I need to add to the jar to make it load properly?
Sometimes the GWT Designer does not pick up changed classes or is not able to find them.
I would recommend to a full rebuild using the menu "Project->Clean" and then closing and reopening the designer.
If this does not help, try a restart of eclipse before reopening the designer.