JTextField 显示“...”当内容太长时
我试图让 JTextField
当其内容比显示区域长时显示点。我希望有与 JTable
相同的行为,当我调整列大小时,里面的文本会发生变化 - 当它很长时,只显示它的开头,然后有三个点(看看我的内容)请记住,请运行此链接中的第一个示例并更改列尺寸 - 我不被允许发布图片,因为我是新来的;))
可以吗?我想到的唯一解决方案是使用一个具有附加字段 oryginalText
的类来扩展 JTextField
,并且其行为会像那样(没有测试它,这只是一个建议,JtextField
的尺寸不会改变):
import javax.swing.JTextField;
public class MyTextField extends JTextField {
private String oryginalText;
private int length;
@Override
public void setText(String text) {
oryginalText = text;
if (oryginalText.length() > length)
super.setText(oryginalText.substring(0, length - 2) + "..");
else
super.setText(oryginalText);
}
}
有什么想法吗?
I'm trying to make JTextField
display dots when its content is longer then display area. I would like to have the same behaviour like in JTable
, where when I resize column the text inside changes - when its to long only begining of it is displayed and then there are three dots (to see what I have in mind please run first example from this link and change column size - I'm not allowed to post images because I'm new here;) )
Is is possible? Only solution that I have in mind is extending JTextField
with a class that will have additional field oryginalText
and will behave like that (didn't test it, it's just a proposal, dimension of the JtextField
will not change):
import javax.swing.JTextField;
public class MyTextField extends JTextField {
private String oryginalText;
private int length;
@Override
public void setText(String text) {
oryginalText = text;
if (oryginalText.length() > length)
super.setText(oryginalText.substring(0, length - 2) + "..");
else
super.setText(oryginalText);
}
}
any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于扩展 JTextField 并存储原始(未修剪)值的想法:
将文本字段显示为禁用(或不可编辑)并带有修剪文本。当用户单击文本字段时,使其启用/可编辑并显示整个原始文本。
然后,当用户按 Enter 或焦点退出文本字段时:禁用/不可编辑和修剪文本。
Over your idea of extending JTextField and storing the original (non trimmed) value:
Show the textfield as disabled (or not editable) and with trimmed text. When the user clics the textfield make it enabled/editable and show the whole original text.
Then again when the user press enter or the focus exits of the textfield: disable/not editable and trimmed text.