swing,在可编辑的 jTable 上显示 JList 以选择条目以自动完成文本
我有一个带有可编辑单元格的 JTable。每个 Cell 都包含一个 CarretListener,用于快速验证输入的文本。但在一个特殊的单元格中,您应该能够从列表中选择条目。当您输入文本时会生成该列表。该程序在列表中搜索条目,因此输入的文本相等,就像谷歌建议的那样。到目前为止一切都很好。但我不知道如何在正确的位置显示列表。我尝试了 GlassPane,但效果不太好。我在获取单元格坐标和显示 JList 时遇到问题。 设置行高以显示整个列表也不起作用,因为我不想更改整行。 也许 TableCellRenderer 中有一个技巧等等......? 我不需要完整的源代码,但我需要朝着正确的方向推动。
这是该程序的图片及其外观:http://img198。 imageshack.us/img198/3227/sosollsseinh.jpg 感谢您的关注,
马克
I have a JTable with editable cells. Each Cell contains a CarretListener for quick validation of the entered text. But in one special cell you should be able to select entrys out of a list. The List is generated when you enter a text. The Programm serach in a list for entrys equal so the entered text, like google suggest. So far its all good. But i dont get it how to show the list at the right position. I tried the GlassPane but this doesn't work so well. I have problems to get the Cordinates of the cell and to show the JList.
Set the row height so show the whole list also dosn't work because i don't want to change the whole row.
Maybe there is a trick in the TableCellRenderer or so...?
I don't want a complete sourcecode or so, but a I need a push in the right direction.
Here is a pic of the programm and hwo it should look like: http://img198.imageshack.us/img198/3227/sosollsseinh.jpg
Thanks for your attention
Marc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非您将特定行为应用于列表,否则您可以尝试另一种方法,添加一个组合框作为该表的编辑器。检查 DefaultCellEditor 获取示例。
我认为,如果您在调用组合框时使用正确的值修改组合框的内容,它将满足您的需求。为此,您需要制作自己的 CellEditor,最有可能通过实现 TableCellEditor,这样您就可以根据调用的时间(在方法 getTableCellEditorComponent() 中)更改其中的值。
编辑:关于您在评论中谈论的KeyListener问题,您必须认为在JTextField中更新实际文本之前发送了Key事件。因此,对
getText()
的调用返回不带新字符的值是正常的。但是,由于这是 KeyEvent< /a>,您可以直接使用
evt.getKeyChar()
或evt.getKeyCode()
访问键入的字符,以检查这是否确实是一个字母被输入。通过这些方法,您可以知道您需要的完整“文本”。另外,从您评论中的视频来看,您似乎想要一个实际上根据用户开始输入的内容进行调整的列表,并根据已输入的内容限制选择。
如果您能够(并且被允许)使用额外的库,我建议您查看 SwingX 组件 (http://swinglabs.org/)。该库总体上建议在 Swing 界面中使用大量有用的组件。他们的网站上有一个演示,尽管现在似乎不可用,也许稍后才能使用。
在他们的包“autocomplete”中,您将能够找到一个名为“AutoCompleteDecorator”的类,以及其他有用的类,这将允许您改进您的组合框编辑器,以便它会尝试完成用户输入本身,然后滚动到列表中的合适位置(我认为它也可以过滤列表,您想要的确切行为,但我不完全确定)。如果您可以使用它,它实际上可以省去您自己处理插入符号事件以及更新列表的麻烦,因为它很可能会为您做这件事。
您可以在他们的网站上下载 .Jar 和 javadoc。 这是一份副本来自另一个站点的 javadoc,用于自动完成包,尽管它可能同时发生变化,但它会给您一个想法。
Unless you apply particular behavior to your list, you could try another way, by adding a combobox as editor of this table. Check DefaultCellEditor for an example of it.
If you modify the content of the combobox when it's called, with the correct values, it will match your need, I think. For this, you will need to make your own CellEditor, most likely by implementing TableCellEditor, so that you will be able to change the values from it depending on when it is called (in the method getTableCellEditorComponent()).
Edit: About the KeyListener problem you are talking about in comment, you have to think that a Key event is sent before the actual text is updated in the JTextField. So it is normal that your call to
getText()
returns the value without the new character.However, since this is a KeyEvent, you have access to the typed character, with
evt.getKeyChar()
directly, orevt.getKeyCode()
, to check if this is actually a letter which was typed. With these methods, you can know the complete "text" that you need.Also, from the video in your comment, it seems that you want a list which is in fact adapting according to what the user started typing, and restraining choice according to what was already entered.
If you are able (and allowed) to use extra libraries, I would recommend you to have a look at SwingX components (http://swinglabs.org/). This library proposes in general plenty of useful components to use in swing interfaces. There is a demo on their site, though it seems to not be available at this hour, maybe a later day.
In their package "autocomplete", you will be able to find a class named "AutoCompleteDecorator", and other useful ones, which will allow you to improve your Combobox editor, so that it will try to complete the user input itself, and scroll to the good position in the list (I think it can also filter the list, the exact behavior you want, but I'm not fully sure). If you can use this, it would actually save you the hassle of taking care of the caret events yourself, as well as updating the list, as it would most likely do it for you.
You can download the .Jar and the javadoc on their site. Here is a copy of the javadoc from another site, for the autocomplete package, though it could have changed in the meantime, but it will give you an idea.
难道不能将其作为
JCombobox
而不是JList
吗?那会更容易不。由于DefaultCellEditor
支持开箱即用的JCombobox
,Can't you just as a
JCombobox
instead of aJList
? That would be easier no. AsDefaultCellEditor
supportsJCombobox
out of the box,