In this video we're going to go
ahead and create our first Java
FX applications. So make sure that if
you haven't already, that you've watched
the last video and if you're running on JDK
11, you'll need to go through the steps
to find the global library because
we're going to be using that in this video
Alright so IntelliJ is got a plug-in
that supports Java FX applications
when you installed IntelliJ the plugin
was installed and enabled by default.
So we're going to go ahead and use that
in this project. So you click on create
new project as you normally would and
you'll notice the option here to the left
you probably see that before we've got
Java which we've used traditionally
but there's also this Java FX. We're going
to be using that in this section, every
time and create a new Java FX
application, make sure your project SDK is set to 11
in this case if you're running JDK 11
running JDK 8 obviously it'll be JDK
8. Click on next we need to define a
name. So I'm going to call this one
"HelloWorldFX". So effectively we're going to
be creating our first Hello World
project but using Java FX. We're going to click on
finish now and create the project. Now if
you're running JDK 8, you should find
that this is ready to go and you won't need
to take the steps I'm about to go
through but if you're running JDK 11, click
over to main. You can see right away that there's
some errors. So we've got the errors
and that's because the JavaFX libraries
are no longer part of the JDK. So
we need to actually configure it and set
it up so that the global library
we've defined is used for this project. So
you will need to do the following
steps every time you create a new Java
FX project if you're running JDK 11
The first things first, we want to click
on the project right-click and select
open module settings. Come up to project
Well firstly check the project SDK
is correctly configured, 11 in my case
the project language level needs to be
configured the same as the project SDK
so the same number. In the case of 11
JDK 11, obviously I'm selecting 11
if you're in JDK-8, you would select
8 there. By doing that then if you
come over to modules you should then
follow that because we're using the
project defaults and that's now also set
to version 11. Now for JDK 11 users the next
step we also need to do is go to
global libraries remember that we defined
that in a previous video then you need to
right-click here we need to click on
add to modules. So what that's doing
is adding this module that we've
defined, this global module.
We're telling IntelliJ that we want to
use that in this particular IntelliJ
project. So we click on that. So its a once
only setup and it confirms and it's
going to be added to the selected
modules, which in this case is the
name of our project click. OK. click OK again.
Now you can see that that has actually
fixed some of the import errors. We're
now no longer getting any errors but
you'll find that if you go to run this now
just expand this out so you can see a
little bit better, we will find that we'll
get an error. So I'll click on run we've
got an error there, "JavaFX runtime
components are missing". basically what we've done
so far is we've added the global library
to our JDK 11 application but at this stage
it's not working because we need to
configure it so that it works when we're
running the application as well
and again this is only for JDK 11, but JDK
8 you won't have this issue it should
work out of the box. So what we need to do
to actually fix this is we need to add
a module - info.java file so that
we've defined the JavaFX control so that
will actually work and the way we do that
for JDK 11 is we right-click the
source folder. Noting that I'm not clicking
on the package name. We're clicking
at source level right click select new
we need to select module
- info dot Java and that should open up
a little file like this and by the
way, if you don't see the option here
when you right-click and select new
if you don't see an option there we won't
see module info there now because
you've already created it but prior to
that next what I'll do is just undo it just
to show you
so we cancel that, right-click again new
so if you don't see this module.info.
Java package what that means
is you haven't correctly configured
in project, the project language levels
so 11 is either not set there or set there
because once you do that, that
option should come up for you automatically
so I'm going to go ahead and create that
file again module - info dot Java
and what we now need to do is add a few
commands here so that Java FX will
work. So first one I'm going to type is requires
JavaFX dot it's going to be FXML
that's number one and the second one requires
Java FX dot controls then the last
one we need to add then is opens, then we
go sample. Sample is the name
that we've defined here, basically the package
name. So we specify that there. At that
point we will now find that this application
which previously would have worked
for JDK 8 will now also work for JDK 11
so let's actually give that a test run
and you can see now we've got basically
a GUI interface. There's no controls
on this state and that's normal because
we haven't added any controls to
the application yet. Bottom line is that
it's now working. We've correctly
configured the Java FX library the global library
we've configured and added it to
this project
We've also added the module - info
.Java file so at this point in time it's
now working under JDK 11 and also
you didn't need to make the change to module
info.Java for JDK 8 users but
in your case, they'll also be working
as well. So we're going to finish the
video here now. Now that we know that we've
got an application that's configured to
work for both JDK 8 and JDK 11, moving forward
we can now start focusing on the Java
FX controls and how to actually use them
and not be too worried about which
particular version of Java are that
we're actually working on. So let's start
working on this application and getting
it to do something meaningful in the
next video.