gpt4 book ai didi

java - 具有 GlazedLists AutocompleteSupport 的 getSelectedItem JComboBox 返回 Null

转载 作者:行者123 更新时间:2023-12-01 12:05:56 24 4
gpt4 key购买 nike

我对编程有点陌生,很抱歉如果有一些事情可以做得更好我的组合框已成功填充字符串数组,并且自动完成功能正常。我只是无法获取组合框中的文本。

返回java.lang.NullPointerException

private ArrayList<String> arrRekening;
private ArrayList<String> arrEienaar;
private String[] sarrRekening;
private String[] sarrEienaar;

public NewConnectionPoint() {

arrAccount = new ArrayList<String>();
arrOwner = new ArrayList<String>();

FillCombo(arrAccount , "Owners", "OwnerName");
FillCombo(arrOwner , "Accounts", "AccountName");
sarrOwner= arrOwner.toArray(new String[arrOwner .size()]);
sarrAccount= arrAccount.toArray(new String[arrAccount.size()]);

JComboBox<String> comboAccount = new JComboBox<String>();
AutoCompleteSupport<String> supAccount = AutoCompleteSupport.install(comboRekening, GlazedLists.eventList(Arrays.asList(sarrAccount)));
supAccount.setStrict(true);

JComboBox<String> comboOwner = new JComboBox<String>();
AutoCompleteSupport<String> supOwner = AutoCompleteSupport.install(comboOwner,GlazedLists.eventList(Arrays.asList(sarrOwner)));
supOwner.setStrict(true);

JButton btnShow = new JButton("ShowSelectedr");
btnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//Error occurs at this line
JOptionPane.showMessageDialog(null, comboOwner.getSelectedItem().toString());

});}

}

//使用sql从数据库中将数据加载到数组列表中

private void FillCombo(ArrayList<String> ComboElements, String sTable, String sColumn){
try{
Data.changeQuery(sTable);// database connection fine returns and fills combobox

while(MyData.rs.next()){
String sReturn= MyData.rs.getString(sColumn);
ComboElements.add(sReturn);
}

}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}

}

最佳答案

您在这里遇到的根本困难是您试图利用 GlazedLists 包,但没有正确采用它的核心实用程序:EventLists。

如果您使用 EventList 而不是 ArrayList,您可以轻松地回避困难。

如果您确实愿意,可以保留返回 ArrayList 的 FillCombo 方法(也许更好的名称为 getElements()),但立即启动 EventList,使用GlazedLists EventComboBoxModel 将 EventList 链接到 JComboBox,然后您会发现您的组合框 getSelectedItem() 应该可以正常工作。

将列表连接到具有自动完成支持的组合框的修改后的代码将如下所示:

...
FillCombo(arrOwner , "Owners", "OwnerName");
EventList<String> ownerEventList = GlazedLists.eventList(arrOwner);
EventComboBoxModel<String> ownerModel = new EventComboBoxModel<String>(ownerEventList);
JComboBox comboOwner = new JComboBox(ownerModel);
AutoCompleteSupport<String> supOwner = AutoCompleteSupport.install(comboOwner,ownerEventList);
supOwner.setStrict(true);
...

关于java - 具有 GlazedLists AutocompleteSupport 的 getSelectedItem JComboBox 返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27599914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com