[英]Netbeans JComboBox multiple columns
有沒有一種方法可以將兩列添加到Netbeans的JComboBox中?
我要執行以下操作:在cboBox中,我想用其ISO代碼填充國家列表,例如:
+----------------+---+
| Afghanistan | V | < The Combo Box :D
+----------------+---+
↓↓↓
+----+---------------+
|ISO | Country |
+----+---------------+
| AF | Afghanistan |
| AX | Åland Islands |
| AL | Albania |
|... | ... |
+----+---------------+
然后,當用戶選擇一個國家/地區時,我需要提取ISO代碼(col。0)以存儲在配置文件中。 此后應再次讀取,並顯示為國家/地區名稱,而不是ISO代碼。
我一直在尋找解決方案,但是我所能找到的就是如何將cboBox放入JTable中。
(這是我使用/修改過的列表: http : //www.textfixer.com/resources/dropdowns/country-list-iso-codes.txt )
謝謝!
您應該做的是將數據存儲在Country
對象中,其中包含name
和iso
字段。 我真的看不到在組合框中顯示iso的意義。 從您的圖形來看,您似乎不希望它在初始顯示中顯示,那么為什么要在下拉菜單中顯示呢?
對於顯示,您可以使用DefaultListCellRenderer
並從每個Country
DefaultListCellRenderer
提取名稱值。 當您從組合框中選擇一個國家時,它已經包含了一個“ Country
對象List
,因此您可以從選定的Country
中提取iso
。
請參閱此處的示例。 注意:該示例僅顯示國家/地區名稱,但是如果您還確實想要iso,則只需將呈現方式更改為setText(country.getIso() + " | " + country.getName());
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.SwingUtilities;
public class ComboBoxDemo {
private List<Country> countries;
private JComboBox cBox;
public ComboBoxDemo() {
countries = createCountryList();
cBox = createComboBox(countries);
JFrame frame = new JFrame();
frame.add(cBox);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JComboBox createComboBox(List<Country> countries) {
final JComboBox comboBox = new JComboBox(countries.toArray());
comboBox.setRenderer(new ComboBoxRenderer());
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
Country country = (Country) comboBox.getSelectedItem();
System.out.println(country.getIso());
}
}
});
return comboBox;
}
private class ComboBoxRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list,
value, index, isSelected, cellHasFocus);
Country country = (Country) value;
label.setText(country.getName());
return label;
}
}
private List<Country> createCountryList() {
List<Country> list = new ArrayList<>();
list.add(new Country("Afghanistan", "AF"));
list.add(new Country("Åland Islands", "AX"));
list.add(new Country("Albania", "AL"));
return list;
}
public class Country {
private String name;
private String iso;
public Country(String name, String iso) {
this.name = name;
this.iso = iso;
}
public String getName() {
return name;
}
public String getIso() {
return iso;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ComboBoxDemo();
}
});
}
}
使用自定義渲染器只是答案的一半。 您還需要使用自定義的KeySelectionManager
以便在使用鍵盤時不會破壞組合框的默認選擇功能。
有關將渲染器和KeySelectionManager組合到一個類中的簡單解決方案,請參見帶有自定義渲染器的組合框 。 或者,您可以在博客中查看Combo Box With Hidden Data
的Combo Box With Hidden Data
鏈接,以獲取有關Timmos在Peeskillet的答案中所提建議的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.