我正在尝试在输入值后检查JFormattedTextField,以确保它与其他类似字段中输入的任何其他值不匹配。如果在用户键入时反复进行此检查,则会变得非常痛苦,因为我想提供错误消息,如果值发生更改,我需要实施一些更改。但是在输入3之前,像123这样的输入将匹配12,即使实际上还没有发生错误。因此需要延迟。所以我正在尝试使用文档监听器,但是将其延迟,直到用户已经验证(通过文本字段接受,通过操作或转移焦点)条目。
我以为我有一个很好的方法,它几乎可以工作。我的计划是使用文档侦听器来设置焦点和动作侦听器。不幸的是,由于文档监听器不断添加它们,我似乎最终会有大量的焦点和动作监听器。这意味着错误消息和后续处理发生了荒谬的次数而不是一次。我尝试了两种解决方法:1)在添加新的之前删除焦点和动作侦听器,以及2)使用布尔值来标记焦点和动作侦听器的存在,以避免再次创建它们。
<?php
namespace MyApp\PaymentBundle\EventListener;
use Payum\Core\Bridge\Symfony\Event\ExecuteEvent;
use Payum\Core\Request\Authorize;
use Payum\Core\Request\BaseGetStatus;
use Payum\Core\Request\Capture;
use Payum\Core\Model\PaymentInterface;
use Payum\Core\Request\GetHumanStatus;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use MyApp\PaymentBundle\Entity\Payment;
use MyApp\PaymentBundle\Event\PayablePaymentStatusChangeEvent;
use MyApp\PaymentBundle\PaymentEvents;
class PaymentListener
{
protected $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function onPayumGatewayPostexecute(ExecuteEvent $event, $eventName, EventDispatcherInterface $eventDispatcher)
{
$request = $event->getContext()->getRequest();
// Exclude Payum GetStatus request
// => protect from infinite loop as following code execute one GetStatus request
if ($request instanceof BaseGetStatus) {
return;
}
// Request types requiring dispatching an event
if (!($request instanceof Authorize || $request instanceof Capture)) {
return;
}
$payment = $request->getFirstModel();
if (!$payment instanceof PaymentInterface) {
return;
}
$event->getContext()->getGateway()->execute($status = new GetHumanStatus($payment));
if (!$payment->isCustomPaymentEventDispatched()) {
if ($request instanceof Capture && $status->isCaptured())
{
$payable = $payment->getPayable();
$this->logger->info('Dispatching PAYMENT_SUCCESS for '.$payable->__toString());
$tmEvent = new PayablePaymentStatusChangeEvent($payment->getPayable());
$eventDispatcher->dispatch(PaymentEvents::PAYMENT_PENDING, $tmEvent);
$payment->markAsCustomPaymentEventDispatched();
}
elseif ($request instanceof Authorize && $status->isAuthorized())
{
$payable = $payment->getPayable();
$this->logger->info('Dispatching PAYMENT_PENDING for '.$payable->__toString());
$tmEvent = new PayablePaymentStatusChangeEvent($payment->getPayable());
$eventDispatcher->dispatch(PaymentEvents::PAYMENT_PENDING, $tmEvent);
$payment->markAsCustomPaymentEventDispatched();
}
}
}
}
因此,尝试修复1是将textField.removeFocusListener(null)和textField.removeActionListener(null)放在两个textField.add ...行之前。尝试修复没有任何改变1.尝试修复2是在trigger()之外创建一个布尔触发事件,然后用if(triggeringEvent == false){triggeringEvent = true;}启动trigger()。 ......所有焦点和动作监听线。尝试修复2只是让我没有任何触发。
感谢任何人提供的见解!!!
编辑:我曾尝试过Boann的addChangeListener,但也会不断触发,甚至在值未被更改时触发。
编辑:我认为我通过调整尝试修复2找到了解决方案,但是它失败并且有多个JTextField:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Tester {
private JFrame frame;
private JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Tester window = new Tester();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Tester() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(50, 50, 200, 50);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField = new JTextField("Change this.");
textField.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
trigger();
}
public void removeUpdate(DocumentEvent e) {
trigger();
}
public void changedUpdate(DocumentEvent e) {
trigger();
}
public void trigger() {
textField.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
System.out.println("You changed the text to: " + textField.getText());
}
});
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You changed the text to: " + textField.getText());
}
});
}
});
frame.add(textField);
}
}
你可以在这里看到。如果你开始在字段之间跳跃,那么它会触发一大堆额外的时间。它适用于您尝试调整的第一个,但随后失败。
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Tester {
private JFrame frame;
private JTextField textField;
private boolean eventTrigger = false;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Tester window = new Tester();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Tester() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(50, 50, 200, 50);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField = new JTextField("Change this.");
textField.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
if(eventTrigger == false) {
trigger();
}
}
public void removeUpdate(DocumentEvent e) {
if(eventTrigger == false) {
trigger();
}
}
public void changedUpdate(DocumentEvent e) {
}
public void trigger() {
eventTrigger = true;
textField.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
System.out.println("You changed the text to: " + textField.getText());
eventTrigger = false;
}
});
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You changed the text to: " + textField.getText());
eventTrigger = false;
}
});
}
});
frame.add(textField);
}
}