In this article we will see how to place and remove placeholder (with validation) in JTextfield using focusListener interface in java swing.
For that we will use some java swing components , Listener Interface , Event , Methods Etc.
- setText() : Defines the single line of text this component will display. If the value of text is null or empty string, nothing is displayed.
- getText() : returns the text from the single-line text field.
- setFont() : sets the font and style when displaying text strings.
- setToolTipText() : sets the tooltip of the component to the specified string s. Use : When the cursor enters the boundary of that component a popup appears and text is displayed.
- setBounds(): set the position and size of any component.
- add() : add a component on another component or JFrame.
- setCursor() : sets a mouse cursor shape like hand pointer,Text Pointer,Help Pointer Etc. The hand cursor is mostly used.
- setBackground() : used to change the color of various objects.
- setLocation(): Sets the location of the point to the specified location.
- 1setLayout() : sets the layout manager for the component.
- setResizable(): Indicates whether this frame is resizable by the user. By default, all frames are initially resizable. return : true if the user can resize this frame; false otherwise.
- setVisible() : sets the visibility of the component or JFrame. It is by default false.
- setFocusable() : Sets the focusable state of this Component to the specified value. This value overrides the Component's default focusability.
- setDefaultCloseOperation() : setDefaultCloseOperation() - Exit the application. The default behavior is to simply hide the JFrame when the user closes the window. When we use setDefaultCloseOperation() it that time we will exit the JFrame form.
- pattern class : It is used to define a pattern for the regex engine.
- matches(regex, input) : Compiles the given regular expression and attempts to match the given input against it.
Example :
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.Pattern;
public class placeHolderTextBox extends JFrame implements ActionListener {
//JFrame components declaration start here
JLabel txtLabel1 = new JLabel();
JLabel txtLabel2 = new JLabel();
JTextField txtId = new JTextField("Enter your id");
JTextField txtName = new JTextField("Enter your name");
JButton submitBtn = new JButton("Submit");
JButton clearBtn = new JButton("Clear");
JLabel disAns = new JLabel();
placeHolderTextBox(){
//Label 1 for first value
txtLabel1.setFont(new Font("Arial", 1, 24));
txtLabel1.setText("Enter Your Name :");
add(txtLabel1);
txtLabel1.setBounds(70, 100, 250, 30);
//Label 2 for first value
txtLabel2.setFont(new Font("Arial", 1, 24));
txtLabel2.setText("Enter Your Id :");
add(txtLabel2);
txtLabel2.setBounds(70, 40, 200, 30);
//JTextField 1 for id
txtId.setFont(new Font("Arial", 1, 15));
add(txtId);
txtId.setBounds(350, 40, 240, 40);
//JTextField 2 for name
txtName.setFont(new Font("Arial", 1, 15));
add(txtName);
txtName.setBounds(350, 100, 240, 40);
//Submit button code start here
submitBtn.setFont(new Font("Verdana", 1, 24));
submitBtn.setToolTipText("Submit");
submitBtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
add(submitBtn);
submitBtn.setBounds(200, 200, 150, 50);
//clear button for clear the jTextField text
clearBtn.setFont(new Font("Verdana", 1, 24));
clearBtn.setToolTipText("Clear");
clearBtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
add(clearBtn);
clearBtn.setBounds(360, 200, 150, 50);
//Label 3 for displaying answer or errors
disAns.setFont(new Font("Arial", 1, 24));
add(disAns);
disAns.setBounds(200,290,420,30);
//EventListener code start here
submitBtn.addActionListener(this);
//Define Some JFrame Properties :
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setFocusable(true);
setLocation(450, 200);
setBackground(Color.LIGHT_GRAY);
setResizable(false);
setSize(710, 400);
setLayout(null);
setVisible(true);
//FocusListeneer interface and it's method for txtId textBox
txtId.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
if (txtId.getText().equals("Enter your id")) {
txtId.setText("");
}
}
@Override
public void focusLost(FocusEvent e) {
if (txtId.getText().equals("")) {
txtId.setText("Enter your id");
}
}
});
//FocusListeneer interface and it's method for txtname textBox
txtName.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
if (txtName.getText().equals("Enter your name")) {
txtName.setText("");
}
}
@Override
public void focusLost(FocusEvent e) {
if (txtName.getText().equals("")) {
txtName.setText("Enter your name");
}
}
});
//actionListener interface and it's method for clear button
clearBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
txtId.setText("");
txtName.setText("");
disAns.setText("");
if (txtId.getText().equals("")) {
txtId.setText("Enter your id");
}
if (txtName.getText().equals("")) {
txtName.setText("Enter your name");
}
}
});
}
//Main method start here
public static void main(String[] args) {
new placeHolderTextBox();
}
//actionListener interface and it's method for submit button
@Override
public void actionPerformed(ActionEvent e) {
if (!txtId.getText().equals("Enter your id") && !txtName.getText().equals("Enter your name")) {
if(!(Pattern.matches("^[a-zA-Z ]+$",txtName.getText()))|| !(Pattern.matches("^[0-9]$",txtId.getText()))) {
JOptionPane.showMessageDialog(null,"Something went wrong please try again","Error",JOptionPane.ERROR_MESSAGE);
}
else{
String id = txtId.getText();
String name = txtName.getText();
disAns.setText("Your id is : " + id + " , Your Name is :" + name);
txtId.setText("");
txtName.setText("");
}
}
else{
JOptionPane.showMessageDialog(null,"All filed are required","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
0 Comments
Enter Your Comment