带有输入提示的 Java JTextField
我想向我的 javax.swing.JTextField
添加提示值。它应该看起来像 的 Firefox 渲染。这将创建一个背景为文本“bla”的编辑字段。如果文本框具有焦点,标题文本就会消失,如果用户离开编辑框而没有文本,标题文本就会重新出现。
是否有(免费)摆动组件可以执行类似的操作?
I would like to add a hint value to my javax.swing.JTextField
. It should look like Firefox rendering of <input type="text" title="bla">
. This creates an edit field with the text 'bla' in the background. If the textbox has focus the title-text disappears and just reappears if the user leaves the editbox without text.
Is there a (free) swing component that does something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以创建自己的:
如果您仍在使用 Java 1.5,请将
this.getText().isEmpty()
替换为this.getText().length() == 0< /代码>。
You could create your own:
If you're still on Java 1.5, replace the
this.getText().isEmpty()
withthis.getText().length() == 0
.这是一个在任何 L&F 中看起来都不错的简单方法:
Here is a simple way that looks good in any L&F:
这是一个单类复制/粘贴解决方案:
像这样使用它:
请注意,它发生在
protected void PaintSafely(Graphics g)
中。Here is a single class copy/paste solution:
Use it like this:
Note that it is happening in
protected void paintSafely(Graphics g)
.看一下这个:http://code.google.com/p/xswingx/
顺便说一句,自己实现它并不是很难。几个监听器和自定义渲染器,瞧。
Take a look at this one: http://code.google.com/p/xswingx/
It is not very difficult to implement it by yourself, btw. A couple of listeners and custom renderer and voila.
对于任何 Swing 组件(即任何扩展 JComponent 的组件),您都可以调用 setToolTipText(String) 方法。
有关详细信息,请参考以下链接:
For any Swing component (that is, anything that extends JComponent), you can call the setToolTipText(String) method.
For more information, reference the following links:
查看 WebLookAndFeel https://github.com/mgarin/weblaf/
Have look at WebLookAndFeel at https://github.com/mgarin/weblaf/
如果您仍在寻找解决方案,这里结合了其他答案(Bart Kiers 和 culmat)供您参考:
If you still look for a solution, here's one that combined other answers (Bart Kiers and culmat) for your reference:
这可以通过使用焦点侦听器更新文本字段内容来实现。
使类实现焦点侦听器接口:
添加一个方法来捕获获得焦点时的空白字段:
添加一个方法来捕获失去焦点时的捕获,以便在字段为空时重新显示默认条目:
将您的类注册为焦点侦听器对于文本字段:
了解更多信息如何编写焦点侦听器 在 Java 教程中。
This can be achieved by using a focus listener to update the text field content.
Make the class implement the focus listener interface:
Add a method to catch when focus is gained that blanks the field:
Add a method to catch when focus is lost to redisplay the default entry if the field was blank:
Register your class as the focus listener for text field:
Learn more at How to Write a Focus Listener in the Java Tutorials.
这是一个基于 Adam Gawne-Cain 早期发布的完整示例。他的解决方案很简单,而且效果非常好。
我在多个字段的网格中使用了以下文本:
这使得可以轻松验证提示文本的 x/y 对齐方式。
一些观察结果:
- 有很多解决方案,但许多只能表面上工作并且/或者有错误
- sun.tools.jconsole.ThreadTab.PromptingTextField 是一个简单的解决方案,但它仅在 Field 没有焦点时显示提示文本它是私人的,但没有什么是一点点剪切和粘贴就解决不了的。
以下内容适用于 JDK 8 及更高版本:
Here is a fully working example based on Adam Gawne-Cain's earlier Posting. His solution is simple and actually works exceptionally well.
I've used the following text in a Grid of multiple Fields:
this makes it possible to easily verify the x/y alignment of the hinted text.
A couple of observations:
- there are any number of solutions out there, but many only work superficially and/or are buggy
- sun.tools.jconsole.ThreadTab.PromptingTextField is a simple solution, but it only shows the prompting text when the Field doesn't have the focus & it's private, but nothing a little cut-and-paste won't fix.
The following works on JDK 8 and upwards:
我创建了一个自定义类,当组件失去焦点且文本为空时,该类会在 JTextArea 内显示输入提示。此实现还可以防止听众收到显示提示文本时出现的不需要的通知。这可能也适用于 JTextFields,但我还没有测试过。
I created a custom class that shows an input hint inside a JTextArea when the component is out of focus and the text is empty. This implementation also prevents listeners from receiving unwanted notifications that occur when the hint text is displayed. This may work for JTextFields as well but I haven't tested it.