Installing and Configuring OpenJFX (Open JavaFX) 13 on Apache NetBeans 11.2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is dr. John Baugh again hopefully you already joined us for the installation of open JDK 13 and Apache NetBeans 11.2 from the previous video this video will focus on ensuring that you can install the open Java FX 13 and also make sure that you can create projects of that type appropriately and also maybe we'll launch a very simple project just to make sure that everything's correct so some of this setup is going to be kind of a one-time thing and then some of it's going to be stuff you have to do every time you create or want to create a Java FX application so I'm going to open up Apache NetBeans 11.2 and once I have this open we'll notice that I have my very simple basic project that I created in the last video which doesn't do all that much it just says hello a world of the console but now we want to do something a little bit more complex as you might recall under the documents on this account I have a Java stuff folder and inside there I have the JDK 13 and also Java FX 13 you have to make sure that they match so JavaFX 13 is right here and it's the open version so we'll have to keep track of the fact that this thing lives right here so I'm going to create a brand are going to create and just a moment a brand new java application and we're going to enable it for the open source version of Java FX and to use the JDK so right now I'm going to go to tools and then libraries and then in here I'm going to create a library to hold all of the Java FX 13 stuff if you're asked you should make sure that the library location is global libraries create a new library and then this I'm gonna just call it JavaFX 13 hit okay and now it's added to the list now currently this has nothing in it so we have to add something so I'm going to go to the add jar folder I'm gonna go to my documents the Java stuff Java FX SDK Lib and you notice there's a bunch of jar files on here and then a source zip you can click and then do shift click down to the bottom of all the jar files but you do not want to include the source zip hit add jar folder and then you're gonna hit OK so that was a one time thing that you need to do on your NetBeans setup or every time you upgrade the version of the open JDK and Java FX you have to kind of redo that make an inline for quite a while so now you have that global library which means all your projects have access to it the next thing is we're going to we have to activate job FX it's gonna be kind of a squirrely strange thing but you're going to go to file new project and you'll notice that it does have the ability to create Java FX projects and then I'm gonna go to Java FX application or FX ml application very important FX ml application hit next and then it'll say activating Java FX now that's the part you want now you'll notice that because this version of NetBeans and because the JDK being version 13 we're getting the same error that has plagued us for ages if you had stayed with version 8 it actually would not give this error and it would let you create a Java FX application the caveat to that is it's using an old version of the JDK and not necessarily old but the commercial version of the JDK through Oracle so it means if you try to make money off of it or create anything that you could possibly make money off of Oracle can sue you or you'll have to at least pay for the pay for the commercial license which is unfortunate so after you've done the activation you can actually cancel it so basically I call that faking out patchy NetBeans the reason we did that is so when we start to add files here it'll actually have a JavaFX category if you didn't do that faking out process like I just did where you pretend like you're about to create a jump effects project and it says activating JavaFX - if you don't do that it's not going to let you see the F XML empty F X and all files and other stuff and there's a lot of other nice features that are still built in and they're integrated into here even if you're not using the version of Java FX that they want you to so once you've done those two things your NetBeans is actually set on whatever system you're at so if you now we want to actually create a project here's the stuff you have to do every time so we're going to create a brand new project and this time we're going to not go under Java exponent or jumbo with ant create a java application hit next I'm going to uncheck create main class I'm going to give this some sort of name that makes sense so I'm going to call it hello FX and hit finish once I do that there's nothing different here there's just the JDK 13 nothing difference between this and a plain old java application because that's what it is at this point now the first thing I need to do is I want this project to have access to Java effects so I right-click the project name that's the thing next to the little coffee cup and then you have to make sure that you do a couple things under here this is so that you have access to Java effects if you try to do anything without doing these following steps that you're going to have a problem you're going to get a lot of errors so the first thing you do is go under libraries category on the compile tab here you're gonna go to the class path and the little plus symbol and then go to add library you're going to add the global library job FX 13 and make sure it appears here still under the libraries category on the run tab you're gonna go to module path and go to add library and we're gonna do the same thing add JavaFX 13 or whatever version you're working with this should work probably for the next few versions unless they change something drastically which is possible but I kind of doubt it they're kind of stabilizing now so that's under the library so you've got the compile tab run tab now go to the run category and under here we have to put this little thing right here this little command to the virtual machine it's called add module add modules JavaFX that controls comma JavaFX dot F XML these are the basic ones we're gonna be working with if you're using F X amount scene builder you need this one if you're doing stuff like anything involving graphics outside of the controls then you'll need to include other things I jump F X dot graphics that's right now this should be good so once you have those things and you know the add modules command here notice it's two hyphens in the front one two and then between add and modules you have a single - space between add modules and the JavaFX dot controls and Java affects that affects ml so we hit OK on that so we've set all that up and now you'll notice under libraries we have not just the JDK 13 but all the Java effects stuff and obviously any of these that you need to use such as F XML and controls like we did before you'd have to put the virtual machine command on the run category for say Java FX dot media or something like that so just be aware of that now when we go under the we've got the default package we're gonna create a new package ok and this one's going to be called hello FX I'm gonna hit finish you can call it whatever you want but you do need something besides the default package that's not just for good software engineering which it is but in here it I found that it doesn't work particularly well without packages and it seems to just work more smoothly with so first thing we're gonna do is create a new Java class this one's going to be a mostly a plain old Java class we're gonna call it just hello FX name it the same as the package written get rid of all the unnecessary Java doc stuff and I'm going to create a main method in here and we're gonna make the Hello FX extend application now it's going to wonder what application is if you had alt enter you should find or click the little thing there you should it's a little symbol you should find that there is an ADD import for Java FX that application death application make sure you do not select the one for calm sunglass UI application you want the one from Java FX once you do that you'll find it gives you another error so this one is because we're claiming we're extending this abstract class application but we have not yet implemented all abstract methods so we're going to click that and it's going to just insert the code for the abstract method start so that was an application we have provided an implementation now this is the little cheesy thing they throw in here it's called an unsupported operation exception so this is just because they don't know how you're gonna implement it and I'm gonna triple click a delete just to get rid of that body and we've got a nice empty body here now usually I put my main underneath start so I'm gonna so I'm going to do that again here but it really doesn't matter where you put it I just prefer it there so inside start or rather inside main all you need to do is right launch and then pass args that you're getting in through main the starts going to be a little bit more complicated but we'll get there in just a second we'll come back to this in just a moment we're gonna create the F XML file next right-click the package go to new other and you notice there is a folder down here called Java FX if you'll remember earlier when we faked out the system by pretending we were going to create a Java FX application it said activating Java FX - well what that does for us we're not using their the JDK 1.8 and going through their system we're using all the open-source products it will create this folder for us which is very useful okay so we can create an empty F XML file here hit next I'm going to give it a name I'm going to call it hello F XML and then when I hit next this is important if you tell it to use a Java controller you can actually tell it to create a new controller which by default it'll be the name of the F XML just with controller at the end you can modify it to whatever you prefer just make sure you're creating a brand new one or if you have an existing one you can point to that but for us since we're starting from scratch we're creating a brand new one you can hit next and do anything you want to with cascading style sheets but usually you can just hit finish no big deal you'll notice now that the F XML file is associated with its controller that controller right here and in fact anytime we update this F XML file we can actually right click here on the F XML and go to make controller and that will update our controller so it's actually pretty snazzy now in NetBeans right now when I double click this I get just the F XML file but I can actually use scene builder I'm going to open up scene builder which I installed previously and inside scene builder I'm just going to navigate through here and open go to open and I'm gonna go find my little project here which is under document snap bean projects and then it's the what is this just hello FX so hello FX I'm going to go under source hello FX and then you notice there's just the hello F XML data XML so I click them there's just an anchor pane I'm going to add to the anchor pain under controls a button and I'm going to add a label the button you'll notice some of the things we want to change on that is the text over here under the inspector there's actually three sub sections on the accordion here properties layout in code we're gonna mostly need stuff under properties just to change the text like click me and then under code you're gonna put on action this one's gonna say we'll just call it handle button but you can call it whatever you want that's going to be the name of the method in the code that will handle the action of clicking the button so on action is usually the default action of whatever the control is in this case clicking the label on the other hand we could clear the text in it so I can delete that and it'll make the text go away so if I click off you might get a little bit concerned you're like how do I ever get back to the label you can do it over here in the scene graph right over here so I just click label and now it's highlighted again but really what we're concerned about over here is under code under code for the label in order to interact with it in NetBeans and provide Java commands related to this we have to go in here and I'll just call it the label we have to give it some sort of FX idea that will be its identifier within the Java code so I'm calling it the label once I've done that I can hit save or go to file file save or hit ctrl s and now I can go back to Apache NetBeans and you'll notice that the ethics of Melon here has been updated right now it does have a problem it says what's handle button I don't know what that is that's because the controller doesn't have a handle button method now you could manually fill all this on yourself but the best way to do it in my opinion this is also another advantage of activating JavaFX when we faked out the system earlier is to right-click hello F XML dot F XML and go to make controller if I click that sometimes it takes a second you might have to wait on it it kind of freezes for a second but then if you let it go you'll notice it will have generated the label and it also generated a handle button and the error and the F XML has gone away now because we have the implemented handle button method so these are really nice handy features here and one thing I can do here and handle button just to prove that this all works is I'll save the label dot set text I'll put hello world on the text now for those of you who might be a little you know very astute and paying very close attention you'll notice that something looks a little bit awry right here because normally you might say oh that's the initialized method that means that's called when this comes in the memory and you'd be correct and we can do any kind of setup we want inside initialized however I'm using this field right here the label and I'm immediately calling set text here's the problem normally when we make fields they get the automatic value if it's a field and it's a reference type meaning it's not one of the eight primitives in Java which is you know the 8 primitive is being byte short int long float double char and boolean if it's not one of those it means it's automatically a reference type and if it's a reference type that holds a memory address and the default memory address that is given to this when you have a field is null so you might think I'm calling set text on null so there's no object living in that memory location so you think this might be a null pointer exception and normally you would be right however the fact that there's the at-at F XML annotation at the top above this says that the F XML system will take care of giving this an object so by the time it gets to handle button this the label will actually be pointing to an object it will hold the address of an object so you don't need to worry about the null pointer exception if you create create your own methods that you're writing in here and then we're not pulled in from scene builder then you need to not put at F XML in front of it this is strictly for the stuff that's generated or associated with what was created in scene builder so let's see what happens run this guy don't see any errors and give it a couple seconds and it should hopefully hopefully hopefully might be a little slow the first time oh I think we forgot something didn't we so it's running and it's it's just kind of waiting they're not doing anything it's cuz we didn't go back like I said we were going to so that's not what we want we're gonna hit stop I'm gonna go to the Java your initial JavaFX file here it's a big part that I've missed here sorry about that guys so on start you're gonna say parent root equals and fix them a loader dot okay it doesn't seem to know what that is so we're gonna say parent and an import for FX of LF XML loader and also for parent and then FX of a loader dot load now you called get class dot get resource and then you have to put the name of the F XML file so this is kind of the final little piece we were missing and that I forgot to put in here got that and then we create a well we don't have to create a stage because the stage is passed in by the system automatically to us we do have to create a scene so the scene we'll just call it C it doesn't know what scene is so we're gonna tell it import that right JavaFX seem that scene I say equals new scene based on the route so this is if you're not using F XML you would not be doing this process you'd be manually creating the scene and adding it to the stage and putting components on it etc so we could say scene dot set title to hello world and then scene oh I'm sorry not scene dust subtitle stage dot set title to hello world stage dot set scene this is important and then the last part that you have to do with the stage is show so I missed all this unfortunately forgot that but hey it's a live television sort of so let's run this see what happens so now I have my little pop-up got my button and when I click it my label should populate with data which it does so hopefully that was useful and this will get you off the ground you can now start adding stuff in scene builder and playing around with the other components so I hope you have a great time thanks buh-bye
Info
Channel: John Baugh
Views: 46,020
Rating: undefined out of 5
Keywords: java, jdk, openjdk, 13, apache, netbeans, install, configure, openjavafx, javafx, openjfx
Id: IdTc_ZIGlMg
Channel Id: undefined
Length: 20min 24sec (1224 seconds)
Published: Wed Nov 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.