本文介绍了JComboBox使用DefaultComboBoxModel修改索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个JComboBox
,并使用DefaultComboBoxModel
从数据库中填充了它的内容.
I have created a JComboBox
and populated its content from database using 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
?
How can I also set the index of the JComboBox
with the value of _fid
?
我对Java& MySQL,现在我没有一个可行的想法.
I'm fairly new to Java & MySQL and right now I don't have a working idea.
推荐答案
不正确,我的班级Item
可以正常工作,在您需要更好帮助的情况下,请尽早发布 SSCCE
,否则这里的所有内容都是黑暗的,例如,您也可以修改并应用ItemRenderer
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,
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();
}
});
}
}
这篇关于JComboBox使用DefaultComboBoxModel修改索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!