简体   繁体   English

在comboBox中显示对象的第一个和第二个值

[英]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.

相关问题 如何使用第一个JSON对象覆盖第二个对象JSON - How to override the 2nd Object JSON with 1st JSON object 在 java 中将第一个数组值乘以 1,将第二个值乘以 3 - Multiply 1st array value with 1 and 2nd value with 3 in java 在更改第一微调器值时更改第二微调器值 - changing 2nd spinner values on changing 1st spinner value 以这样的方式更新 HashMap,使第二个值变成第一个,第三个变成第二个,依此类推 - Updating a HashMap in such a way that the 2nd value becomes 1st and 3rd becomes 2nd and so on 关于第一个init()和第二个init() - regarding 1st init() and 2nd init() 检查第一个 if 条件,然后检查第二个 - Checking 1st if condition and then 2nd 给定一组具有2个值的对象。 相对于第一个值,然后使用第二个值对集合进行排序 - Given a set of objects that have 2 values. Sort the set with respect to the 1st value and then with the 2nd value 从第一页获取按钮的值,并将其回显到“模态”和第二页 - Getting the value of the button from the 1st page and echoing it to the Modal and to the 2nd page 当第二阶段对第一阶段的结果值不感兴趣时​​,完成阶段链接 - CompletionStage chaining when 2nd stage is not interested in the 1st stage result value 无法通过getIntent()将第一个活动的textview值传递给第二个活动的textview - Unable to pass 1st activity's textview value to 2nd activity's textview through getIntent()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM