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
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);
}
}
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 :)
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
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).
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)