Restricting JTextField input

Hello,
New to swing programing, and I want to restrict the type of input accepted in a JTextField. For example, I want to only allow numeric characters. I tried changing the value on a caret event, but quickly realized that is not a good thing.
Any suggestions?
Thanks
evster

See JFormattedTextField

Similar Messages

  • Restrict the input string length

    Hi, how do I restrict the input string length in a JTextField. Suppose if I want my text field to accept only 4 characters, the fifth character I try to enter shouldn't be accepted in the field.
    Thanks,
    Kalyan.

    This is for 6 characters limit
    //create a JTextField that is six characters long
    JTextField textField = new JTextField(new FixedNumericDocument(5,true),"", 6);
    Here boolean true means only numeric. set to false to take in alphanumeric.
    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.*;
    public class FixedNumericDocument extends PlainDocument {
    private int maxLength = 9999;
    private boolean numericOnly;
    public FixedNumericDocument(int maxLength, boolean numericOnly) {
    super();
    this.maxLength = maxLength;
    this.numericOnly = numericOnly;
    //this is where we'll control all input to our document.
    //If the text that is being entered passes our criteria, then we'll just call
    //super.insertString(...)
    public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
    if (getLength() + str.length() > maxLength) {
    return;
    else {
    try {
    if (numericOnly) {
    Integer.parseInt(str);
    //if we get here then str contains only numbers
    //so that it can be inserted
    super.insertString(offset, str, attr);
    catch(NumberFormatException exp) {
    return;
    return;

  • Transaction F110 - Possible to restrict the input of Identification-field?

    Hello all,
    is it possible to restrict the input of the "Identification"-field in transaction F110 (Automatic Payment Transactions: Status)?
    e.g.: User X with company code XX is only allowed to enter XX01 in the identification field.
            User Y with company code YY is only allowed to enter YY01...
    Is that possible?
    Thanks in advance for any reply!
    Steffen
    Message was edited by:
            Steffen Poetsch

    Hi
    You can control the "identification" with the help of the BASIS guy.
    They will define the authorization object to field level.
    Create seperate roles for that and assign accordingly
    VVR

  • Restriction of input help for BPS exit variables

    Hello,
    In BPS 3.0b, I created BPS variables with user exit in order to restrict the values in the <b>input help</b> of a characteristic (related to the value selected by user of another variable for example).
    In IP, I know that BPS variables are not used anymore and we have to create BI variables. But I don't know how to restrict the values of an input help in BI. I tried with the user exit RSR00001 but I didn't manage.
    may be is there a possibility to reuse BPS variables ?
    Thank you for your Help.
    Catherine Bellec.<b></b>

    Hi Catherine,
    could you please show me a sample how to restrict the input help for a variable in the user-exit?
    Thank you in advance.
    Best regards,
    Andreas

  • HOW to Restrict the input Help for 0MATERIAL in the BPS Layout

    Hi,
    I have requirement to Restrict the input Help for 0MATERIAL  in the BPS Layout.
    For Example if the Planning Package is Restricted to SALES ORGANISATION ( 3000 )  then the system shuold  check the 0MAT_SALES  where SALES ORGANISATION IS "3000" )AND PASS THE Material Numbers to the 0material list.
    I have Copied the standard Funtcion group  "UPF_VARIABLE_USER_EXIT"  to Z fucttion and have attached to Z Variabe as User Exit .
    this Variable is  Attached to 0material in the Planning Pakage. So tha now the 0MATERIAL is restricted to the variable which is having the User Exit.
    But how to acces the Values of Planning Package for which the Layout is bein Executed from this Z User Exit ???
    I Know how to restrict the input help, but my only problem is that how to get the values of Planning package through this User Exit.
    Please suggest if it is possible.
    Regards,
    Nilesh Labde

    Hi Nilesh,
    As I understand from your question,you know how to restrict but the issue is to know the value in the package with which you need to restrict.
    There are two tables which can help you finding the value used in package for sales organisation:
    1. UPC_PACKAGE
    2. UPC_OPTIOS
    How to use ?
    From UPC Package you will get one GUID, Hit the second table UPC_OPTIOS with this GUID.
    In field "FIELDNAME" enter the name of the characteristic whose value is req (sales organisation in your case)
    Hope this helps you
    Mann

  • Event.rc and Restriction of inputs from the User...

    Im using Adobe Professional Live cycle designer 7.0 to design pdf form. Also, Im using an existing pdf (supplied by the customer), which is designed through teleform designer (with adobe 6.0) model for this design. In the existing one, the javascript methods are working fine for restricting keys, validation, etc. But these scripts are not working in designer as the designer is using XFA model.
    Im trying to restrict the key inputs (like only numbers/characters should be entered). But I couldnt find the equivalent statements for some of the statements (which are used in the teleform pdf).
    For eg.
    event.rc = true. Allows keystroke to be added to entry. event.rc = false. Prevents keystroke being added to entry in the following function:
    function TFTemplate_KeyStroke( mask )
    if (event.willCommit) {
    return;
    var value = AFMergeChange( event );
    if (!mask.test( value )) {
    event.rc = false;
    app.beep( 0 );
    But I couldnt find the matching statements in Designer for event.willCommit, AFMergeChange(),event.rc, etc.
    If I get, I can restrict the inputs.
    Expecting any solutions from you...
    Thanks in Advance...

    Hi,
    Based on your description, I understand that you want to use application or some functional tools and then
    monitor the user printer job. When be greater than a certain number, it will need to acquire additional information. As I know, it seems that there is no such function can meet your requirement. Sorry for it.
    In addition, I find that you use a third-party software for this functionality before. Please check and confirm
    if you can get a new version (or some updates, service package) that may help it to be supported in Windows Server 2012.
    Hope this helps.
    Best regards,
    Justin Gu

  • JTextField input check (errorDialog if invalid) & JComboBox Focus

    I have a JTextfield which does an input check when the JTextField focus listener's focusLost method is called.
    If I loose focus by clicking in on a JComboBox. The check gets done ok and focus is returned appropriately to the JTextField without any problems.
    But if I try to show an errorDialog when the input is incorrect, the focus is returned to the JTextField without any problems, but the JComboBox remains highlighted as though it is still selected!
    I am using sdk 1.4... on a linux box. does anyone know why this happens? and can anyone provide a clean solution to this?
    Thanx,
    Diego Paloschi.

    Hello Kleopatra,
    Here's a little demo:
    import javax.swing.*;
    import javax.swing.event.*;
    public class Debug extends JPanel
    private JTextField t1;
    private JComboBox c1;
    private T1Verifier t1Verifier;
    public void init()
         t1 = new JTextField("20", 10);
         t1.setInputVerifier(new T1Verifier(this));
         String[] s = {"v1","v2","v3"};
         c1 = new JComboBox(s);
         add(t1);
         add(c1);
    public static void main(String s[])
         Debug d = new Debug();
         d.init();
         JFrame f = new JFrame("Debug");
         f.getContentPane().add(d);
         f.setSize(300,300);
         f.setVisible(true);
    class T1Verifier extends InputVerifier
    private Debug debug;
    public T1Verifier(Debug debug)
         this.debug = debug;
    public boolean verify(JComponent input)
         JTextField tf = (JTextField) input;
         float value = Float.parseFloat(tf.getText());
         boolean inRange = (value >= 0) && ( value <= 10);
         return inRange;
    public boolean shouldYieldFocus(JComponent input)
         boolean valid = super.shouldYieldFocus(input);
         JTextField tf = (JTextField) input;
         if ( !valid ) {
         tf.requestFocus();
         JOptionPane d = new JOptionPane();
         d.showMessageDialog(debug, "Out Of Range.", "ERROR" , JOptionPane.ERROR_MESSAGE);
         return valid;
    Even if u comment out the JOptionPane and just make the inputverifier return control to the textfield it still doesn't work! What am I missing? Or how would u doi it?

  • Restrict Data Input in an Input Schedule for a particular number of days in a Month

    Hi All,
    I want to restrict data save in Input Schedule for any particular number of days in a month. BRAD will select number of days for the that particular month and data input for that duration should be disabled.
    Duration will be dynamic and will be decided by BRAD.
    Basically this is a requirement of BPF, but I have thought of doing in this manner.
    Regards,
    Mayank

    Hi Vadim
    I saw your this solution:
    Dim epm As New FPMXLClient.EPMAddInAutomation
    Dim blnMySave As Boolean
    Public Function BEFORE_SAVE() As Boolean
    If blnMySave Then
        BEFORE_SAVE = True
    Else
        MsgBox "Please use button on the worksheet!"
        BEFORE_SAVE = False
    End If
    End Function
    It seems as if this may help me, but can you please guide me how to trigger this function for a change of value in any particular cell, in an input form.

  • Restrict user input to numbers

    Hello All,
    Is there  a native functionality to restrict the user input only to numbers in say inputfield UI elem? other than writing code to check what has been entered
    Cheers
    Senon

    Hi,
    check the Format inputfield when using simpletypes. More details here:
    http://help.sap.com/saphelp_nwce72/helpdata/en/48/57551fc01071b9e10000000a421937/frameset.htm
    Still what you'd like to achieve is not possible I guess, because a validation must take place in order to decide what you have entered in your browser on client side. A round trip is necessary to have the validation as per the WD Phase Model (http://help.sap.com/saphelp_nw70/helpdata/en/13/aeeb41eda96624e10000000a1550b0/frameset.htm).
    Perhaps using some kind of scripting this is possible, but I am not sure we provide support for it in case any question raises.
    Cheers,
    Ervin

  • Restrict multiline input field to 2 lines?

    Is there a way to restrict the amount of lines that a
    multiline input field will allow?

    Double post

  • Check validation of JTextField input

    In my code, I have a JTextField which can only input 0 or 1, I want to check if the input is empty first then if it is 0 or 1. The following is my code:
    if (myJTextField.getText()==""){
    return "empty";
    else if (myJTextField.getText()!="0" && myJTextField.getText() != "1")
    return "not 0 or 1";
    but it does not work, where is the problem?
    Thanks,

    if (myJTextField.getText().length() > 0){
    try {
    int i =
    int i = Integer.parseInt(myJTextField.getText() );
    if((i != 0) || (i != 1)) msg = "wrong
    g = "wrong number";
    else processIt;
    catch etc...I think the logic operator should be && instead of ||?

  • Problem matching JTextField input to an item in a JList box

    Hello,
    I am trying to read the user's input in a text field and take the user to a match in a list box located underneath the textfield. To do that I have implemented the following code:
    m_pathwayNameTextField.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e) {
    m_pathwayNameTextField_KeyTyped(e);
    private void m_pathwayNameTextField_KeyTyped(KeyEvent e) {
    if (e.getID() == KeyEvent.KEY_TYPED) {
    String prefix = m_pathwayNameTextField.getText();
    char c = e.getKeyChar();
    if (e.getKeyChar() >= KeyEvent.VK_SPACE && e.getKeyCode() < 127) {
    prefix = prefix + e.getKeyChar();
    int position = m_pathwayNamesList.getNextMatch(prefix, 0,
    Position.Bias.Forward);
    if (position != -1) {
    m_pathwayNamesList.ensureIndexIsVisible(position);
    Although the above kind of works, it still has some problems. The first is that if I press a "backspace" character, it won't try to match any results. Also, if I highlight the whole text in the textfield and press a letter, it won't attempt to match either. The reason for the later is that the textfield at that time is not blank and so prefix becomes the textfield plus the newly entered letter, whereas it should be the newly entered letter.
    Any help on this is highly appreciated.
    Thanks.

    The best way to do this is to use a DocumentListener instead of a KeyListener. When the DocumentEvent is fired the document has already been updated and you can just use getText() to get the contents of the Document. No need to play around with backspace characters.

  • JTextfield input Date convert to java.sql.Date

    Hi. I have a textfield for users to input date in 'dd/mm/yyyy' format and I would like to format this input into java.sql.Date to be stored into a database. How should I go about doing that ?
    I did the following:
    SimpleDateFormat formatDate = new SimpleDateFormat("dd/mm/yyyy");
    java.util.Date invoiceDate = formatDate.parse(jTextField3.getText());
    java.sql.Date sqlDate = new java.sql.Date(invoiceDate.getTime());and when I input into jTextField3 the value '28/05/2008' , I found in my database '2008-01-28'. January and May is a few months off and is totally not what I wanted.

    Hi you did you code perfect, but you need to change the pattern for formatting the Date
    dd - date
    mm - minute
    yyyy - year
    here you are parsing month as minute, so the output may wrong..!
    If you need to format properly
    please use the below pattern
    {color:#0000ff}SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");{color}

  • How do your restrict valid input into a JFormattedTextField

    Hi
    I'm interested in setting up a text box that only accepts a one or two digit integer value - so it would stop the user from inputting any character that wasnt a digit, and stop the user from inputting more than two digits - this must be something that is commonly done.
    Thanks, Dave

    Well, I'm confused by your question. JDK1.4 has a class called JFormattedTextField. I don't have JDK1.4 so I can't tell you how to use it, but from other posts I've seen on the forum I think it is something like:
    JFormattedTextField field = new JFormattedTextField( "##" );
    If you don't use JDK1.4 then you can create a custom Documents which will do what you need. The Swing tutorial on "General Rules for Using Text Components" will help:
    http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html
    http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html#validation
    The first link shows how to limit a document to 'x' character. The second on how to check for numeric values.

  • JTextField input validation

    hi!
    i have made a input form for my study project.in the form i want that user only can input numeric value.
    another problem is that i want to make my textfield in such a type so that uesr can,t enter more than 20 character.
    please tell me how can i solve these problems.

    You are entering the same question with two different user names, i dont know what you are trying to achieve but i can guarantee you wont get a faster reply to your question if you free post like that. anyway i have answered part of your question to the other post.

Maybe you are looking for

  • Is apple giving money back if you have Mountain Lion uninstalled?

    Hi everyone, My main question is if Apple will give me my money back for Mountain Lion and iPhoto11 if I have them uninstall them? I know it's only $35 total, but I am not paying them my hard earned money for a program that has ruined my computer and

  • Updated to iTunes 7 - artwork no longer shows on ipod

    After upgradeing to iTunes 7 and firmware upgrade to 1.2 on my 60gb video ipod, I no longer see artwork on my ipod. In fact, everything was pretty messed up, so I restored my ipod, but retained the 1.2 firmware version and iTunes 7 on my PC (Win XP P

  • Pages not loading after pressing the enter button

    when i write some adress into the navigation panel and press enter nothing happens. Pages doesn`t start to load even from the history. Only thing that works are bookmarks, they load on, and I can browse the internet from the sites. By the way, the en

  • How do i sort photos by rating stars in bridge?

    ow

  • Choice for a Backup Hard Drive

    Is paying a 50% premium for a Seagate or Lacie HD (200gig for around $140, as opposed to $90 for Fantom, lower priced brands, etc.) worth it for a backup HD? No audio recording involved, but dependable backup obviously important. Opinions? Dual 1.8 G