0:03 hi this is dave from javacodejunkie.com
0:06 and welcome to the first in a new series
0:07 of tutorials on
0:11 java swing in this video we're going to
0:12 write our first
0:15 java swing gui program so let's get
0:17 right to it
0:20 let's go to eclipse and create a new
0:22 java project i'm going to call this
0:32 for my first swing project click finish
0:38 and then i'm going to create a new package
0:39 package
0:50 click finish and i'm going to create
0:54 two classes the first is going to be
0:56 a class that i always use to start a
0:57 program and it's called launcher
1:05 i'll create a main method in that
1:08 click finish and then the second
1:18 no main method and click finish
1:21 so let's go back to our launcher class
1:23 and in the launcher class
1:24 we're going to use a method from the
1:26 swing utilities class
1:38 the invoke later method requires as a parameter
1:39 parameter
1:43 a an instance of runnable the swing
1:45 utilities.invokelater method
1:46 will ensure that all of our swing
1:48 related code is run
1:50 on the same thread and that thread is
1:52 called the event
1:54 dispatch thread and it's done that way because
1:55 because
1:58 swing is not thread safe so
2:01 all swing code has to run in the same thread
2:01 thread
2:04 so we'll do this once and we never have
2:05 to think about it ever again so i'm
2:07 going to create
2:18 and then we will override the run method
2:19 which is the only
2:21 method specified in the runnable
2:23 interface and in that method what we're
2:24 going to do
2:27 is create a new instance of our
2:44 and then i'll do a main dot show
2:45 now this has not yet been written so
2:47 we're going to get an error at this
2:48 point but
2:49 let's not worry about that we're going
2:52 to take care of that right away
2:55 we'll move now to the main window class
2:57 and in the main window class i'm going
2:59 to create an instance variable
3:02 of type jframe and jframe is the top
3:18 organize the imports create a constructor
3:32 and we'll also create a method called show
3:43 so now if we go back to our launcher we
3:44 no longer have an error
3:47 so in our constructor i'm going to create
3:48 create
3:58 i'm going to use a method of the jframe
4:14 i'm going to set an option that defines
4:16 what's going to happen when we click the
4:18 x in the top right hand corner of the
4:21 window to close the window the default
4:22 is to
4:24 do nothing and what it actually does is
4:26 it just hides the window so what we want
4:28 to happen when we click it
4:31 is to close or dispose of the window
4:38 default close operation
4:42 j frame dot
4:45 dispose on close we're going to set
4:57 we'll make it 800
5:04 and to center the window on the screen
5:10 we'll call another method
5:13 set location relative to and setting
5:16 it relative to null actually centers it
5:17 on the screen
5:20 and in the show method we'll simply say window
5:22 window
5:26 dot set visible true
5:29 and now if we run we should see a window
5:32 with a title that says hello world
5:35 in this video tutorial we created our
5:36 first java
5:38 swing program if you enjoyed the
5:40 tutorial please click the thumbs up to
5:42 like the video
5:44 also please consider subscribing to the
5:45 channel to view
5:48 more java swing videos thanks for
5:49 hanging out with me today
5:52 and until next time stay safe and keep