A button in my app should only get the text in 8 text fields and send it to a table IF all fields are filled in

前端 未结 3 391
半阙折子戏
半阙折子戏 2021-01-29 15:21

A button in my app gets all the text you enter in 8 text fields and sends it to a table. I need code so that you need to fill in ALL fields before you can send the info. How do

相关标签:
3条回答
  • 2021-01-29 15:52

    A different approach is to disable the button until data is entered in all the text fields. The you can enable the button:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.List;
    import java.util.ArrayList;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class DataEntered implements DocumentListener
    {
        private JButton button;
        private List<JTextField> textFields = new ArrayList<JTextField>();
    
        public DataEntered(JButton button)
        {
            this.button = button;
        }
    
        public void addTextField(JTextField textField)
        {
            textFields.add( textField );
            textField.getDocument().addDocumentListener( this );
        }
    
        public boolean isDataEntered()
        {
            for (JTextField textField : textFields)
            {
                if (textField.getText().trim().length() == 0)
                    return false;
            }
    
            return true;
        }
    
        @Override
        public void insertUpdate(DocumentEvent e)
        {
            checkData();
        }
    
        @Override
        public void removeUpdate(DocumentEvent e)
        {
            checkData();
        }
    
        @Override
        public void changedUpdate(DocumentEvent e) {}
    
        private void checkData()
        {
            button.setEnabled( isDataEntered() );
        }
    
        private static void createAndShowUI()
        {
            JButton submit = new JButton( "Submit" );
            submit.setEnabled( false );
    
            JTextField textField1 = new JTextField(10);
            JTextField textField2 = new JTextField(10);
    
            DataEntered de = new DataEntered( submit );
            de.addTextField( textField1 );
            de.addTextField( textField2 );
    
            JFrame frame = new JFrame("SSCCE");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(textField1, BorderLayout.WEST);
            frame.add(textField2, BorderLayout.EAST);
            frame.add(submit, BorderLayout.SOUTH);
            frame.pack();
            frame.setLocationByPlatform( true );
            frame.setVisible( true );
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowUI();
                }
            });
        }
    }
    

    A benefit of this approach is you don't worry about if statements which is a good thing as the code is easily changed.

    0 讨论(0)
  • 2021-01-29 16:00

    You use the AND boolean operator; &&

    if(!textfield1.getText().equals("") && !textfield2.getText().equals("") && !textfield3.getText().equals("") && so on){
    //fill table in
    }
    

    In case you didn't know, ! means NOT, so we're saying 'if text field 1 is not empty and textfield 2 is not empty and ...'

    0 讨论(0)
  • 2021-01-29 16:04

    You can check, if a field is empty by getting the text length and comparing it to zero.

    If you don't want to check every text field in one single if-statement, you could check them separately and return, if one field has an invalid value.

    if (jTextField1.getText().length() == 0) 
    {
        //Tell the user, that the first field has to be filled in.
        return;
    }
    if (jTextField2.getText().length() == 0) 
    {
        //Tell the user, that the second field has to be filled in.
        return;
    }
    //...
    

    Otherwise, you could check them all in one if-statement:

    if (jTextField1.getText().length() != 0
        && jTextField2.getText().length() != 0
        && ...) 
    {
        //Fill in the table
    }
    else
    {
        //Tell the user, that the all fields have to be filled in.
    }
    
    0 讨论(0)
提交回复
热议问题