Working with labels and text fields

Working with labels and text fields

Posted in : Core Java Posted on : October 23, 2010 at 6:41 PM Comments : [ 0 ]

This section contains the detail about the Swing's labels and text fields in java.

Working with labels and text fields


JLabel is used to create text labels. A JLabel object provides text instructions or information on a GUI . It displays a single line of read-only text, an image or both text and image. We use a Swing JLabel when we need a user interface component that displays a message or an image.

Example :

In this example, we add three label in the Frame. First label has "text as well as image" while the second one has only text. The third one contains only image as label.

package corejava;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

public class JlabelTest extends JPanel {
	JLabel jlbLabel1, jlbLabel2, jlbLabel3;
	public JlabelTest() {
	ImageIcon icon = new ImageIcon("E:/eclipse_programs/CORE/src/corejava/logo.gif",
				"My Website");
		// Creating an Icon
		setLayout(new GridLayout(3, 1));
		// 3 rows, 1 column Panel having Grid Layout
		jlbLabel1 = new JLabel("Image with Text", icon,JLabel.CENTER);
		// We can position of the text, relative to the icon:

		jlbLabel2 = new JLabel("Text Only Label");
		jlbLabel3 = new JLabel(icon); // Label of Icon Only
		// Add labels to the Panel
	public static void main(String[] args) {
		JFrame frame = new JFrame("jLabel Usage Demo");
		frame.addWindowListener(new WindowAdapter() {

			// Shows code to Add Window Listener
			public void windowClosing(WindowEvent e) {
		frame.setContentPane(new JlabelTest());


Output :


JTextField is an input area where the user can type in characters. JTextField allows editing/displaying of a single line of text. New features include the ability to justify the text left, right, or center, and to set the text?s font. When the user types data into them and presses the Enter key, an action event occurs. If the program registers an event listener, the listener processes the event and can use the data in the text field at the time of the event in the program.

Example :

package corejava;

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

public class JTextFieldDemo extends JFrame{
	//Class Declarations
	JTextField jtfText1, jtfUneditableText;
	String disp = "";
	TextHandler handler = null;
	public JTextFieldDemo() {
	super("TextField Test Demo");
	Container container = getContentPane();
	container.setLayout(new FlowLayout());
	jtfText1 = new JTextField(10);

	jtfUneditableText = new JTextField("Uneditable text field", 20);
	handler = new TextHandler();
	setSize(325, 100);
	//Inner Class TextHandler
	private class TextHandler implements ActionListener {

	public void actionPerformed(ActionEvent e) {
	if (e.getSource() == jtfText1) {
		disp = "text1 : " + e.getActionCommand();
	} else if (e.getSource() == jtfUneditableText) {
		disp = "text3 : " + e.getActionCommand();
	JOptionPane.showMessageDialog(null, disp);
	//Main Program that starts Execution
	public static void main(String args[]) {
		JTextFieldDemo test = new JTextFieldDemo();


Output :

After executing program :

When we press enter after entering text into text box, we get :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics