简体   繁体   中英

How to validate a JTextField in eclipse?

I want to be able to validate a number of different JTextFields in my java project. One being to ensure the user enters a number into a particular field. Another problem I'm having is being able to validate a particular number, such as 1234 in a field when a submit button is pressed.

If I have to create a method in a separate class then that fine but how would I call the method into my GUI class to use for my JTextField.

I'm quite new to Java and find some of this hard so any help is greatly appreciated.

At the minute I have:

import java.awt.BorderLayout;

public class CashDialog extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField txtEmilysBistro;
    private JTextField textconfirmNumber;
    private String confirmNumber;
    private JButton btnSubmit;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            CashDialog dialog = new CashDialog();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public CashDialog() {

        this.confirmNumber = confirmNumber;

        setBounds(100, 100, 526, 372);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBackground(new Color(255, 0, 153));
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        {
            txtEmilysBistro = new JTextField();
            txtEmilysBistro.setBackground(new Color(255, 0, 153));
            txtEmilysBistro.setHorizontalAlignment(SwingConstants.CENTER);
            txtEmilysBistro.setForeground(new Color(255, 255, 255));
            txtEmilysBistro.setFont(new Font("AR ESSENCE", Font.PLAIN, 50));
            txtEmilysBistro.setEditable(false);
            txtEmilysBistro.setText("Emily's Bistro");
            txtEmilysBistro.setBounds(0, 0, 504, 57);
            contentPanel.add(txtEmilysBistro);
            txtEmilysBistro.setColumns(10);
        }

        JTextArea txtrConfirm = new JTextArea();
        txtrConfirm.setForeground(new Color(255, 255, 255));
        txtrConfirm.setBackground(new Color(255, 0, 153));
        txtrConfirm.setEditable(false);
        txtrConfirm.setFont(new Font("AR ESSENCE", Font.PLAIN, 25));
        txtrConfirm.setText("Please allow your waiter/waitress to enter         the confirmation number.");
        txtrConfirm.setBounds(54, 73, 407, 77);
        contentPanel.add(txtrConfirm);
        txtrConfirm.setLineWrap(true);

        textconfirmNumber = new JTextField();
        textconfirmNumber.setFont(new Font("AR ESSENCE", Font.PLAIN, 30));
        textconfirmNumber.setBounds(147, 148, 227, 47);
        contentPanel.add(textconfirmNumber);
        textconfirmNumber.setColumns(10);

        btnSubmit = new JButton("Submit");
        btnSubmit.setBackground(new Color(102, 51, 255));
        btnSubmit.setForeground(new Color(255, 255, 255));
        btnSubmit.setFont(new Font("AR ESSENCE", Font.PLAIN, 25));
        btnSubmit.setBounds(184, 211, 162, 29);
        contentPanel.add(btnSubmit);

    }
}

In this particular text field I only want it to accept the number 1234 in the text field.

There are a few issues with your code, but that is beside the point. To address your question specifically here is one approach. Grant it, you'd probably want to handle it differently, verify you are getting an integer instead of comparing strings, but still this is how you'd add a listener to your submit button.

        btnSubmit.addActionListener(new ActionListener()
        {

                @Override
                public void actionPerformed(ActionEvent arg0)
                {
                    final String data = txtrConfirm.getText();
                    if( data.compareTo("1234") != 0 )
                    {
                        // alert the user
                    }
                    else
                    {
                        // data is good, do something with it
                    }
                }

        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM