java - JComboBox 使用 DefaultComboBoxModel 修改索引

标签 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);

我怎样才能同时使用 _fid 的值设置 JComboBox 的索引?

我是 Java 和 MySQL 的新手,现在我没有一个可行的想法。

最佳答案

it's still the same and on the class Fruit it says the id is unused.

不对,我的类(class) 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();
            }
        });
    }
}

关于java - JComboBox 使用 DefaultComboBoxModel 修改索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14078840/

相关文章:

java - DWR 异常处理

java - 空指针异常?

php - Mysql显示汉字

java - 从 JFrame 中删除图像

java - JFrame setPreferredSize 导致屏幕尺寸稍大

java - 使用 Java 通过 HTTP 传输数据

java - 确定打开两个文件时找不到哪个文件

Java Tetris - 使用转置来旋转一 block

mysql - 使用(my)sql 中的两个 sql 表实现此目的的查询是什么?

php - 在 Mysql 中运行第二次提交的结果是什么?