jTextField in swing

jTextField in swing

I am new to java and i am making a registration form.i just want to know if there is any code in which a string cannot be inserted in a jtextfield where a phone number has to be inserted.the jtextfield should not respond to any character in jtextfield.plzz help

View Answers

December 4, 2012 at 3:01 PM

Here is an example that accept the numbers only through the jtextfield.

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

class TextValidation extends JFrame{
JTextField text;
JLabel label;
JPanel p;
TextValidation(){
text=new JTextField(15);
label=new JLabel("Enter number:");
p=new JPanel();
p.add(label);
p.add(text);
text.addKeyListener(new KeyAdapter(){
            public void keyPressed(KeyEvent e){

                char ch = e.getKeyChar();
                if(Character.isDigit(ch)){
                }
                else{
                    JOptionPane.showMessageDialog(null, "Only numbers are allowed!");
                    text.setText(" ");
                }
            }
});
add(p);
setVisible(true);
setSize(300,100);
}
public static void main(String[]args){
TextValidation v=new TextValidation();
}
}









Related Tutorials/Questions & Answers:
jTextField in swing
jTextField in swing  I am new to java and i am making a registration form.i just want to know if there is any code in which a string cannot be inserted in a jtextfield where a phone number has to be inserted.the jtextfield should
JTextField Validation in java swing
JTextField Validation in java swing In this tutorial, you will learn how to validate jtextfield in java swing. Here we have created a textfield on frame... extends JFrame{ JTextField text; JLabel label; JPanel p; TextValidation(){ text
Advertisements
JTextField
JTextField  simple program for text field cannot not be edited in java swing
SWT_AWT bridge ,jtextfield edit problem - Swing AWT
SWT_AWT bridge ,jtextfield edit problem  Hi All, I am using SWT_AWT bridge in my code to embed swing components in to SWT Composite. I am using SWT_AWT bridge frame and adding JTextfield to it, My problem is that the JTextfield
jtextfield
jtextfield  hii..is it possible to make a jtextfield in login page in such a way that some text should already be inserted like username and it should be displayed light in color and when a user enters some text then it should
navigating the value of JTextField from one JFrame to another - Swing AWT
navigating the value of JTextField from one JFrame to another  hello I m doing one project where I m retriving the data from databse in JTextField... button all the details of an item of JTextField will be displayed in another
navigating the value of JTextField from one JFrame to another - Swing AWT
navigating the value of JTextField from one JFrame to another  Hello...(JTextField of JForm1).. can you plz help me regarding this... I will be thankfull... JFrame{ JLabel label1,label2; JTextField text1,text2; JPanel panel; JButton
Jlist and JTextfield
Jlist and JTextfield  How can we filter values from jlist by adding only a single letter in jtextfield such that when letter S is pressed in jtextfield then jlist should diplay all the values starting from letter S.I am using
java jtextfield
java jtextfield  how to validate jTextField which accept only number or alphabets from keyboard....plez do help me
Sum of a Number using Swing
Sum of a Number using Swing       In this section, you will learn how to sum of a number using swing. JTextField, JButton is a component of GUI. Brief description of the Component
validate jtextfield in two different class use keylistener
validate jtextfield in two different class use keylistener  validate jtextfield in java swing will use two class, design part in one class and method calling in one class will use Keylisteners or change listeners
SWING
SWING  A JAVA CODE OF MOVING TRAIN IN SWING
swing
swing  How to make swing component auto-resizable when JFrame resize
swing
swing  Write a java swing program to delete a selected record from a table
float validation for jTextField
float validation for jTextField  Hi; I try that users enter only float number into jtextfield but I can not handle it. I try regex in keylistener but I couldn't be successful. Can you help me please
JtextField - Java Beginners
JtextField  Hi, How can I populate a jtextfield of another Jframe...; JLabel label1,label2; JTextField text1,text2; Form() { label1 = new JLabel(); label1.setText("First Name:"); text1 = new JTextField(20
Copy data from one JTextField to another JTextField
and then paste into another JTextField. JTextField is used in the swing... You can use JTextField into your swing applications to develop data entry... Copy data from one JTextField to another JTextField
Swing
Swing  Write a java swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
Swing
Swing  Write a java swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
easy way to make a JCombobox look like a JTextField?
easy way to make a JCombobox look like a JTextField?  Is there a good(and easy) way to make a JCombobox look like a JTextField? By this I mean.... works   Autosuggestion box in Java Swing
java swing - Swing AWT
java swing   how i can insert in JFrame in swing?  Hi... label1,label2,label3,label4,label5,label6; JTextField text1,text2,text3,text4,text5,text6; label1 = new JLabel(); label1.setText("Name:"); text1 = new JTextField(20
Java JTextField Validation - Java Beginners
Java JTextField Validation  Hello Sir ,How I can Validate JTextField In Java,when I enter Any Character value in JTextField ,I want To Display MsgBox... Validation extends JFrame{ JTextField text; JLabel l1,l2; JPanel p
How to return a string from a JTextField
How to return a string from a JTextField  Hi, Am new to java, and am trying to get a string from a JTextField so that I can compare it with another...(); final JTextField text=new JTextField(20); JButton b=new JButton
swing - Swing AWT
swing   how to create JTabbedPane.when i choose one jtabbedpane it will appear JTextField,resultMessages.pls give me any examples  Hi... JLabel(); label.setText("Name"); JTextField text =new JTextField(15
SWING - Swing AWT
,JTextfield in JFrame in swing?  Hi Friend, Try the following code...; JTextField text; JButton button; JPanel p; JFrame f; BufferedImage image..."); text= new JTextField(20); button= new JButton("Submit"); p=new JPanel
Java swing
Java swing  how to create simple addition program using java swing... JLabel("Enter first number: "); final JTextField text1=new JTextField(20); JLabel lab2=new JLabel("Enter second number: "); final JTextField text2=new
Swing - Applet
Swing  Hello, I am creating a swing gui applet, which is trying... JTextField { public static void main(String[] args) { JFrame f = new...(f.getContentPane(), BoxLayout.Y_AXIS)); final JTextField text1 = new JTextField(20
update data to mysql database through JTextField
update data to mysql database through JTextField  I am getting an error, when i am updating a data to mysql database through JTextField. Send me the code used to solve my error. public void update(){ try
java swing
java swing  what is code for diplay on java swing internal frame form...("Address"); final JTextField text1=new JTextField(20); final JTextField text2=new JTextField(20); panel.add(label1); panel.add(text1
Java swing
Java swing  when i enter the time into the textbox and activities into the textarea the datas saved into the database.the java swing code...("Activities: "); final JTextField text=new JTextField(20); final
Swing - Swing AWT
/java/swing/get_jtextfield_value_from_anothe.shtml Thanks
Swing Program
Swing Program  write a java swing program to get username... extends JFrame{ JButton SUBMIT; JLabel label1,label2; final JTextField text1,text2... = new JLabel(); label1.setText("Username:"); text1 = new JTextField(15
java swing - Swing AWT
java swing  how i can insert multiple cive me exampleolumn and row in one JList in swing?plz g  Hi Friend, Please clarify your question. Thanks
swing-awt - Swing AWT
swing-awt  Hi, Thanks for replying to my question...I'm getting some confusion to add action events in my application(Rich Text Editor).How to add action events? Thank U
What is Swing?
What is Swing?  What is Java Swing? Where to get the tutorials of Swing? I am beginner in Java Swing programming and trying to find the tutorials of Swing. Tell me the urls to learn swing. Thanks   Hi, Swing is Java
swing/awt - Swing AWT
swing/awt  How to create richtexteditor using swings...?I'm very much new to swings....It's urgent.....Thank u...   hello read this book you get idea; JFC Swing Tutorial, The: A Guide to Constructing GUIs, Second
Swing paint - Swing AWT
Swing paint  hi, i want to print something on window using swing applet.. m doing dis.. protected void paintComponent(Graphics g... the Swing Applet, use html file with the following code: Java Applet Demo
swing sms
swing sms  HOW TO SEND SMS MESSAGE FROM SWING USING SQL DATABASE
Swing Program
Swing Program  Write a java swing program to calculate the age from given date of birth
SWING FRMES
SWING FRMES  hai SIR? HOW TO DESIGN swing Frames send source code
SWING FRMES
SWING FRMES  hai SIR? HOW TO DESIGN swing Frames send source code
swing to applet
swing to applet  hi how i can isplay a java swing into applet java thanks
java swing - Swing AWT
{ JButton insertButton; JTextField text1,text2; JLabel label1,label2,label..."); insertButton.addActionListener(this); text1 = new JTextField(15); text2 = new JTextField(15); label1 = new JLabel("Name:"); label2 = new JLabel("Address
swing question
swing question  please provide me a program with oracle conectivity... extends JFrame{ JButton SUBMIT; JLabel label1,label2,lab; final JTextField... JTextField(15); label2 = new JLabel(); label2.setText("Password:"); text2
java swing - Swing AWT
java swing  how to add image in JPanel in Swing?  Hi Friend, Try the following code: import java.awt.*; import java.awt.image....: http://www.roseindia.net/java/example/java/swing/ Thanks
Java swing
Java swing  what are the root classes of all classes in swing
Java swing
Java swing  Does Swing contains any heavy weight component
AWT & SWING
AWT & SWING  What is diffennce between AWT & SWING
java swing
java swing  view the book details using swing
printing in swing
printing in swing  How can i print payslip in swing

Ads