How to Use JavaFX in Eclipse Swing Project - java

I'm wanting to use a few JavaFX components in a Swing app (notably JFXPanel for its HTML 5 rendering support). My app is currently set up in Eclipse 4.3 (Kepler) and I have the latest Java 7 JDK installed on my machine.
This tutorial from Oracle suggests that all you have to do is just refer to a JavaFX class and it'll work in a Swing app. So I type in JFXPanel panel = new JFXPanel(), but Eclipse does not recognize it as a valid Java class. It does recognize other Java 7 classes, like java.nio.
I can't figure out how to get Eclipse to recognize JavaFX classes. This question was previously answered about JavaFX in Eclipse. The first response said that you need to start by creating a new JavaFX project, which I can't do because I already have a fairly developed Swing app. The second is to use the e(fx)clipse plugin, but the tutorial also seems to require this. If there's something special I have to do in order to get a non-JavaFX project in Eclipse to use JavaFX classes, I can't figure out how to do it.

The problem with JavaFX is that it is not on the default classpath so eclipse won't find it (and your exported swing application won't either unless you use the oracle packaging utility!).
So you have 2 options:
a) you install e(fx)clipse and modify the projects Buildpath to include the JavaFX-SDK library.
b) you add the jfxrt.jar yourself to the projects Buildpath (you don't need anything else).
e(fx)clipse would give you extra support for FX development. The version that can be installed into Kepler can be found at http://download.eclipse.org/efxclipse/updates-nightly/site

Related

JavaFX and IntelliJ no errors althought the documentation says I should have some

I'm currently following this tutorial: https://openjfx.io/openjfx-docs/ (JavaFX and IntelliJ IDEA). I use Intellij 2021.3.2.
I have created a project (1. Create a JavaFX project), which worked out great and didn't need to 2. Set JDK 16 because it was already set to 17 (which I guess is fine).
But here begin the weird stuff (and I'm very new to programming so I'm sorry if it sounds silly). "You can also set the language level to 11 or greater." I don't have this option. Because it's "can" I felt like it wasn't a big deal but if someone can explain what this is all about? Just out of curiosity, because I'm at the very beginning of the tutorial and I kinda only understand half of it.
Then 3. Create a library, and I don't have what they have, and my stuff is red:
So when they said "add the JavaFX 17 SDK as a library to the project" I didn't know what to do. I also didn't know what to do to fix the red stuff.
And finally, they say "Warning: If you run now the project it will compile but you will get this error: ..." but if I click on this button
everything works! which you know i'm not complaining about but I would like to understand a bit more what is happening.
And then they suggest I should 4. Add VM options to fix the problem that I don't have. So I figured maybe my computer set the VM options correctly without me knowing it but there is currently no VM option (and it works). Should I add them nonetheless?
Thank you for your help, I'm very lost and feel like I don't really understand anything.
The current documentation in the openjfx tutorial for getting started with JavaFX using Idea is incorrect, at least for recent Idea releases (2021.3 +).
The tutorial is written as though a new Java project was created, rather than a new JavaFX project. Once a new JavaFX project has been created, most of the rest of the steps in the tutorial are either redundant or wrong.
Step 1, “Create a JavaFX project", does a lot more than just “Create a Java project”, which is why everything else is different from the tutorial.
A better tutorial for getting started with JavaFX with Idea, is the official Idea documentation:
Create a new JavaFX project.
Differences between creating a new Java project and creating a new JavaFX project
The new JavaFX project:
Provides a build script for Maven (pom.xml) or Gradle (build.gradle).
Adds the appropriate dependencies for JavaFX base, graphics, controls and fxml.
Provides an example application and controller code that you can run immediately.
The example project is modular and provides a module-info.java
Because the program is modular and dependencies are downloaded via maven and recognized by the IDE, you don’t need to manually configure VM runtime options for the module path and adding modules.
Idea will recognize the Maven or Gradle projects and automatically synchronize the initial transitive dependent libraries with the Idea project.
There are options in the Idea Maven tool window which will allow you to synchronize further changes to dependent libraries or javadoc and source code in libraries.
Selects a JDK and attaches it to the project, automatically downloading the selected JDK version if not already present.
Sets an appropriate language level for the project.
When you just use the create new Java Project option, it doesn’t do any of those things, so you need to do things manually instead, which is what the rest of that tutorial is about:
Manually download the JDK and configure it in the SDK.
Manually download the JavaFX SDK and add the libraries from it to your project.
Manually configure VM modular arguments.
Manually maintain any other dependencies.
Manually associate javadoc and source code.
Manually write the code for a basic application.
Manually set the project language level.
Doing all this stuff manually is more work and more error prone. The manual work usually leads to a worse outcome and a project that is more difficult to maintain for many people getting started with JavaFX, so I do not recommend it.
Versions
In terms of the versions to use, I recommend using the most recent stable (non-beta) releases of both JDK and JavaFX, regardless of what versions may be referenced in any tutorials you may be using.
You can set JavaFX versions in the Maven file generated by the new JavaFX project wizard, then press the button in the Maven Tool window to synchronize the Maven project with the Idea project.
Language level settings
These are important later on, but pretty irrelevant when just getting started.
The language level settings:
Tell the compiler what version of the java byte code to compile the application to.
Tell the IDE what language syntax rules to enforce and provide help with.
If you use Java 17 only features, the app won’t run on a Java 11 VM.
You can define the settings in Idea manually:
language setting level.
But it is better to set them in the compiler section of the maven project and synchronize the project with Idea, which will also configure the settings in the IDE.
IMO, set it to the most recent stable version and have a requirement that your application be run with that Java version as a minimum (you can enforce that by using jlink or jpackage to bundle the JRE version you choose with your packaged app).

Does Eclipse have a GUI builder out-of-the-box?

I'm using Eclipse for Android coding. I wanted to make another version of my application for Desktops, but couldn't find any option to add a JFrame layout or something.
I found out that there's a plugin provided by Google called "Window Builder", and a variety of other plugins, but in Netbeans I don't have to download any kind of plugin, it comes with it.
Does Eclipse have a GUI-building support out-of-the-box (without any plugins)?
No, you will need a plugin for that.
The Eclipse RCP - Rich Client Platform is the library you are looking for, this is a getting started tutorial.
RCP is the eclipse based Swing like library used to build eclipse itself, similar to the Netbeans Platfom.

combining javafx and java se in netbeans

how do you run a javafx and a normal java SE application together.
I tried adding a new javafx class to a java SE project and i get errors. all the javafx imports have error.
I tried adding a new java SE class in javafx project and get the error there is no main while it is there.
Thanks
JavaFX projects are different from Java SE projects. You can sure use JavaFX components in Swing based Java SE applications though. Netbeans supports both JavaFX and Java SE, but if you're working on a JavaFX project, try and follow its rules.
how do you run a javafx and a normal java SE application together.
Follow Oracle's JavaFX <-> Swing Interoperability guide (this guide details integrating WebView with Swing which is what you want).
I tried adding a new javafx class to a java SE project and i get errors. all the javafx imports have error.
One option is to use Java 8, you won't have compilation issues with JavaFX imports. To understand the base reason why this works, see Compile code using JavaFX 2.0 (using command line). Java 8 should work with NetBeans.
NetBeans 7.4 currently has a JavaFX project type File | New Project | JavaFX | JavaFX Application. I don't really know why NetBeans separates these things, but the JavaFX project type in NetBeans does provide a GUI wizard to the JavaFX Ant Tasks which provides a good way to package your application up for distribution.
There might be other JavaFX specific features that are enabled by the NetBeans JavaFX project type, so (as Awfully Awesome recommends in his answer), it would be best to use the NetBeans JavaFX project type rather than a Java SE project type.
I tried adding a new java SE class in javafx project and get the error there is no main while it is there.
Add a main method as suggested by assylias in comments.

GWT Designer not working with Eclipse 4.2 service release 2?

I'm trying to do this tutorial after having installed Eclipse Juno 4.2 service release 2 (Java EE distribution) und following exactly the GWT installation instructions over here.
However, I neither get the WindowBuilder entry under Preferences, nor is there a WindowBuilder entry in the new projects dialog appearing. What am I doing wrong?
I'm running Ubuntu 12.04.02 LTS on amd64, and I have tried oracle jdk 7u17 and Ubuntu's own jdk6 distribution, but to no avail...
Just tried the same in Win2k3. Exact same result. Google is starting to annoy me. GWT 2.5.1 throws an error when trying one of my simplest projects...
Update: it is working to some extent. Meaning: WindowBuilder does not recognize GWT Designer's installation and offers to install GWT Designer for Eclipse 3.7. The designer toolbar's GWT selection possibilities therefore are not there.
Update: bug filed.
Update: bug was closed as won't fix. They don't care.
They do care. GWT is a magical development environment, under constant evolution.
They have to race with new versions of browsers, Javascript and releases of Eclipse, so sometimes tiny things may not be always documented up to date. The tutorial you are trying to run is made for GWT Designer 2.3, GPE 2.3, Eclipse 3.7 & Java 1.6.
This tutorial works also perfectly well for Juno 4.2 SR2 with a few minor changes:
In step 1, just Create a Web Application Project. You can generate project sample code, make sure that things work and then clean it up and stick with the folder layout.
In step 3, just add a class and make it extend com.google.gwt.user.client.ui.Composite. Add an empty Constructor and then you can open the GWT Designer as always, in order to do the rest. The CSS styleName property has been improved. There are tool-tips to guide you.
PS: My tests were made in Ubuntu12.04-32bit, WinXP-32bit and Win7-64bit with JDK1.7.0_17-32bit and Eclipse-32bit.
In case the designer tab does't show up by default, I noticed that I can get it by right-clicking the .java on the package explorer and selecting 'open with ...' 'WindowBuilder Editor'.

Java SWT autocomplete for Eclipse

I'm manipulating SWT library for Java with Eclipse. It's cool however something is missing. Eclipse doesn't propose any thing concern SWT, Ctrl-Space seems losing its magic.
I'd like to know if we can add Autocompletation for SWT and Do you know any cool site to learn SWT
Thanks
If you are creating a project that uses SWT, and you'd like to have all the SWT source and Javadoc available while you're coding, you probably want to use the PDE version of Eclipse. This version comes with all of the source and documentation for all of the SWT, JFace, and Eclipse RCP classes, and provides useful functionality if you are ultimately going to create an RCP application or plug-ins for one.
No auto-completion? That's not supposed to happen. Does your project compile? Do you have the swt.jar in your build path? Does the JDK version work with the SWT version?
Anyway if you want all the documentation, completion etc, if you're using IDE Eclipse, you need both swt.jar and swt.zip - the source. Add swt.jar to build path, tie swt.zip for source and build the project. If still doesn't work try Refresh and build again.

Categories

Resources