JComboBox的值 [英] Values of JComboBox

查看:140
本文介绍了JComboBox的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以定义与JComboBox中的实际内容不同的值?

在HTML中,它看起来如下:

Is it possible, to define values different from the actual content in a JComboBox?
In HTML it looks as follows:

<select>
  <option value="value1">Content1</option>
  <option value="value2">Content2</option>
  <option value="value3">Content3</option>
</select>

无论内容有多长,这里都可以获得一个简短的价值。


在Java中我只知道以下解决方案:

Here it's possible to get a short value, no matter how long its content is.

In Java I only know the following solution:

// Creating new JComboBox with predefined values
   String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; 
   private JComboBox combo = new JComboBox(petStrings);

// Retrieving selected value
   System.out.println(combo.getSelectedItem());

但在这里我只会得到猫,狗等。


问题是,我想将数据库中客户的所有名称加载到JComboBox中,然后从所选客户中检索ID。它应如下所示:

But here I only would get "Cat", "Dog", etc.

The problem is, that I want to load all names of the customers from a database into the JComboBox and then retrieve the ID from the selected customer. It should look like this:

<select>
  <option value="104">Peter Smith</option>
  <option value="121">Jake Moore</option>
  <option value="143">Adam Leonard</option>
</select>


推荐答案

如果您创建一个Customer类并加载一个列表客户反对组合框然后你会得到你想要的。组合框将显示对象的toString(),因此Customer类应返回toString()中的名称。当您检索所选项目时,它是一个Customer对象,您可以从中获取ID或其他任何内容。

If you create a Customer class and load a list of Customer objects into the combobox then you will get what you want. The combo box will display the toString() of your object, so the Customer class should return the name in toString(). When you retrieve the selected item its a Customer object from which you can get the id or whatever else you want.

此处是一个例子来说明我的建议。但是,当你得到这个基本想法时,遵循kleopatra和mKorbel的建议是个好主意。

Here is an example to illustrate what I am suggesting. However, it would be a good idea to follow kleopatra's and mKorbel's advice when you get this basic idea working.

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ComboJumbo extends JFrame{

    JLabel label;
    JComboBox combo;

    public static void main(String args[]){
        new ComboJumbo();
    }

    public ComboJumbo(){
        super("Combo Jumbo");
        label = new JLabel("Select a Customer");
        add(label, BorderLayout.NORTH);

        Customer customers[] = new Customer[6];
        customers[0] = new Customer("Frank", 1);
        customers[1] = new Customer("Sue", 6);
        customers[2] = new Customer("Joe", 2);
        customers[3] = new Customer("Fenton", 3);
        customers[4] = new Customer("Bess", 4);
        customers[5] = new Customer("Nancy", 5);

        combo = new JComboBox(customers);
        combo.addItemListener(new ItemListener(){

            public void itemStateChanged(ItemEvent e) {
                Customer c = (Customer)e.getItem();
                label.setText("You selected customer id: " + c.getId());
            }

        });
        JPanel panel = new JPanel();
        panel.add(combo);
        add(panel,BorderLayout.CENTER);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 200);
        setVisible(true);
    }


    class Customer{
        private String name;
        private int id;

        public Customer(String name, int id){
            this.name = name;
            this.id = id;
        }

        public String toString(){
            return getName();
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }
 }

这篇关于JComboBox的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆