keep jButton grey until jTextFields != null

后端 未结 5 2004
暖寄归人
暖寄归人 2021-01-14 23:08

I have 4 jTextFields that I save the input to a file once a submit button is pressed. I want to be able to keep the submit button disabled until each field is at least not n

相关标签:
5条回答
  • 2021-01-14 23:53

    A simple runnable demo:


    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.util.List;
    import java.util.Vector;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    
    public class ButtonDemo extends JFrame implements DocumentListener {
    
        /**
         * 
         */
        private static final long serialVersionUID = -68704905659973315L;
    
        private JPanel panel = null;
        private JTextField field1 = null;
        private JTextField field2 = null;
        private JButton btn = null;
        private List<JTextField> textFields = null;
    
        public static void main(String[] args) {
            new ButtonDemo();
        }
    
        private ButtonDemo() {
            this.panel = new JPanel();
            this.field1 = new JTextField("JTextField_1");
            this.field2 = new JTextField("JTextField_2");
    
            this.field1.getDocument().addDocumentListener(this);
            this.field2.getDocument().addDocumentListener(this);
    
            this.textFields = new Vector<>();
            this.textFields.add(field1);
            this.textFields.add(field2);
            this.btn = new JButton("Tests-Button");
    
            this.panel.setLayout(new FlowLayout());
            this.panel.add(field1);
            this.panel.add(field2);
            this.panel.add(btn);
    
            this.add(panel);
    
            this.setPreferredSize(new Dimension(200, 200));
            this.pack();
            this.setVisible(true);
        }
    
        @Override
        public void insertUpdate(DocumentEvent e) {
            updateButtonEnabledStatus(btn, textFields);
        }
    
        @Override
        public void removeUpdate(DocumentEvent e) {
            updateButtonEnabledStatus(btn, textFields);
        }
    
        @Override
        public void changedUpdate(DocumentEvent e) {
            updateButtonEnabledStatus(btn, textFields);
        }
    
        private void updateButtonEnabledStatus(JButton btn, List<JTextField> fields) {
            boolean enabled = true;
            for (JTextField field : fields) {
                if (field.getText().length() == 0) {
                    enabled = false;
                    break;
                }
            }
            btn.setEnabled(enabled);
        }
    }
    
    0 讨论(0)
  • 2021-01-14 23:53
    1. Disable the button while initializing the components

    eg:

     public Home() {
       
     initComponents();
     button.setEnabled(false);
    }
    

    2)Enable it by calling the event 'KeyReleased' in the last jTextField.

    eg:

    private void jTextFieldKeyReleased(java.awt.event.KeyEvent evt) {
    button.setEnabled(true);
    }
    

    If you do so, the button will automatically enable when any character enter into the last text feild.

    Thank You :)

    0 讨论(0)
  • 2021-01-14 23:55

    You need to add listeners to detect when the user enters text. In order to have it register any change (and not just when the user hits Enter) you should attach a DocumentListener to the underlying document of each JTextField.

    Then, have each listener call a function to do your check and update the JButton's enabled status accordingly.

    Related

    0 讨论(0)
  • 2021-01-15 00:07

    The jTextField may be empty but it wont necessarily be null. You want to test the contents of it.

    if(jTextField1.getText() == null || jTextField2.getText() == null || jTextField3.getText() == null || jTextField4.getText() == null){
        jButton2.setEnabled(false);
    }
    

    If you want to update the button you need to check the contents on edit. You can do that by implementing an action listener to watch the contents of the text fields. You can do that with a DocumentListener (http://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html).

    0 讨论(0)
  • 2021-01-15 00:10

    use the keyPressed listener for each textfield to run your check wherever the text of a textfield is changed.

    the method to disable the buttons is setEnabled(false);

    Note that you will have to make the buttons dissabled when the program starts if your textfiends are empty at that time (listeners won't run)

    0 讨论(0)
提交回复
热议问题