Select Page

We have learned a lot about how to put different kinds of things into our desktop applications but what if we want to add some text to our applications; I know we have already covered a way to do that with a Jlabel but today I will be showing you a way which is a little different. We will be using an object called a JTextField. It even has the word text in its name, so it has to be great, right? Ya, of course it is! I am going to assume everyone is as excited about JTextFields as I am.

Have you ever wanted to have a user be able to input characters back into your application, and have the user actually interact with your application? I bet you’re probably thinking something like “well ya duh, Christian”. I would awesome so, or else why would we write beautiful applications for our user, but I guess we can have an application with just button interaction but that’s beside the point. Anyways, that’s where JTextField comes in because it’s an empty text box where your user can type in characters.

JTextField Example:

import java.awt.Dimension;
import javax.swing.*;
/**
 *
 * @author christian
 */
public class JTextFieldExample1 {
    
    public static void main(String[] args) {
        // Variables: 
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JTextField textBox = new JTextField();
        
        // Settign the size of the JTextField:
        textBox.setPreferredSize(new Dimension(20,20));
        
        // Setting JFrame properties:
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setTitle("JTextField Example");
        
        panel.add(textBox);
        frame.add(panel);        
    }
}

This simple code will bring up an empty textbox on your application. I challenge you to try out the code and you should see something like this:

The little white box is the JTextField. All by itself, it might not be very interesting, but it’s customizable. One of the many things that can be done with it is the size of the box can be changed. Take a look at the JTextField Example code above can you spot what line of code is the one where we set the size of JTextField? Okay, I assume you already took a look at the code, and if you guessed the line that contains ” textBox.setPreferredSize(new Dimension(20,20));” then you’re right! If you read the comment above that line then you’re a smart person. 🙂 But if you’re reading this then you’re already a smart person. Okay now that we have settled that argument lets keep on learning. Another feature that in my opinion is really cool is the ability to have the JTextField have text inside that the programmer(aka you) predefined. Take a look at the next code example.

JTextField Example With Predefined Text:

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


/**
 *
 * @author christian
 */
public class JTextFieldExample1 {

    
    public static void main(String[] args) {
        // Variables: 
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JTextField textBox = new JTextField();
        ImageIcon image = new ImageIcon("//home//chris//Documents//Nerdy_Rocks//JTextField_Post//Lion.png");
        JLabel imageLabel = new JLabel(image);
        
        // Adding text to JTextField:
        textBox.setText("Are you having fun programming? :)");
        
        // Setting JFrame properties:
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setTitle("JTextField Example");
        // Adding components to JPanel:
        panel.add(imageLabel);
        panel.add(textBox);
        // Adding JPanel to JFrame:
        frame.add(panel);        
    }
    
}

Do you notice something cool about this code? We are using a JLabel in our application. This code should generate an application looking like this:

Take a look at the code again and take a look at where we set the text that should appear in the JTextField every time the application is opened. I know this seems like the same thing as adding text with a JLabel, but if you take your mouse and click on the box a cursor will appear and it will actually allow the user to delete that text and enter his or her response to the question. Obviously, everyone is going to say yes to the question. But wait there is more! The JTextField’s color can also be changed.

JTextFied Example 3:

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


/**
 *
 * @author christian
 */
public class JTextFieldExample1 {

    
    public static void main(String[] args) {
        // Variables: 
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JTextField textBox = new JTextField();
        ImageIcon image = new ImageIcon("//home//chris//Documents//Nerdy_Rocks//JTextField_Post//Lion.png");
        JLabel imageLabel = new JLabel(image);
        
        // Adding text to JTextField:
        textBox.setText("Are you having fun programming? :)");
        // Setting color:
        textBox.setForeground(Color.yellow);
        textBox.setBackground(Color.red);
        
        // Setting JFrame properties:
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setTitle("JTextField Example");
        // Adding components to JPanel:
        panel.add(imageLabel);
        panel.add(textBox);
        // Adding JPanel to JFrame:
        frame.add(panel);        
    }
    
}

The more I mess around with JTextField the more interesting things I find. Feel free to mess around with the code above. Not only am I encouraging you to mess with JTextField but I expect you to find a favorite feature. Leave a comment about the most interesting JTextField that you’ve found, or the feature you enjoy the most.