- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:您认为可能重复的问题的解决方案( java.lang.IllegalStateException while using Document Listener in TextArea, Java )正是我在代码中输入的解决此问题的方法。我在编写自定义 DocumentListener 时使用了 3 个重写方法。然而,这并没有解决问题。
出于某种原因,使用 actionListener
时效果很好。 (参见此处 - Update JTextField.addActionListener without pressing "enter")。 actionListener
的问题每次我想要更新标签时,我都必须按“Enter”键。所以有人建议使用DocumentListener
反而。做完这件事。我遇到了一个我无法弄清楚的错误,以及非常奇怪的行为。
由于代码太大,无法在此处发布,我已压缩该项目(与 Intellij 和 JRE 1.8 一起使用)> https://www.dropbox.com/s/pf4hiuk9y0jby7y/FF7LevelUpStatCalculator.zip?dl=0
隔离了问题发生位置的代码块(以便快速查看):
private void setHpBaseStatsTextFieldAction(){
hpBaseStatsTextField.getDocument().addDocumentListener(
new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
updateStatGridLabels();
}
public void removeUpdate(DocumentEvent e) {
updateStatGridLabels();
}
public void changedUpdate(DocumentEvent e) {
//Plain text components do not fire these events
}
public void updateStatGridLabels() {
String currCharacter = charSelCombo.getSelectedItem().toString();
String checkBaseHpInGui = hpBaseStatsTextField.getText();
int baseHpInGui = 0;
if (isInteger(checkBaseHpInGui)){
baseHpInGui = Integer.parseInt(checkBaseHpInGui);
}
if ((!(currCharacter.equals(guiCharSelDefaultValue[unselectedDefaultElement]))) && (isInteger(checkBaseHpInGui))){
characters[getSelectedCharactersIndex()].setBaseHp(baseHpInGui);
hpBaseStatsTextField.setText(Integer.toString(characters[getSelectedCharactersIndex()].getBaseHp()));
setHpStatGridRowValues();
}
}
}
);
}
重现问题:
我注意到它在尝试访问对象并执行 setValue() method
时发出提示。在线 #1338...有人知道为什么会发生这种情况吗?
更新:
DocumentFilter 似乎做得正确,只要我注释掉一行(检查最大值规则时需要)。这是迄今为止的代码:
private void setHpBaseStatsTextFieldAction(){
((AbstractDocument) hpBaseStatsTextField.getDocument()).setDocumentFilter(
new DocumentFilter() {
public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
String text, AttributeSet attrs) throws BadLocationException {
if (offset >= fb.getDocument().getLength()) {
System.out.println("Added: " + text);
} else {
String old = fb.getDocument().getText(offset, length);
System.out.println("Replaced " + old + " with " + text);
}
super.replace(fb, offset, length, text, attrs);
updateStatGridLabels();
}
public void insertString(DocumentFilter.FilterBypass fb, int offset,
String text, AttributeSet attr) throws BadLocationException {
System.out.println("Added: " + text);
super.insertString(fb, offset, text, attr);
updateStatGridLabels();
}
public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
throws BadLocationException {
System.out.println("Removed: " + fb.getDocument().getText(offset, length));
super.remove(fb, offset, length);
updateStatGridLabels();
}
public void updateStatGridLabels() {
String currCharacter = charSelCombo.getSelectedItem().toString();
String checkBaseHpInGui = hpBaseStatsTextField.getText();
int baseHpInGui = 0;
if (isInteger(checkBaseHpInGui)){
baseHpInGui = Integer.parseInt(checkBaseHpInGui);
}
if ((!(currCharacter.equals(guiCharSelDefaultValue[unselectedDefaultElement]))) && (isInteger(checkBaseHpInGui))){
characters[getSelectedCharactersIndex()].setBaseHp(baseHpInGui);
//hpBaseStatsTextField.setText(Integer.toString(characters[getSelectedCharactersIndex()].getBaseHp()));
setHpStatGridRowValues();
}
}
}
);
}
我仍然需要一些帮助。
更新 2:我开始使用 SwingUtilities.invokeLater
正如 camickr 所建议的那样。它与我在上面的代码片段中注释掉的行一起工作,但是,它将它设置为无限循环......
private void setHpBaseStatsTextFieldAction(){
((AbstractDocument) hpBaseStatsTextField.getDocument()).setDocumentFilter(
new DocumentFilter() {
boolean newTextReplaceSet = false;
public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
String text, AttributeSet attrs) throws BadLocationException {
if (offset >= fb.getDocument().getLength()) {
System.out.println("Added: " + text);
} else {
String old = fb.getDocument().getText(offset, length);
System.out.println("Replaced " + old + " with " + text);
}
super.replace(fb, offset, length, text, attrs);
updateStatGridLabels();
newTextReplaceSet = true;
}
boolean newTextInsertSet = false;
public void insertString(DocumentFilter.FilterBypass fb, int offset,
String text, AttributeSet attr) throws BadLocationException {
System.out.println("Added: " + text);
super.insertString(fb, offset, text, attr);
updateStatGridLabels();
newTextInsertSet = true;
}
boolean newTextRemoveSet = false;
public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
throws BadLocationException {
System.out.println("Removed: " + fb.getDocument().getText(offset, length));
super.remove(fb, offset, length);
updateStatGridLabels();
newTextRemoveSet = true;
}
public void updateStatGridLabels() {
String currCharacter = charSelCombo.getSelectedItem().toString();
String checkBaseHpInGui = hpBaseStatsTextField.getText();
int baseHpInGui = 0;
if (isInteger(checkBaseHpInGui)){
baseHpInGui = Integer.parseInt(checkBaseHpInGui);
}
if ((!(currCharacter.equals(guiCharSelDefaultValue[unselectedDefaultElement]))) && (isInteger(checkBaseHpInGui))){
characters[getSelectedCharactersIndex()].setBaseHp(baseHpInGui);
if (!newTextReplaceSet || !newTextInsertSet || newTextRemoveSet) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("run");
hpBaseStatsTextField.setText(Integer.toString(characters[getSelectedCharactersIndex()].getBaseHp()));
}
});
}
//hpBaseStatsTextField.setText(Integer.toString(characters[getSelectedCharactersIndex()].getBaseHp()));
setHpStatGridRowValues();
}
}
}
);
}
我似乎无法正确设置 boolean 标志的组合来阻止它进行无限循环,但仍然执行行 System.out.println("run");
JTextField 中所做的每一个更改。如果我更改 if statement
来自if (!newTextReplaceSet || !newTextInsertSet || newTextRemoveSet)
至if (!newTextReplaceSet || newTextInsertSet || newTextRemoveSet)
,它将从执行 System.out.println("run");
开始使用无限循环,只执行一次(并且当对 JTextField
进行另一次更改时不会再次执行)。谁能帮我吗?
最佳答案
For some reason, this worked okay when using actionListener
这是因为 ActionListener 代码是在 Document
更新后调用的。
I used the 3 override methods when writing the custom DocumentListener. However, this did not solve the problem
问题是您无法更改 DocumentListener 中的 Document
,因为 Document
尚未使用更改的文本进行更新。
真正的问题是,当您在文本字段中输入文本时,为什么要尝试更新文本字段?
如果您确实需要这样做,那么有两种常见的解决方案:
不要使用 DocumentListener。相反,您可以使用 DocumentFilter
,它允许您在添加/删除测试时操作文档。
您需要延迟更新文档。执行此操作的方法是使用 SwingUtilities.invokeLater(...)
。这会将代码放置在事件调度线程 (EDT)
的末尾。
baseHp cannot iterate above 9999).
然后这是基于编辑的逻辑,应该在 DocumentFilter 中完成。或者您甚至可以使用 JFormattedTextField 或 JSpinner。
阅读Swing Tutorial 。有使用 JFormattedTextField 和 JSpinner 的示例。您还可以在“文本组件功能”部分中找到 DocumentFilter 的示例。
编辑:
however, it sets it off into an infinite loop...
首先您需要了解使用 DocumentListener 和 DocumentFilter 之间的区别。
DocumentListener 用于在文档中添加/删除数据后在应用程序中进行处理
DocumentFilter 用于在将数据添加到文档之前编辑数据
但是,这两种情况下的循环问题是相同的,解决方案也是相同的。
问题是你:
因此,解决方案是在调用 setText(...) 方法之前删除 Listener,然后恢复 Listener。
doc.removeDocumentListener( this );
textfield.setText(...);
doc.addDocumentListener( this );
虽然我想说实际更改输入的数据是不正常的。通常,您会验证数据并在出现错误时显示一条消息,而不是尝试修复数据并重置文本。通过不更改文本字段,您不必担心导致无限循环。
这就是为什么您会使用 JFormattedTextField 或 JSpinner 作为编辑组件。您可以轻松地将数据强制为具有最大位数的数字。
关于java - JTextField DocumentListener 异常变异错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43738710/
//java swing 新手,需要帮助获取第一个 Jtextfield 中的文本以显示在//第二个 jtextfield 中???我很年轻,刚刚开始接触java,需要一些帮助。下面是我已经完成的代码
我正在制作一个用户界面,其中有两个 jtextfields。我想要的是当用户输入第一个jtextfield 它应该以 1 的增量镜像在第二个 jtextfield 中,并且如果用户在第二个 Jtext
我正在设计许可证激活部分。它在 Java Swing 中有两个文本字段。 Java代码: jTextField1.setDocument(new JTextFieldLimit(8)); jTex
我需要将文本从 JFrame 中的 JTextField 传递到另一个 JFrame 中的第二个 JTextField。 GUI 是使用 GUI Editor netbeans 6.9 创建的。 最佳
在从另一个 JTextField 读取一个 String 后,在另一个类中更新一个 JTextField 时遇到一些问题。这是有问题的方法: public JTextField buyVowel(pl
我正在制作身份验证 GUI,它应该包含 2 个文本字段,用户名 JTextField 和密码 JPasswordField。我想让密码字段位于用户名字段下方。我目前的代码如下: public clas
当鼠标在该文本字段中单击时,我需要让该程序清除文本字段中的文本。我尝试了一些方法,但还没有一个对我有用。 完整代码如下: public class TimerClassPanel extends JF
我目前正在学习如何在 Java 中使用 Swing。将 JTextFields、JLabels 或 JButtons 添加到 JPanel 时,我通常用 4 行来完成,如下所示: gbc.gridx
我不确定之前是否已经回答过这个问题;我尝试搜索看看是否有,但没有成功。 我正在做一项家庭作业,并且必须有两个 JTextField:一个接受输入,第二个反转显示给定的输入。例如,如果有人输入 1234
如何将内容从一个 JTextField 逐个字符复制到另一个不可编辑的 JTextField? 第一个 JTextField 中输入的每个字符都应动态显示在第二个 JTextField 中。 最佳答案
我想找到jtextfields(sht1到sht13)的总和并将结果放入llog jtextfield中,但如果我不在每个jtextfield(sht1到sht13)中放入值,它就不起作用 我如何修复
如何验证文本字段在 Swing 中仅输入 10 位数字的手机号码? 我有三个文本字段。 姓名 联系方式 没有。对于电子邮件 我想点击提交按钮,然后检查姓名、联系方式和电子邮件是否正确。我还想设置字符限
我已将 JPopupMenu 添加到 JTextField。不幸的是,当我从 JTextField 更改焦点时,或者当我“最小化”窗口时,JPopupMenu 仍然可见。如果 JTextField 失
我的 JTextField 遇到问题。我不知道如何将变量从 JTextField (位于 JFrame A 中)传递到另一个 JTextField (位于 JFrame B 中)。我尝试这样做,但它没
我正在用java创建一个小程序,用户在四个不同的四个不同文本字段中输入一些数据,单击一个按钮,然后从这些字段中获取数据并将其存储在旁边的文本字段中。 我制作了一个按钮,它实现了 ActionListe
以下是多文件项目的部分代码。我只发布这段代码,因为我不清楚为什么 setText 方法不起作用。并未包含此项目的所有代码。 import javax.swing.*;
大家好,我有一个连接到 Oracle 数据库的 Swing 应用程序,我希望一旦我在 JTextField 中键入一个值,JFrame 上的其他 JTextfield 就会加载来自数据库的后续数据,但
我有一个名为 Name 的 JTextField 和一个名为 Address 的 JTextArea。我的要求是,当用户在 JTextField 中输入名称并按 Enter 键时,光标应转到下一个文本
在 Netbeans 中,每当 JTextField、JButton 获得焦点时,边框就会被蓝色突出显示覆盖,我如何更改其颜色或完全删除它? 最佳答案 您可以通过右键单击设计 View 中的组件,选择
double B=Double.parseDouble(emp_txt2.getText()); double C=Double.parseDouble(nopay_txt3.getText(
我是一名优秀的程序员,十分优秀!