set JTextfiled digit value

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
zolfAsked:
Who is Participating?
 
ksivananthConnect With a Mentor Commented:
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
 
objectsCommented:
you can use a DocumentFilter to achieve that.
0
 
objectsConnect With a Mentor Commented:
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
zolfAuthor Commented:


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
 
objectsCommented:
JTextField  ADSLNoTF = new JTextField(new FixedSizePlainDocument(10));
0
 
zolfAuthor Commented:


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

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
 
objectsCommented:
sorry that should have been:

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

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

insertString where do i put this method
0
 
objectsCommented:
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
 
zolfAuthor Commented:


i mean in my class AddNewBucht extends JDialog
where i am showing the textbox where do i use that insertString method.
0
 
objectsCommented:
you don't, its part of the FixedSizePlainDocument class.
0
 
zolfAuthor Commented:

then why is it accepting more then 10 digit.please help
0
 
CEHJCommented:
Make sure you´re not confusing one text field with another. You need to initialize an instance variable probably, not a local one
0
 
zolfAuthor Commented:

can you please give me more details.in my code above
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.