Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Restrict String character's type and location.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Restrict String character's type and location.

    I have the next dialog to pop up and ask the user for some info:

    String id = (String)JOptionPane.showInputDialog(
    						null,
    						"Post #",
    						"Información del cliente.",
    						JOptionPane.PLAIN_MESSAGE
    						);

    The format I would like it to be like one of this:

    1. Only numbers.
    or
    2. Limit the first character to a "+" and the rest numbers.
    or
    3. Limit the first character to a "." and the rest numbers.

    So, I hope you guys understand what I want.

    I don't want to get the string and subtract the characters that I want, I want to limit the characters that the user can type in the dialog box.

    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Restrict String character's type and location.

    I don't think there is a way to restrict input with the JOptionPane class's methods.
    There is a way by using the model behind a JTextField class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Restrict String character's type and location.

    Okay, I updated my code to this:

    JPanel userPanel = new JPanel();
    				userPanel.setLayout(new GridLayout(2,1));
     
    				JLabel posLbl = new JLabel("POS #:");
    				posLbl.setFont(font2);
    				JTextField posFld = new JTextField();
    				posFld.setFont(font);
     
    				userPanel.add(posLbl);
    				userPanel.add(posFld);
     
    				int input = JOptionPane.showConfirmDialog(userPanel, userPanel, "Información del cliente."
    						,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
     
    				if (input == 0)
    				{
    					String id = posFld.getText();
    }

    How would I do it with a JTextField class?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Restrict String character's type and location.

    Look at how to use the document model used by the JTextField class.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Restrict String character's type and location.

    I've been reading about it for the past 45 minutes and I still have no Idea how they limit the characters:

    For example, i found the next code:

    class DocumentInputFilter extends DocumentFilter
    	{
    		public void replace(FilterBypass fb, int offset
    				, int length, String text, AttributeSet as) throws BadLocationException
    				{
    			int len = text.length();
    			if (len > 0)
    			{
    				if (Character.isDigit(text.charAt(len - 1)))
    					super.replace(fb, offset, length, text, as);
    				else 
    				{
    					JOptionPane.showMessageDialog(null, "Please Enter a valid Integer Value."
    							, "Invalid Input : ", JOptionPane.ERROR_MESSAGE);
    					Toolkit.getDefaultToolkit().beep();
    				}
    			}                                               
    				}

    It will get the numbers part done, but how do I add the "." and the "+" to the exceptions?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Restrict String character's type and location.

    how do I add the "." and the "+" to the exceptions
    Add some logic to allow those characters in addition to the digits.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. What is the easiest way to change a single character in a string?
    By Jumbosize in forum Java Theory & Questions
    Replies: 2
    Last Post: April 26th, 2012, 04:16 PM
  3. Help using String and character analyzers such as isDigit() and isLetter()
    By scholaryoshi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 6th, 2012, 06:24 PM
  4. Replies: 2
    Last Post: October 22nd, 2011, 01:34 AM
  5. How to convert or break String into Character
    By Bharath12 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 14th, 2011, 11:41 PM