0 java swing jtextfield documentlistener
实际上,我正在通过这个小项目来学习 Java Swing API。
这是代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import com.userView.Utilities;
public class MobileNumberValidation extends JFrame {
private JButton clearButton ;
private JTextField mobileNumberField ;
private JLabel mobileNumberLabel ;
private JLabel errorMessage ;
MobileNumberValidation(){
//Intializing the instance variables
clearButton = new JButton("clear");
mobileNumberField = new JTextField();
mobileNumberLabel = new JLabel("TEL");
errorMessage = new JLabel("Tel is empty");
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
//add components to Jframe
add(mobileNumberField);
add(clearButton);
add(mobileNumberLabel);
add(errorMessage);
//set components location
mobileNumberField.setBounds(200, 170, 230, 20);
clearButton.setBounds(200, 220, 100, 20);
mobileNumberLabel.setBounds(160, 170, 100, 20);
errorMessage.setBounds(210, 190, 300,20);
//components style
errorMessage.setForeground(Color.decode("#CD5C5C"));
//add listener and handle event
mobileNumberField.getDocument().addDocumentListener(new DocumentListener(){
@Override
public void changedUpdate(DocumentEvent arg0) {
}
@Override
public void insertUpdate(DocumentEvent arg0) {
processTelValidation();
}
@Override
public void removeUpdate(DocumentEvent event) {
System.out.println("removeUpdate");
processTelValidation();
}
public void processTelValidation() {
if (Utilities.isRegExPatternMatching("^0[0-9]{6}$", mobileNumberField.getText())) {
errorMessage.setVisible(false);
}else{
errorMessage.setVisible(true);
errorMessage.setText("enter a valid Tel number");
}
}
});
clearButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
//clear mobileNumberField
mobileNumberField.setText("");
}
});
}
public static void main(String[] args) {
new MobileNumberValidation();
}
}
Run Code Online (Sandbox Code Playgroud)
每次我按下clearButton清除字段时,mobileNumber.setText("")都会被呼叫并被解雇removeUpdate(),DocumentListener我不希望这样。
您是否知道任何其他方法来清理组件的字段JTextField或使用JTextField.setTextField()但不使用触发DocumentListener方法?
您可以简单地将侦听器保存在局部变量中。然后,从 中删除监听器Document,清除文本,最后将监听器放回到 上Document:
(请记住,我删除了你的一行if (Utilities.isRegExPatternMatching("^0[0-9]{6}$", mobileNumberField.getText())) :)
public class MobileNumberValidation extends JFrame {
private JButton clearButton;
private JTextField mobileNumberField;
private JLabel mobileNumberLabel;
private JLabel errorMessage;
MobileNumberValidation() {
// Intializing the instance variables
clearButton = new JButton("clear");
mobileNumberField = new JTextField();
mobileNumberLabel = new JLabel("TEL");
errorMessage = new JLabel("Tel is empty");
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
// add components to Jframe
add(mobileNumberField);
add(clearButton);
add(mobileNumberLabel);
add(errorMessage);
// set components location
mobileNumberField.setBounds(200, 170, 230, 20);
clearButton.setBounds(200, 220, 100, 20);
mobileNumberLabel.setBounds(160, 170, 100, 20);
errorMessage.setBounds(210, 190, 300, 20);
// components style
errorMessage.setForeground(Color.decode("#CD5C5C"));
// add listener and handle event
DocumentListener validatePhoneListener = new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent arg0) {
}
@Override
public void insertUpdate(DocumentEvent arg0) {
processTelValidation();
}
@Override
public void removeUpdate(DocumentEvent event) {
System.out.println("removeUpdate");
processTelValidation();
}
public void processTelValidation() {
if (Math.random() > 0.5d) {
errorMessage.setVisible(false);
} else {
errorMessage.setVisible(true);
errorMessage.setText("enter a valid Tel number");
}
}
};
mobileNumberField.getDocument().addDocumentListener(validatePhoneListener);
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mobileNumberField.getDocument().removeDocumentListener(validatePhoneListener);
mobileNumberField.setText("");
mobileNumberField.getDocument().addDocumentListener(validatePhoneListener);
}
});
}
public static void main(String[] args) {
new MobileNumberValidation();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
270 次 |
| 最近记录: |