Java Video Tutorial 20

Java SwingI 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.

All of the code follows the video and it’s set up to help you retain this information.

If you like videos like this share it

Code From the Video

// Swing allows you to create Graphical User Interfaces
// You need the Swing library to create GUI interfaces
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.*;

// You must extend the JFrame class to make a frame

public class LessonTwenty extends JFrame{
	public static void main(String[] args){
		new LessonTwenty();
	public LessonTwenty(){
		// Define the size of the frame
		this.setSize(400, 400);
		// Opens the frame in the middle of the screen---------------------------------------------------
		// You could also define position based on a component                   |
		// this.setLocationRelativeTo(null);
		// Toolkit is the super class for the Abstract Window Toolkit
		// It allows us to ask questions of the OS
		Toolkit tk = Toolkit.getDefaultToolkit();
		// A Dimension can hold the width and height of a component
		// getScreenSize returns the size of the screen
		Dimension dim = tk.getScreenSize();
		// dim.width returns the width of the screen
		// this.getWidth returns the width of the frame you are making
		 int xPos = (dim.width / 2) - (this.getWidth() / 2);
		 int yPos = (dim.height / 2) - (this.getHeight() / 2);
		// You could also define the x, y position of the frame
		 this.setLocation(xPos, yPos);
		// Define how the user exits the program
		// This closes when they click the close button
		// Define if the user can resize the frame (true by default)
		// Define how the frame exits (Click the Close Button)
		// Without this Java will eventually close the app
		// Define the title for the frame
		this.setTitle("My First Frame");
		// The JPanel contains all of the components for your frame
		JPanel thePanel = new JPanel();
		// How to create a label with its text ----------
		JLabel label1 = new JLabel("Tell me something");
		// How to change the text for the label
		label1.setText("New Text");
		// How to create a tool tip for the label
		label1.setToolTipText("Doesn't do anything");
		// How to add the label to the panel
		// How to create a button -----------------------
		JButton button1 = new JButton("Send");
		// How to hide the button border (Default True)
		// How to hide the button background (Default True)
		// How to change the text for the label
		button1.setText("New Button");
		// How to create a tool tip for the label
		button1.setToolTipText("Doesn't do anything either");
		// How to add a textfield ----------------------
		JTextField textField1 = new JTextField("Type Here", 15);
		// Change the size of the text field
		// Change the initial value of the text field
		textField1.setText("New Text Here");
		// Change the tool tip for the text field
		textField1.setToolTipText("More of nothing");
		// How to add a text area ----------------------
		JTextArea textArea1 = new JTextArea(15, 20);
		// Set the default text for the text area
		textArea1.setText("Just a whole bunch of text that is important\n");
		// If text doesn't fit on a line, jump to the next
		// Makes sure that words stay intact if a line wrap occurs
		// Gets the number of newlines in the text
		int numOfLines = textArea1.getLineCount();
		// Appends text after the current text
		textArea1.append(" number of lines: " + numOfLines);
		// Adds scroll bars to the text area ----------
		JScrollPane scrollbar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		// How to add the panel to the frame
		// Makes the frame show on the screen
		// Gives focus to the textfield
		// You can also request focus for the text array
		// textArea1.requestFocus();

30 Responses to “Java Video Tutorial 20”

  1. punit says:

    nice tutorial as you always give..;-)
    but i have a request to you, i want to know how to create jar files as well as installers for any type of application, please if possible provide some tutorial for that, i never found any video for this.

    Thank You

    • admin says:

      Hi, I’ll definitely do that. Because Java is such a powerful language I hope to create numerous tutorials on all the things it can be used to do. Keep sending me requests and I’ll keep going until I cover most everything. Thank you for taking the time to say Hi 🙂

  2. punit says:

    one more request i have, if possible, make some tutorial for databases (in java swing) also, using derby/javadb, and if possible explain with some projects with installation files as well, ie(how to create installation file/.exe, as in my previous request)..

    Thank you

  3. Pete says:

    Thank you 🙂

  4. Elizabeth vonTauffkirchen says:

    Thanks for this GREAT Java video. This is the first tutorial of yours I’ve viewed but I LOVE it! Thank you so much. I’m sure I will be viewing many more of your videos in the future. I especially love the transcript information with the code below. You helped me tremendously!

    • admin says:

      Thank you very much 🙂 I’m glad you were able to find me. I don’t have that many fans because I make long videos. Always feel free to ask questions and make requests

  5. J3K4 says:

    Hi Derek, great one!

    Could you possible tell me Y is this not the same in NetBeans(it throws me 267 error when try to run it)?
    I tried the code and even the first few lines and no luck, but in Eclipse it works?

    • Derek Banas says:

      I can’t imagine why? What line is causing the error

      • J3K4 says:

        Hi (:

        Well, basically, after typing the first part, following your video, when you run the script I tried running mine and no luck there!
        * To change this template, choose Tools | Templates
        * and open the template in the editor.
        package igra;

        import java.awt.Dimension;
        import java.awt.Toolkit;
        import javax.swing.*;

        * @author user2
        public class Igra extends JFrame{

        private static final long serialVersionUID = 1L;

        public static void main(String[] args) {

        new Igra();

        public Igra(){



  6. J3K4 says:

    Hi (:

    My machine is old and partition that I was saving my projects on, on HDD, somehow is messed up and it only works when I save projects to system partition.. not sure y! But still, SORRY for bothering with this.

  7. Med says:

    Thanks for these.. That’s all really 🙂 I start uni in September and you have held my hand through JQuery, PHP and now Java… Some of the best instruction on the net.

  8. Iresh says:

    Hello..i am a bit new to programming but i have experience in in visual studio..but i have never created gui by hand this seems like much pain for me..i am hoping for an advice from you..what is better to start with because its hard to get rid of bad habits once you start them..should i use a tool or plugin to build gui or should i create them programmatically? thanks in advance 🙂

  9. pa says:

    Great Video’s. Any plans on making videos on SWT and RCP. Appreciate what you do.

  10. danny says:

    Code runs but no scroll bars are shown. I see a dot to the right of the textarea.

  11. danny says:

    Nevermind. YOUR code works, my nesting was wrong 🙂

  12. Alexander says:

    Nice tutorials. I understood a lot about java from you. Thank you for everything. I have been studying at school C++ for 2 years and I think this helps me to understand Java better. Thank you again, you’re awesome. I have a question: how did you learn all this classes? It seems impossible to me. I guess by practicing and coding.

  13. Anonymous says:

    Really, REALLY nice tutorial. I am a fan for sure and I have passed it to me Java class.

Leave a Reply

Your email address will not be published.