java JComboBox 使用 DefaultComboBoxModel 修改索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14078840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JComboBox modify index using DefaultComboBoxModel
提问by alisongaleon
I have created a JComboBox
and populated its content from database using DefaultComboBoxModel
.
我创建了一个JComboBox
并使用DefaultComboBoxModel
.
Here is the code:
这是代码:
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);
How can I also set the index of the JComboBox
with the value of _fid
?
我怎样才能JComboBox
用 的值设置 的索引_fid
?
I'm fairly new to Java & MySQL and right now I don't have a working idea.
我对 Java 和 MySQL 还很陌生,现在我没有一个可行的想法。
回答by mKorbel
it's still the same and on the class Fruit it says the id is unused.
它仍然相同,并且在 Fruit 类上它说该 ID 未使用。
not true, my class Item
works as I expected, in your case required for better help sooner post an SSCCE
, otherwise everything here are shots to the dark, for example, you can to modify and apply ItemRenderer
too,
不是真的,我的课程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();
}
});
}
}
回答by imxylz
Use a object to connect with your database record.
使用对象与您的数据库记录连接。
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)
回答by Michael Salazar
Once you have created the Item
Class, you will know how to call the id
associated to the item class in the combo box:
创建Item
类后,您将知道如何id
在组合框中调用关联到项目类:
//************************
System.out.print("The ID Associated is :"+((Item)jcbqtype.getSelectedItem()).getId());
//************************