Source Code : Based on JTextField content, enable or disable a JButton
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Based on JTextField content, enable or disable a JButton
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
public class Main {
JButton button = new JButton("foo");
JTextField textfield = new JTextField(10);
Document document;
public Main() {
document = textfield.getDocument();
document.addDocumentListener(new JButtonStateController());
}
class JButtonStateController implements DocumentListener {
JButtonStateController() {
}
public void changedUpdate(DocumentEvent e) {
disableIfEmpty(e);
}
public void insertUpdate(DocumentEvent e) {
disableIfEmpty(e);
}
public void removeUpdate(DocumentEvent e) {
disableIfEmpty(e);
}
public void disableIfEmpty(DocumentEvent e) {
button.setEnabled(e.getDocument().getLength() > 0);
}
}
}
Thank with us