Adding JTextFields, JButtons & Tool Tip Elements to a JFrame in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Event-Driven Programming in Java: Definition & Components

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 Java Form Elements
  • 2:16 Tool Tips
  • 2:56 Lesson Summary
Save Save Save

Want to watch this again later?


Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs


In this lesson, you will learn how to add text fields, buttons, and tool tips to GUI applications using Java. Working code examples are provided to illustrate these important features of graphical programs.

Java Form Elements

A graphical application is pretty boring if it just has some labels. Without text fields, buttons, and items like tool tips, it has little use to the end user. In this lesson, we will walk through adding a text box to complete a motto for an old TV show. We'ill do this by adding a text field, a button, and finally tool tips for these elements. Let's go through the different form elements one at a time here.

1. The JFrame

Graphical interaction isn't possible without a JFrame. The JFrame is the foundation for GUI applications that use Swing. Without it, you can't add any other containers or elements. To create a JFrame, see the code snippet below:

JFrame f = new JFrame("New Frame");

2. JTextField

If you want to allow users to sign in or enter any information on a form, you're going to need a text field. In Java Swing, this is a JTextField object. You need to create an instance of the JTextField class and add it to your panel and JFrame.

Creating a new text field is the same as instantiating any object. The code here creates the JFrame, adds a label, and then adds a text field for the user to enter some information:

JTextField textField = new JTextField();
//or you can enter default text into the field
JTextField textField2 = new JTextField("Enter Name");

The next step is to set the boundaries or size of the text box. The method to use is setBounds. This sets the x and y starting position and the height and width. For example, the text box we created previously can be set to start at 0,0, be 150 pixels long, and have a height of 10 pixels as follows:

textField.setBounds(0, 0, 150, 10);

Let's look at a full implementation of our code, creating a text box for the user to enter a value:

import javax.swing.*;
import java.awt.event.*;
public class JFrameElements {
  public static void main(String args) {
   JFrame f = new JFrame("The Twilight Zone");
   //set size and location of frame
   f.setSize(390, 300);
   f.setLocation(100, 150);
   //make sure it quits when x is clicked
   //set look and feel
   JLabel labelM = new JLabel("Not Only of Sight, but of: ");
   labelM.setBounds(50, 50, 200, 30);
   JTextField motto1 = new JTextField();
   //set size of the text box
   motto1.setBounds(50, 100, 200, 30);
   //add elements to the frame

When this program runs, the output is as follows:

JTextField output

3. JButton

Next, we want the user to click a submit button. In a Java JFrame, we can add an instance of a JButton class, which creates a button on the frame as follows in the code below:

//add a button
JButton b = new JButton("Submit");
b.setBounds(50, 150, 100, 30);
//add button to the frame

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit


To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice


Create an account to start this course today
Try it risk-free for 30 days!
Create an account