[英]show the 1st and 2nd value of object in comboBox
I have a combobox with list of authors and with change in combobox I have to show the author's detail in table using java swing. 我有一个带有作者列表的组合框,并且随着组合框的更改,我必须使用java swing在表中显示作者的详细信息。 I did like:
我喜欢:
for(Author author: Application.authors){
jComboBoxAuthors.addItem(author);
}
and with change in item selected : 并更改所选项目:
if(jComboBoxAuthors.getSelectedIndex()>0){
Author author = (Author)e.getItem();
String name = author.getFirstName()+" "+author.getLastName();
}
It shows object in combo but i need the name only and if I dojComboBoxAuthors.addItem(author.getFirstName());
它以组合形式显示对象,但是如果我需要名称,则只需要我
dojComboBoxAuthors.addItem(author.getFirstName());
I can't get value in table ie. 我不能在表中获得价值,即。 name return nothing.
名字什么也没返回。 How can I fix this issue?
如何解决此问题?
Using a custom renderer will break the default functionality of a JComboBox. 使用自定义渲染器将破坏JComboBox的默认功能。 That is you will no longer be able to select an item using the keyboard.
那就是您将不再能够使用键盘选择一个项目。
Check out Combo Box With Custom Renderer for more information and a more complete solution that shows how to fix this problem. 请查看带有“自定义渲染器”的组合框,以获取更多信息和更完整的解决方案,其中显示了如何解决此问题。
One thing you can do is override the toString() method as following 您可以做的一件事是重写toString()方法,如下所示
@Override
public String toString() {
return firstName+" "+lastName; // so that name will be displayed instead of default object
}
But it has its limitations. 但是它有其局限性。 Hope it helps
希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.