问题描述
我正在研究一个Java应用程序并遇到了一个我自己似乎无法解决的问题。
我设置了 DocumentFilter
on JTextField
仅允许数字输入,但默认值为text。我有一个按钮将 JTextField
重置为默认值,并且由于 DocumentFilter
而无法正常工作。 / p>
我如何克服这个问题?
谢谢
用户只能输入数字数据,但也需要显示非数字值的字段是矛盾的。
文本通过文档
提供给字段,用户或以编程方式对内容的生成方式没有区别。
如果您在该字段中尝试显示提示,则可以在
例如
当字段有焦点时,提示t将被隐藏,但你可以控制它,直到用户输入内容时显示,或者在获得焦点时突出显示。
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.prompt.BuddySupport;
import org.jdesktop.swingx.prompt.PromptSupport;
公共类PromptSupportTest {
public static void main(String [] args){
new PromptSupportTest();
}
public PromptSupportTest(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}
JFrame frame = new JFrame(Testing);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
公共类TestPane扩展JPanel {
public TestPane(){
JTextField firstName = new JTextField(10);
PromptSupport.setPrompt(First Name,firstName);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT,firstName);
JTextField lastName = new JTextField(10);
PromptSupport.setPrompt(姓氏,lastName);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT,lastName);
JTextField picture = new JTextField(10);
PromptSupport.setPrompt(图片,图片);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT,picture);
JButton browse = new JButton(...);
browse.setMargin(new Insets(0,0,0,0));
browse.setContentAreaFilled(false);
browse.setFocusPainted(false);
browse.setFocusable(false);
browse.setOpaque(false);
//为brose按钮添加动作监听器以显示JFileChooser ...
BuddySupport.addRight(浏览,图片);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
add(firstName,gbc);
add(lastName,gbc);
add(picture,gbc);
gbc.anchor = GridBagConstraints.CENTER;
add(new JButton(Ok),gbc);
}
@Override
public Dimension getPreferredSize(){
return new Dimension(200,200);
}
}
}
BuddySupport 的示例,它是同一个API的一部分,它允许您与文本组件伙伴另一个组件。在这里,我已经完成了经典的文件浏览器组合,但我一直在搜索这样的样式字段...
I'm working on a Java application and ran into a problem that I cannot seem to solve on my own.
I've set a DocumentFilter
on JTextField
to only allow digit entries, however, the default values are text. I have a button that resets JTextField
s back to default, and it is not working presumingly because of the DocumentFilter
.
How can I overcome this problem?
Thanks
A field that the user can only enter numeric data into, but also needs to display non-numeric values is contradictive.
Text is supplied to field via the Document
, there is no distinction over how that content is generated, either by the user or programmatically.
If you're trying to display a "prompt" in the field, you can could take a look at PromptSupport
in SwingLabs SwingX Library
For Example
When the fields have focus, the "prompt" will be hidden, but you can control this, making it shown until the user types something or highlight when focus is gained.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.prompt.BuddySupport;
import org.jdesktop.swingx.prompt.PromptSupport;
public class PromptSupportTest {
public static void main(String[] args) {
new PromptSupportTest();
}
public PromptSupportTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JTextField firstName = new JTextField(10);
PromptSupport.setPrompt("First Name", firstName);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, firstName);
JTextField lastName = new JTextField(10);
PromptSupport.setPrompt("Last Name", lastName);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, lastName);
JTextField picture = new JTextField(10);
PromptSupport.setPrompt("Picture", picture);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, picture);
JButton browse = new JButton("...");
browse.setMargin(new Insets(0, 0, 0, 0));
browse.setContentAreaFilled(false);
browse.setFocusPainted(false);
browse.setFocusable(false);
browse.setOpaque(false);
// Add action listener to brose button to show JFileChooser...
BuddySupport.addRight(browse, picture);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
add(firstName, gbc);
add(lastName, gbc);
add(picture, gbc);
gbc.anchor = GridBagConstraints.CENTER;
add(new JButton("Ok"), gbc);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
I've also added an example of BuddySupport
which is part of the same API, which allows you to "buddy" another component with a text component. Here I've done the classic "file browser" combination, but I do "search" style fields like this all the time...
这篇关于强制JTextField为字符串值,而DocumentFilter仅允许数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!