Java Video Tutorial 20

Java Video Tutorial 20

FavoriteLoadingAdd to favorites

Code Here:


Best Java Book :

I introduce you to Graphical User Interfaces (GUI) in this part of my Java Video Tutorial. I focus specifically on Java Swing and its components.

We figure out how to display frames, panels, labels, buttons, text areas and more. I also go over the Dimension object and the Java Toolkit, which allows you to ask questions of the operating system.

By |2017-06-08T01:30:15+00:00June 8th, 2017|Java Video Tutorials|50 Comments


  1. Alim Mustafoski June 8, 2017 at 1:33 am - Reply

    Hey Derek 🙂

    Can you please explain the difference between "dim.width" and this.getWidth()". Thx and have a nice day 🙂

  2. imran hussain June 8, 2017 at 1:34 am - Reply

    after extending JFrame , is there any need of using "this " ? if there is , what is it ???

  3. Amrit Sharma June 8, 2017 at 1:35 am - Reply

    these imported library classes are awesome but what if I want to make one on my own or is it really needed…

  4. Suhas C June 8, 2017 at 1:37 am - Reply

    Hello!… I have completed first 19 tutorials in this series…Will it be a problem in understanding few things later on in this series!? If I skip the "Swings" part of this tutorial series (i.e., from Tutorial 20 to Tutorial 30 )…. and continue after that(i.e., from Tutorial 31)?. What is recommended? Any suggestions pleaseeee! ?

  5. Станислав Чесалин June 8, 2017 at 1:38 am - Reply

    Hello Derek! Thank you so much for your explanations. The purpose of calculatin Dimesion parameters through Toolkit class is not quite clear. We have the same result without calculation just using setLocationRelativeTo(null) method, do not?

  6. Totally Anonymous June 8, 2017 at 1:39 am - Reply

    not very good…first of all too fast man…just typing lines of code not really explaining…better tutorials elsewhere

  7. Kiko Maya June 8, 2017 at 1:41 am - Reply

    Hello, Thank you for the amazing video.

    I have a question, how can I import a class ( that I wrote ) that is neither in the same package nor in the same project of the class I am actually writing .

    Have a good day.

  8. the Dark Bright June 8, 2017 at 1:47 am - Reply

    anybody please help me….how to pass an array from one jframe to another jframe..
    Suppose an array which is taken as input from first jframe and I need to work with the array in second how to pass it..

  9. Xuân Anh Đặng June 8, 2017 at 1:49 am - Reply

    can I ask what is this font pls?

  10. Zero 01001100 01110101 01101001 01110011 June 8, 2017 at 1:50 am - Reply

    I am pretty exited to learn more about this topic. This should be fun.

  11. nasr gamer June 8, 2017 at 1:52 am - Reply

    see i have a problem considering this code its not in the video
    ublic class Application extends JFrame {

    public Application() {


    private void initUI() {

    add(new Board());

    setSize(250, 200);


    public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
    public void run() {
    Application ex = new Application();

    now look at the method initUL
    the one who wrote the code
    didnt use an object tooput the values of setTitle and the other methods
    if i was him i would have made an object lets say x
    and did the same thing but like that

  12. Sew Deep June 8, 2017 at 1:53 am - Reply

    Swing is dead? JavaFX is future?

  13. MinecraftMovieMaker June 8, 2017 at 1:53 am - Reply

    Does "this" mean the object of the JavaLessonTwenty??

  14. Sahil Jain June 8, 2017 at 1:53 am - Reply

    Do you need to learn Java Swing if you learn JavaFX?

  15. Hamza el bouti June 8, 2017 at 1:54 am - Reply

    thenks iam huge fun, can u make tutorial about javafx and fxml file please ?

  16. newmagicfilms June 8, 2017 at 1:56 am - Reply

    How you recommand us to learn ?
    1. watch the video and after that download the code and review .
    2. try to keep up with you and write code in the same time

  17. AzSa5z1n.ProGamers June 8, 2017 at 1:56 am - Reply

    Thanks Derek I didn’t know how to use this in Java this helps a lot I am going to continue to watch these!

  18. Gursharanjot Singh June 8, 2017 at 1:57 am - Reply

    watch this videos 2 years ago now they are teaching me using your video in university strange !

  19. pancakehungry June 8, 2017 at 1:58 am - Reply

    Does our method JavaLessonTwenty have to have the same name as our class? And if so why? is it because we said the class JavaLessonTwenty extends JFrame?

  20. Asa Elliott June 8, 2017 at 1:58 am - Reply

    How come we can use "new JavaLessonTwenty();" instead of "JavaLessonTwenty objectName = new JavaLessonTwenty();" ?

  21. SrbijaaBree June 8, 2017 at 2:01 am - Reply

    It’s really pain in the ass when you need to learn Swing after you’ve already learnt JavaFX.

  22. MCforTechnology June 8, 2017 at 2:01 am - Reply

    Hey Derek thanks for the great video, I was wondering if you know how to set the location of a text area in the JPanel wherever you want. Thanks.

  23. Sayan Das June 8, 2017 at 2:01 am - Reply

    I had created a frame of background colour black and when i make a textarea of considerate size of different colour , the textarea is not showing until i click on it…can u suggest some solution??

  24. mladen knezovic June 8, 2017 at 2:01 am - Reply

    GTFO Treehouse !!!!!

  25. jerg70 June 8, 2017 at 2:02 am - Reply

    When I use "requestFocus()" it doesn’t get highlighted, the mouse position is in the requested area but I have to erase everything manually.. is there a solution to this?

  26. Ronan Mac Fhlannchadha June 8, 2017 at 2:02 am - Reply

    I have done some research and it appears Java GUI has progressed from AWT -> Swing -> JavaFX. It appears however most of your videos past this are centered on AWT and Swing. is there much point in learning awt and swing at this point?

  27. Galacticore June 8, 2017 at 2:04 am - Reply

    Took me a while to figure out that you have to replace the add(textArea) with add(theScrollPane). Don’t use both. Maybe that will help some future peeps.

  28. Supetorus June 8, 2017 at 2:05 am - Reply

    Why did you extend JFrame instead of just using it? Doing it that way seems to be common in videos I have watched, is that the way you have to use JFrame?

  29. Shyam Kotla June 8, 2017 at 2:07 am - Reply

    I’m getting an unknown compilation error. Here’s the code

    package tuts;

    import java.awt.Component;

    import java.awt.Dimension;

    import java.awt.Toolkit;

    import javax.swing.*;

    public class Lesson20 extends JFrame {

    public static void main(String[] args){
    new Lesson20();


    public Lesson20(){



    //centers the frame

    Toolkit tk = Toolkit.getDefaultToolkit();

    Dimension dim = tk.getScreenSize();

    int xPos = (dim.width/2) – (this.getWidth()/2);

    int yPos = (dim.height/2) – (this.getHeight()/2);




    this.setTitle("My First Frame");

    JPanel thePanel = new JPanel();

    JLabel label1 = new JLabel("Tell me something");

    label1.setText("New Text");

    label1.setToolTipText("Doesn’t do anything");


    JButton button1 = new JButton("OK");






    this code gives an error in eclipse Indigo SR2.
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at tuts.Lesson20.main(

  30. Rob Baartwijk June 8, 2017 at 2:08 am - Reply

    Hi Derek,

    I am an old school programmer(Cobol, RPG, CLP etcetera) and I have worked on IBM iSeries my whole life. Nowadays there just isn’t enough work for me anymore so about two weeks ago I decided to seriously study Java and get a new direction for my career.

    It is not easy to find proper free tutorials that are actually any good on the web. They tend to oversimplify or actually assume to much knowledge. I have been finding my way and I understand the basics of Java now, but my problem is/was, that I want to test myself by writing an interactive Frame-based application that retrieves and updates the MySql database information that I have created. That means knowing about SQL-usage in Java, about using Swing in Java, etcetera.

    I have just watched this video(Lesson #20) and I am very happy with it. It explains designing frames at such a basic level that I can compare it to my experience with RPG and Display files on the iSeries. Baby steps are what I need and that is what I got from your video. I user Ecllipse by the way, so that makes it easier to follow your instructions. I would advise anyone to go with Eclipse, haven’t seen anything that it cannot do so far.

    Thank you very much for your efforts. If the next videos are just as good I will watch them diligently and learn much faster than if I would just read the manuals.

    Programming in Java turns out to be great fun! 😉


  31. Jiang Kuan June 8, 2017 at 2:10 am - Reply

    this is great, thank you

  32. GobblowGalaxyGamer June 8, 2017 at 2:11 am - Reply

    Hi Derek,

    I’m just wondering, when you import the javax.swing package, it has classes such JLabel that allow you to use those classes in your program, right? If so, can you give me a link on where I can read more about these packages and classes that are inside of these packages?


  33. TheBlue8ird June 8, 2017 at 2:12 am - Reply

    hey Derek, what does the (400, 400) mean? I know it’s the size, but what is the 400? Bytes? How do I know the exact location I want to put my window?

  34. keshav ganguly June 8, 2017 at 2:14 am - Reply

    all worked good till buttons… i mean the label and buttons worked fine but when i tried to add textfield… everything was gone..and it was like a blank frame…then moment i removed the textfield the label and button again worked….

  35. Hasnain Ahmed June 8, 2017 at 2:14 am - Reply

    you should describe tookit further more i can not get it. :/ 🙁

  36. Sahil Jain June 8, 2017 at 2:15 am - Reply

    I didnt understand the step wherein you typed new JavaLessonTwenty();

  37. AKSHAT SHARMA June 8, 2017 at 2:17 am - Reply

    Toolkit tk =Toolkit.getdefault();. u made an object of Toolkit tk. but here u don’t called the constructor u called an in built function how that possible …I think Toolkit tk =new Toolkit();. please explain. I don’t get that

  38. Dan Davis June 8, 2017 at 2:18 am - Reply

    If my UI instructor was 1 part magician and 1 part Elvis (@10:01) he would be Derek Banas. Thank you for this easy to follow video!

  39. vivek kumar June 8, 2017 at 2:19 am - Reply

    thanks for uploading this very nice and knowledgeable video……

  40. Prabha Ch June 8, 2017 at 2:19 am - Reply


  41. MinecraftMovieMaker June 8, 2017 at 2:21 am - Reply

    Does scrollBar combine the textArea with the bar somehow. Cause you only add the bar to the panel but the textArea gets added too. So.. the bar is basically the area?? 😀
    Would make much more sense if you could do something like theArea.add(theScrollBar); and then add the area to the plane.

  42. AKSHAT SHARMA June 8, 2017 at 2:22 am - Reply

    at 2.40 u defined a toolkit as

  43. Youssef Samwel June 8, 2017 at 2:22 am - Reply

    Derek, do you have video that shows how to import a picture in the window?

  44. Steven Gurney June 8, 2017 at 2:24 am - Reply

    Do you have any plans to create a JavaFX tutorial list? I’m still getting comfortable with Swing, but was wondering as JavaFX is gaining more and more use/support.

    P.S. Great videos, thanks for taking the time to do these. Good review and I’m learning a lot of new stuff too.

  45. NIKHIL SINNARKAR June 8, 2017 at 2:25 am - Reply

    All your videos are excellent. they helped me a lot more than any website or book.
    thanks a lot Derek.

  46. Tara Razavi June 8, 2017 at 2:25 am - Reply

    Great tutorial! Thanks!

  47. Romulo Negrillo Jr. June 8, 2017 at 2:27 am - Reply

    Why do you need to put "this.setVisible(true)" at the end of the constructor? In my code I placed it at the top after declaring the size of the frame. The label and button is displayed on the window but when I add the JTextField, it just shows a blank frame, the label and button are gone. But then I place the ""this.setVisible(true)" at the end of the constructor at it works fine now. But I just want to know why that happens, why can’t I just place the "this.setVisible(true)" at the beginning of the constructor?

  48. C Liviu June 8, 2017 at 2:28 am - Reply

    Hi. Why don’ t you have to add textArea1 to thePanel?

  49. Jade of Iearthia June 8, 2017 at 2:29 am - Reply

    I love your videos! Now that that is out of my system, I wanted to make a suggestion: Do you think you could make a JavaCC or ANTLR tutorial? I have been trying to use both, but any documentation is convuluted and confusing. Thanks for these videos you already make, though! They really help n00bs at programming/Web Design like I used to be 😀

  50. Youssef Samwel June 8, 2017 at 2:29 am - Reply

    derek, can i use request focus on anything including jframe. like the windows, buttons, etc

Leave A Comment