Home » Desktop Java » accessibility » Associating a Label with a Component

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Associating a Label with a Component

In this example we are going to see how to associate a Label with a component in a Java Desktop Application. This is a very important step to consider when developing an application that accepts user input especially with text boxes. You have to, somehow, make it clear to the user what kind of input he should provide in a specific text field. For example, a text field that accepts username should be labeled “username”.

Associating a label with a component is very easy as it only requires that you:

  • Create a class that extends JFrame.
  • Create a new TextField.
  • Create a new JLabel.
  • Use setDisplayedMnemonic('N') to set a mnemonic for the label.
  • Use JLabel.setLabelFor to associate the TextField with the label.

Let’s see the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;

import javax.swing.JLabel;

public class LabelComponentAssociation {

    public static void main(String[] args) {

// Create frame with specific title

Frame frame = new Frame("Example Frame");

/*

 * Create a container with a flow layout, which arranges its children

 * horizontally and center aligned. A container can also be created with

 * a specific layout using Panel(LayoutManager) constructor, e.g.

 * Panel(new FlowLayout(FlowLayout.RIGHT)) for right alignment

 */

Panel panel = new Panel();

// Create a component to add to the panel; in this case a text field with sample text

Component nameField = new TextField("Enter your name");

// Create a component to add to the panel; in this case a label for the name text field

JLabel nameLabel = new JLabel("Name:");

// Set a mnemonic on the label. The associated component will get the focus when the mnemonic is activated

nameLabel.setDisplayedMnemonic('N');

// make the association explicit

nameLabel.setLabelFor(nameField);

// Add label and field to the container

panel.add(nameLabel);

panel.add(nameField);

// Create a component to add to the frame; in this case a button

Component button = new Button("Click Me!!");

// Add the components to the frame; by default, the frame has a border layout

frame.add(panel, BorderLayout.NORTH);

frame.add(button, BorderLayout.SOUTH);

// Display the frame

int frameWidth = 300;

int frameHeight = 300;

frame.setSize(frameWidth, frameHeight);

frame.setVisible(true);

    }

}

 
This was an example on how to associate a JLabel with a component.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of