问题描述
我有一个可编辑的JComboBox和JTextField.两者都带有自定义文档.这是代码:
I have an editable JComboBox and JTextField. Both with custom Documents. Here is the code:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class SwingUtilStrangeBehav extends JFrame {
public SwingUtilStrangeBehav() {
JComboBox<String> combo = new JComboBox<>(new String[]{"a", "b", "c"});
combo.setEditable(true);
((JTextField)combo.getEditor().getEditorComponent()).setDocument(new PlainDocument() {
private static final long serialVersionUID = 1L;
@Override
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
System.out.println("New text inserted into combo!");
super.insertString(offs, str, a);
}
});
JTextField field = new JTextField();
field.setDocument(new PlainDocument() {
private static final long serialVersionUID = 1L;
@Override
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
System.out.println("New text inserted into text!");
super.insertString(offs, str, a);
}
});
Container c = getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
c.add(combo);
c.add(Box.createRigidArea(new Dimension(0, 5)));
c.add(field);
//SwingUtilities.updateComponentTreeUI(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String arg[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new SwingUtilStrangeBehav();
}
});
}
}
然后我在JComboBox或JTextField中输入一些文本,例如,在控制台中获得以下输出:
Then I input some text in JComboBox or JTextField I get the following output in my console, for example:
新文本已插入组合!
在文本中插入了新文本!
太好了!当取消注释以下行 SwingUtilities.updateComponentTreeUI(this); 并运行该程序时,我只能得到以下输出:
在文本中插入了新文本!
That's great! When I uncomment the following line SwingUtilities.updateComponentTreeUI(this); and run this programm, I can only get this output:
New text inserted into text!
似乎JComboBox的文档已删除.为什么要删除自定义文档以及如何解决此问题?我希望自定义文档在执行 SwingUtilities.updateComponentTreeUI(this); 后仍将位于JComboBox中.
It seems that JComboBox's Document was removed. Why was custom Document removed and how to solve this problem? I want that custom Document would still be in JComboBox after executing SwingUtilities.updateComponentTreeUI(this);.
仅供参考:我使用 SwingUtilities.updateComponentTreeUI(this); 将新字体应用于容器.
FYI: I use SwingUtilities.updateComponentTreeUI(this); to apply new Font to Container.
推荐答案
之所以发生这种情况,是因为JComboBox的编辑器是由其UI-Delegate(即XXComboBoxUI)控制的:随着updateUI设置新的ui,受控的编辑器也将被替换.几个选项:
This happens because the JComboBox's editor is controlled by its UI-Delegate, that is XXComboBoxUI: as updateUI sets a new ui, the controlled editor is replaced as well. A couple of options:
- 在comboBox上注册一个propertyChangeListener,以便在替换编辑器时得到通知,并再次设置自定义文档
- 安装自定义编辑器(而不是仅安装自定义文档)
- (可能不是一个选择,以防万一)不要使用updateUI,而是在容器中设置字体
这篇关于'SwingUtilities.updateComponentTreeUI(this)'从JComboBox中删除自定义文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!