如果我输入documentListener
并匹配IPv4格式的正则表达式,我想创建一个<{1>} ,它将更改JTextField : txt_ip4class
内容。
如果内容被更改(JComboBox : box_ip4class
),IP类的范围将被写入itemStateChanged
,和位用法将写入`JTextField:txt_bitclass ``*。
我已成功使用此方法,但使用经典方法(使用JTextField : txt_rangeclass
),但我使用jButton
在此方法中失败了。仅documentListener
已更改。
我的代码,将JComboBox : box_ip4Class
应用于documentListener
:
txt_ip4class
txt_ip4class.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
logUpdate();
}
@Override
public void removeUpdate(DocumentEvent e) {
logUpdate();
}
@Override
public void changedUpdate(DocumentEvent e) {
logUpdate();
}
void logUpdate() {
Runnable doHighlight = new Runnable() {
@Override
public void run() {
String aTxt = txt_ip4class.getText();
classValue = 1;
if (!aTxt.matches("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}\\b")) {
classValue = 0;
} else if (aTxt.matches("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}\\b")) {
try {
classW.classMain(classValue, box_ip4class, txt_ip4class, txt_rangeclass, txt_bitclass);
} catch (IllegalStateException e) {
}
}
}
};
SwingUtilities.invokeLater(doHighlight);
}
});
包含两个条件,如果为零则会设置基于classW.Main
的其他字段(jComboBox
将被替换),如果应设置txt_ip4class
则选择一个选项。从该选项中,它应该像选项零
完整代码(我将这些文件从Netbeans项目导出到zip ):
完整异常堆栈跟踪是here
或
jComboBox
===============编辑1 ============
感谢@MarkRotteveel 并this post
此问题已修复,但存在另一个问题..
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification
at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1338)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:658)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1669)
at engines.ClassW.setFromCombo(ClassW.java:140)
at interfaces.Netgui$1.logUpdate(Netgui.java:63)
at interfaces.Netgui$1.insertUpdate(Netgui.java:41)
at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:201)
at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:748)
at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:707)
at javax.swing.text.PlainDocument.insertString(PlainDocument.java:130)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:669)
at javax.swing.text.JTextComponent.replaceSelection(JTextComponent.java:1328)
at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(DefaultEditorKit.java:884)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1663)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2882)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2929)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2845)
at java.awt.Component.processEvent(Component.java:6310)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
at java.awt.Component.dispatchEventImpl(Component.java:4760)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 19 seconds)
中的代码循环运行。
===============编辑2 ============
更新了代码。 当条件不等于正则表达式时,doHighlight不再循环。 但是当条件等于时仍然在做循环。