简体   繁体   English

如何在Java Swing中给JCombobox项两种形式

[英]How to Give Two Forms to JCombobox Items in Java Swing

i am stuck with a new problem, don't know if this works but here i have list of JCombobox as follow. 我陷入了一个新问题,不知道这是否JCombobox ,但是在这里,我列出了JCombobox如下。

JCombobox comboBox = new JComboBox();
    comboBox.addItem("UserName");
    comboBox.addItem("Password");
    comboBox.addItem("DLNo 20 b");
    comboBox.addItem("DLNo 20 b");

i want to print my database column names which are more than 40! 我想打印40多个数据库column names

when i select the Combobox it must internally print my custom item here. 当我选择组合Combobox ,必须在此处内部打印我的自定义项目。

Here i tried with this code but i am not satisfied with this 在这里,我尝试使用此代码,但对此不满意

            if(comboBox.getSelectedIndex()==0)
            {
                System.out.println("U_NAME");
            }

            if(comboBox.getSelectedIndex()==1)
            {
                System.out.println("P_NAME");   
            }

            if(comboBox.getSelectedIndex()==2)
            {
                System.out.println("DL_NO_20_b");   
            }

            if(comboBox.getSelectedIndex()==3)
            {
                System.out.println("DL_NO_20_b");   
            }

is there any better way to over come this, like mapping objects 有没有更好的方法可以克服这个问题,例如mapping objects

You could create a class ComboBoxItem with a name- and a columnName-attribute. 您可以创建一个具有name-和columnName-attribute的类ComboBoxItem。 Use instances of this class for the ComboBox. 将此类的实例用于ComboBox。

In the ComboBoxItem-class, overwrite the toString()-method to return the name, so it gets displayed as wished in the ComboBox. 在ComboBoxItem类中,覆盖toString()方法以返回名称,以便在ComboBox中按需要显示它。 Add a getColumnName()-method to return the columnName, so you could invoke getSelectedItem().getColumnName(). 添加getColumnName()方法以返回columnName,以便可以调用getSelectedItem()。getColumnName()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM