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

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-17 07:20:06

问题


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 I write the if statement?

This is the code for the add info button:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    DefaultTableModel model = (DefaultTableModel) jTable1.getModel();

    model.addRow(new Object[]{jTextField1.getText(), jTextField2.getText(),
    jTextField3.getText(), jTextField4.getText(), jTextField5.getText(),
    jTextField6.getText(), jTextField7.getText(), jTextField8.getText()});
}

回答1:


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 ...'




回答2:


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.




回答3:


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.
}


来源:https://stackoverflow.com/questions/34602542/a-button-in-my-app-should-only-get-the-text-in-8-text-fields-and-send-it-to-a-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!