JComboBox使用DefaultComboBoxModel修改索引

时间:2012-12-29 05:01:48

标签: java mysql swing jcombobox

我创建了JComboBox并使用DefaultComboBoxModel从数据库填充了其内容。

以下是代码:

DefaultComboBoxModel model = new DefaultComboBoxModel();
PreparedStatement statement = con.prepareStatement("SELECT _fid, fruit_name FROM fruits;");

ResultSet result = statement.executeQuery();
while (result.next()) {
    model.addElement(result.getString(2));
}
comboBox = new JComboBox(model);

如何设置JComboBox的索引值为_fid

我对Java& MySQL和现在我没有一个有用的想法。

3 个答案:

答案 0 :(得分:2)

  

它仍然是相同的,在类Fruit上它表示id未被使用。

不正确,我的课程Item按照我的预期工作,在你的情况下需要更快的帮助SSCCE,否则这里的一切都是向黑暗的镜头,例如,你可以修改并申请ItemRenderer

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

public class SelectedComboBoxID {

    public SelectedComboBoxID() {
        JComboBox comboBox = new JComboBox();
        comboBox.addItem(new Item(1, "-"));
        comboBox.addItem(new Item(2, "Snowboarding"));
        comboBox.addItem(new Item(3, "Rowing"));
        comboBox.addItem(new Item(4, "Knitting"));
        comboBox.addItem(new Item(5, "Speed reading"));
        comboBox.addItem(new Item(6, "Pool"));
        comboBox.addItem(new Item(7, "None of the above"));
        comboBox.setMaximumRowCount(3);
        comboBox.setPrototypeDisplayValue(" None of the above ");
        comboBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox comboBox = (JComboBox) e.getSource();
                Item item = (Item) comboBox.getSelectedItem();
                System.out.println(item.getId() + " : " + item.getDescription());
            }
        });
        //comboBox.setRenderer(new ItemRenderer());
        JFrame frame = new JFrame("MyComboEg");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(comboBox);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

   private class ItemRenderer extends BasicComboBoxRenderer {
        private static final long serialVersionUID = 1L;

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value != null) {
                Item item = (Item) value;
                setText(item.getDescription().toUpperCase());
            }
            if (index == -1) {
                Item item = (Item) value;
                setText("" + item.getId());
            }
            return this;
        }
    }

   private class Item {

        private int id;
        private String description;

        public Item(int id, String description) {
            this.id = id;
            this.description = description;
        }

        public int getId() {
            return id;
        }

        public String getDescription() {
            return description;
        }

        @Override
        public String toString() {
            return description;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                SelectedComboBoxID selectedComboBoxID = new SelectedComboBoxID();
            }
        });
    }
}

答案 1 :(得分:0)

使用对象连接数据库记录。

class Fruit{
    private String id;
    private String name;
    public Fruit(String id,String name){
        this.id=id;
        this.name=name;
    }

    public String toString(){
        return this.name;
    }
}

DefaultComboBoxModel model = new DefaultComboBoxModel();
comboBox = new JComboBox(model);
//
PreparedStatement statement = con.prepareStatement("SELECT _fid, fruit_name FROM fruits;");
ResultSet result = statement.executeQuery();
while (result.next()) {
    model.addElement(new Fruit(result.getString(1),result.getString(2)));
}
close(resultset,statement,con)

答案 2 :(得分:0)

创建Item班级后,您将知道如何在组合框中调用与项目类关联的id

//************************
System.out.print("The ID Associated is :"+((Item)jcbqtype.getSelectedItem()).getId());
//************************