[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

set JTextfiled digit value

Posted on 2007-07-31
15
Medium Priority
?
264 Views
Last Modified: 2012-06-22
hello there,

i have a textfield and dont want users to enter more then 10 digits and less then 10 in the textfield.in short it short take 10 digit phone number
0
Comment
Question by:zolf
15 Comments
 
LVL 92

Expert Comment

by:objects
ID: 19598491
you can use a DocumentFilter to achieve that.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 19598493
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 1600 total points
ID: 19598499
there are number of ways, the below link describes three possibilities KeyListener, InputVerifier, Document...

http://java.sun.com/developer/JDCTechTips/2001/tt1120.html

I used to do the validation in insertstring of the document!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:zolf
ID: 19598510


hi objects,

thanks for your comment.can you please tell me how can i add this in my code below.without changing a lot.i want to have this in the textfileld JTextField  ADSLNoTF = new JTextField(); below.
please help

public class AddNewBucht extends JDialog
{
      JButton JBUpdate = new JButton(new ImageIcon(getClass().getResource("/images/save.png")));
      JButton JBCancel = new JButton("Cancel",new ImageIcon(getClass().getResource("/images/cancel.png")));
      
      JLabel JLPic1 = new JLabel();
      JLabel JLBanner = new JLabel("Please fill-up all the required fields.");
      
      JLabel ADSLNoLabel = new JLabel("ADSL #:");
      
      JTextField  ADSLNoTF = new JTextField();

      Dimension screen =       Toolkit.getDefaultToolkit().getScreenSize();
      
      Statement st;
      boolean ADDING_STATE;
      Connection connect;
      Statement stAEC;
      ResultSet rsAEC;
      PictMenuFrameDemo OwnerForm;
      TreeTableInternalFrame tf;
      int pk;
      final int terminal_ID;
      int terminalPort_id = 0;
      String cityName = null;
      String coName = null;
      int terminal_Port= 0;
      int privilege = 0;
      int cityView = 0;
      
      public AddNewBucht(boolean ADD_STATE,PictMenuFrameDemo OwnerForm,final int terminalID,int terminalPortID,int terminalPort,TreeTableInternalFrame tableFrame)
      {
            super(OwnerForm,true);
            tf = tableFrame;
            ADDING_STATE = ADD_STATE;
            terminal_ID = terminalID;
            //System.out.println("terminalID    "+terminalID);
            //System.out.println("terminal_ID    "+terminal_ID);
            //System.out.println(OwnerForm+", "+terminal_ID+", "+terminalPortID+", "+terminalPort+", "+tf);
            pk = terminalID;
            terminalPort_id = terminalPortID;
            ADSLNoTF.setEnabled(true);
            
            privilege = Login.privilege;
            cityView = Login.cityView;
            
            if(ADD_STATE==true)
            {
                  JLPic1.setIcon(new ImageIcon(getClass().getResource("/images/bNew.png")));
                  setTitle("Add New Customer");
                  JBUpdate.setText("Update");
                  //JTFDState.setSelectedItem(state);
                  
            }
            
            JPanel JPContainer = new JPanel();
            JPContainer.setLayout(null);
            //-- Add the JLPic1
            JLPic1.setBounds(5,5,32,32);
            JPContainer.add(JLPic1);
            
            //-- Add the JLBanner
            JLBanner.setBounds(55,5,268,48);
            JLBanner.setFont(new Font("Dialog",Font.PLAIN,12));
            JPContainer.add(JLBanner);
            
            
            ADSLNoLabel.setBounds(40,72,105,20);
            ADSLNoLabel.setFont(new Font("Dialog",Font.PLAIN,12));
            
            ADSLNoTF.setBounds(115,72,100,20);
            //JTFDState.setBounds(110,116,200,20);
            ADSLNoTF.setFont(new Font("Dialog",Font.PLAIN,12));
            
            JPContainer.add(ADSLNoLabel);
            JPContainer.add(ADSLNoTF);
            
            JBUpdate.setBounds(45,130,105,25);
            JBUpdate.setFont(new Font("Dialog", Font.PLAIN, 12));
            JBUpdate.setMnemonic(KeyEvent.VK_A);
            JBUpdate.addActionListener(JBActionListener);
            JBUpdate.setActionCommand("update");
            JPContainer.add(JBUpdate);
            
            JBCancel.setBounds(170,130,99,25);
            JBCancel.setFont(new Font("Dialog", Font.PLAIN, 12));
            JBCancel.setMnemonic(KeyEvent.VK_C);
            JBCancel.addActionListener(JBActionListener);
            JBCancel.setActionCommand("cancel");
            JPContainer.add(JBCancel);
            
            getContentPane().add(JPContainer);
            setSize(325,200);
            setResizable(false);
            setLocation((screen.width - 325)/2,((screen.height-383)/2));
      }
0
 
LVL 92

Expert Comment

by:objects
ID: 19598518
JTextField  ADSLNoTF = new JTextField(new FixedSizePlainDocument(10));
0
 

Author Comment

by:zolf
ID: 19598549


i get a error saying the constructor JTextfield(FixedSizePlainDocument); is undefined
0
 

Author Comment

by:zolf
ID: 19598588

i do this


public class AddNewBucht extends JDialog
{
      JButton JBUpdate = new JButton(new ImageIcon(getClass().getResource("/images/save.png")));
      JButton JBCancel = new JButton("Cancel",new ImageIcon(getClass().getResource("/images/cancel.png")));
      
      JLabel JLPic1 = new JLabel();
      JLabel JLBanner = new JLabel("Please fill-up all the required fields.");
      
      JLabel ADSLNoLabel = new JLabel("ADSL #:");
      //JTextField  ADSLNoTF = new JTextField();      

      Dimension screen =       Toolkit.getDefaultToolkit().getScreenSize();
      
      Statement st;
      boolean ADDING_STATE;
      Connection connect;
      Statement stAEC;
      ResultSet rsAEC;
      PictMenuFrameDemo OwnerForm;
      TreeTableInternalFrame tf;
      int pk;
      final int terminal_ID;
      int terminalPort_id = 0;
      String cityName = null;
      String coName = null;
      int terminal_Port= 0;
      int privilege = 0;
      int cityView = 0;
      
      public AddNewBucht(boolean ADD_STATE,PictMenuFrameDemo OwnerForm,final int terminalID,int terminalPortID,int terminalPort,TreeTableInternalFrame tableFrame)
      {
            super(OwnerForm,true);
            tf = tableFrame;
            ADDING_STATE = ADD_STATE;
            terminal_ID = terminalID;
            //System.out.println("terminalID    "+terminalID);
            //System.out.println("terminal_ID    "+terminal_ID);
            //System.out.println(OwnerForm+", "+terminal_ID+", "+terminalPortID+", "+terminalPort+", "+tf);
            pk = terminalID;
            terminalPort_id = terminalPortID;
            
            JTextField  ADSLNoTF = new JTextField(new FixedSizePlainDocument(10));
            
            ADSLNoTF.setEnabled(true);
            privilege = Login.privilege;
0
 
LVL 92

Expert Comment

by:objects
ID: 19598727
sorry that should have been:

            JTextField  ADSLNoTF = new JTextField();
            ADSLNoTF.setDocument(new FixedSizePlainDocument(10));
0
 

Author Comment

by:zolf
ID: 19598807

thanks that got solved.but when i enter more then 10 digits it accepts it.

insertString where do i put this method
0
 
LVL 92

Expert Comment

by:objects
ID: 19598857
should be in FixedSizePlainDocument

    class FixedSizePlainDocument extends PlainDocument {
        int maxSize;
   
        public FixedSizePlainDocument(int limit) {
            maxSize = limit;
        }
   
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
            if ((getLength() + str.length()) <= maxSize) {
                super.insertString(offs, str, a);
            } else {
                throw new BadLocationException("Insertion exceeds max size of document", offs);
            }
        }
    }
0
 

Author Comment

by:zolf
ID: 19598955


i mean in my class AddNewBucht extends JDialog
where i am showing the textbox where do i use that insertString method.
0
 
LVL 92

Expert Comment

by:objects
ID: 19598971
you don't, its part of the FixedSizePlainDocument class.
0
 

Author Comment

by:zolf
ID: 19598989

then why is it accepting more then 10 digit.please help
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19600076
Make sure you´re not confusing one text field with another. You need to initialize an instance variable probably, not a local one
0
 

Author Comment

by:zolf
ID: 19606192

can you please give me more details.in my code above
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question