2

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 null. Where can i put something like this

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

so that the program will enable/disable the button live. Like once the last field even has 1 character in it I want it to be enabled?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
erp
  • 2,950
  • 9
  • 45
  • 90

5 Answers5

4

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

Community
  • 1
  • 1
resueman
  • 10,572
  • 10
  • 31
  • 45
  • what would the check do? would something like setting a variable inside each document Listener, then when they call the method if all are true for example then the button would setEnable? – erp Aug 19 '14 at 16:43
  • @erp You'd just need to test that each field is non-empty. Something like `if(jTextField1.getText().equals("") || ...) jButton2.setEnabled(false);` – resueman Aug 19 '14 at 16:52
3

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);
    }
}
My-Name-Is
  • 4,814
  • 10
  • 44
  • 84
0

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

SlopeOak
  • 175
  • 1
  • 10
0

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)

user3789587
  • 105
  • 1
  • 1
  • 6
  • Sorry, but this is a very bad answer. You should never use a KeyListener in a JTextField and especially not for this purpose. Instead check the JTextField's Document with a DocumentListener as per other answers to this question. – Hovercraft Full Of Eels Aug 19 '14 at 16:52
-1
  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 :)

Sivaprasad
  • 119
  • 10
  • 1
    Sorry to be blunt, but this is a terrible recommendation: 1) requires use of a KeyListener, something that can interfere with the inner workings of the text field, and 2) it does not work at all for copy and paste applications. There are much better and robust solutions that are already listed in this question and answer. Never use key listeners on Swing text components and instead always favor higher level listeners such as DocumentListeners or DocumentFilters. – Hovercraft Full Of Eels Nov 01 '20 at 17:42