Using an if statement to test if JTextField is an integer

后端 未结 3 1686
栀梦
栀梦 2021-01-28 02:07

I want my program to be able to tell if what is inside my two JTextFields is an integer or a String.

CODE

          public void actionP         


        
3条回答
  •  Happy的楠姐
    2021-01-28 02:38

    For restricting a User from entering anything but digits, you can set a DocumentFilter on the JTextField.

    Here is a small example :

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.AbstractDocument;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.DocumentFilter;
    import javax.swing.text.DocumentFilter.FilterBypass;
    
    public class InputInteger
    {
        private JTextField tField;
        private MyDocumentFilter documentFilter;
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("Input Integer Example");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            contentPane.setBorder(
                BorderFactory.createEmptyBorder(5, 5, 5, 5));
            tField = new JTextField(10);
            ((AbstractDocument)tField.getDocument()).setDocumentFilter(
                    new MyDocumentFilter());        
            contentPane.add(tField); 
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String[] args)
        {
            Runnable runnable = new Runnable()
            {
                @Override
                public void run()
                {
                    new InputInteger().displayGUI();
                }
            };
            EventQueue.invokeLater(runnable);
        }
    }
    
    class MyDocumentFilter extends DocumentFilter
    {   
        @Override
        public void insertString(DocumentFilter.FilterBypass fp
                , int offset, String string, AttributeSet aset)
                                    throws BadLocationException
        {
            int len = string.length();
            boolean isValidInteger = true;
    
            for (int i = 0; i < len; i++)
            {
                if (!Character.isDigit(string.charAt(i)))
                {
                    isValidInteger = false;
                    break;
                }
            }
            if (isValidInteger)
                super.insertString(fp, offset, string, aset);
            else
                Toolkit.getDefaultToolkit().beep();
        }
    
        @Override
        public void replace(DocumentFilter.FilterBypass fp, int offset
                        , int length, String string, AttributeSet aset)
                                            throws BadLocationException
        {
            int len = string.length();
            boolean isValidInteger = true;
    
            for (int i = 0; i < len; i++)
            {
                if (!Character.isDigit(string.charAt(i)))
                {
                    isValidInteger = false;
                    break;
                }
            }
            if (isValidInteger)
                super.replace(fp, offset, length, string, aset);
            else
                Toolkit.getDefaultToolkit().beep();
        }
    }
    

    Or one can simply use this approach, as given by @Carlos Heuberger

    @Override
    public void insertString(FilterBypass fb, int off
                         , String str, AttributeSet attr) 
                                   throws BadLocationException 
    {
        // remove non-digits
        fb.insertString(off, str.replaceAll("\\D++", ""), attr);
    } 
    @Override
    public void replace(FilterBypass fb, int off
          , int len, String str, AttributeSet attr) 
                           throws BadLocationException 
    {
        // remove non-digits
        fb.replace(off, len, str.replaceAll("\\D++", ""), attr);
    }
    

提交回复
热议问题