Reputation: 572
So I got know this is a popular question and already found the solution. But when I try this it doesn't work properly.
My JTextField is empty and the button isn't enabled. When I write something in my textfield the button doesn't get enabled.
So my program should check this field every second whether it's empty or not. As soon as someone writes something into the textfield the button should be enabled.^^
loginbt = new JButton("Login");
loginbt.addActionListener(new loginHandler());
add(loginbt);
if(name.getText().equals("")) {
loginbt.setEnabled(false);
}else {
loginbt.setEnabled(true);
}
Upvotes: 34
Views: 256146
Reputation: 1
You can use
`loginbt = new JButton("Login"); loginbt.addActionListener(new loginHandler()); add(loginbt);
if(String.valueOf(name.getText()).isEmpty() {
loginbt.setEnabled(false);
}else {
loginbt.setEnabled(true);
}
`
Upvotes: 0
Reputation: 540
The following will return true if the JTextField name
does not contain text:
name.getText().isEmpty();
Upvotes: 22
Reputation: 1
Try this one and change booleans statements around.
String text = name.getText();
if(!text.isEmpty()) {
loginbt.setEnabled(true);
}else {
loginbt.setEnabled(false);
}
In my action method it worked fine.
public void actionPerformed(ActionEvent e){
String inputText = e.getActionCommand();
String itext = text1.getText();
if(inputText.equals("Submit") && !itext.isEmpty()){
label1.setText(text1.getText());
text1.setText("");
}
}
Upvotes: 0
Reputation: 25
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// Submit Button
String Fname = jTextField1.getText();
String Lname = jTextField2.getText();
String Desig = jTextField3.getText();
String Nic = jTextField4.getText();
String Phone = jTextField5.getText();
String Add = jTextArea1.getText();
String Dob = jTextField6.getText();
// String Gender;
// Image
if (Fname.hashCode() == 0 || Lname.hashCode() == 0 || Desig.hashCode() == 0 || Nic.hashCode() == 0 || Phone.hashCode() == 0 || Add.hashCode() == 0)
{
JOptionPane.showMessageDialog(null, "Some fields are empty!");
}
else
{
JOptionPane.showMessageDialog(null, "OK");
}
}
Upvotes: -1
Reputation: 2414
you can use isEmpty()
or isBlank()
methods regarding what you need.
Returns true if, and only if, length() is 0.
this.name.getText().isEmpty();
Returns true if the string is empty or contains only white space codepoints, otherwise false
this.name.getText().isBlank();
Upvotes: 2
Reputation: 9775
Well, the code that renders the button enabled/disabled:
if(name.getText().equals("")) {
loginbt.setEnabled(false);
}else {
loginbt.setEnabled(true);
}
must be written in javax.swing.event.ChangeListener
and attached to the field (see here). A change in field's value should trigger the listener to reevaluate the object state. What did you expect?
Upvotes: 3
Reputation: 363
if(name.getText().hashCode() != 0){
JOptionPane.showMessageDialog(null, "not empty");
}
else{
JOptionPane.showMessageDialog(null, "empty");
}
Upvotes: 0
Reputation: 29
use the following code :
if(name.getText().equals(""))
{
loginbt.disable();
}
Upvotes: 2
Reputation: 71
To Check JTextFiled is empty or not condition:
if( (billnotf.getText().length()==0)||(billtabtf.getText().length()==0))
Upvotes: 4
Reputation: 1332
Try this
if(name.getText() != null && name.getText().equals(""))
{
loginbt.setEnabled(false);
}
else
{
loginbt.setEnabled(true);
}
Upvotes: 0
Reputation: 17622
For that you need to add change listener (a DocumentListener
which reacts for change in the text) for your JTextField
, and within actionPerformed()
, you need to update the loginButton
to enabled/disabled depending on the whether the JTextfield
is empty or not.
Below is what I found from this thread.
yourJTextField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
changed();
}
public void removeUpdate(DocumentEvent e) {
changed();
}
public void insertUpdate(DocumentEvent e) {
changed();
}
public void changed() {
if (yourJTextField.getText().equals("")){
loginButton.setEnabled(false);
}
else {
loginButton.setEnabled(true);
}
}
});
Upvotes: 39
Reputation: 1
Try with keyListener in your textfield
jTextField.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (text.getText().length() >= 1) {
button.setEnabled(true);
} else {
button.setEnabled(false);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
Upvotes: -1
Reputation: 117587
What you need is something called Document Listener. See How to Write a Document Listener.
Upvotes: 4