问题
As the topic suggests I want to unlock(setEnabled(true)) my JButton register when the other fields have any text but I don't know what type of listener is this. I upload an image for clearer understanding. http://postimg.org/image/ab8alz44d/
回答1:
add a document listener for each text field.
void init() {
//construct text fields
...
// add listeners
textField1.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
checkUnlock();
}
public void removeUpdate(DocumentEvent e) {
checkUnlock();
}
public void insertUpdate(DocumentEvent e) {
checkUnlock();
});
// repeat for each textfield
}
...
private void checkUnlock() {
boolean unlock = !(textField1.getText().equals("")) && !(textField2.getText().equals("")); // ... and so on
yourButton.setEnabled(unlock);
}
回答2:
Here is a more generic and reusable implementation of using a DocumentListener
to handle this. You can related 1 or more text fields to a specific button. The button will only be enable when all the text fields have some text:
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;
public class DataEntered implements DocumentListener
{
private JButton button;
private List<JTextField> textFields = new ArrayList<JTextField>();
public DataEntered(JButton button)
{
this.button = button;
}
public void addTextField(JTextField textField)
{
textFields.add( textField );
textField.getDocument().addDocumentListener( this );
}
public boolean isDataEntered()
{
for (JTextField textField : textFields)
{
if (textField.getText().trim().length() == 0)
return false;
}
return true;
}
@Override
public void insertUpdate(DocumentEvent e)
{
checkData();
}
@Override
public void removeUpdate(DocumentEvent e)
{
checkData();
}
@Override
public void changedUpdate(DocumentEvent e) {}
private void checkData()
{
button.setEnabled( isDataEntered() );
}
private static void createAndShowUI()
{
JButton submit = new JButton( "Submit" );
submit.setEnabled( false );
JTextField textField1 = new JTextField(10);
JTextField textField2 = new JTextField(10);
DataEntered de = new DataEntered( submit );
de.addTextField( textField1 );
de.addTextField( textField2 );
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(textField1, BorderLayout.WEST);
frame.add(textField2, BorderLayout.EAST);
frame.add(submit, BorderLayout.SOUTH);
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
回答3:
Add key listener to text fields and check all fields have some values, if so then enable jbutton.
text1.addKeyListener(this);
text2.addKeyListener(this);
And in key typed method write
public void keyTyped(KeyEvent e) {
if(text1.getText().length() > 0 && text2.getText().length() > 0) {
button.setEnabled(true);
} else { button.setEnabled(false); }
}
来源:https://stackoverflow.com/questions/29438072/unlock-jbutton-as-fields-have-text