带有GlazedLists的getSelectedItem JComboBox AutocompleteSupport返回Null

时间:2014-12-22 09:33:05

标签: java autocomplete nullpointerexception jcombobox glazedlists

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

返回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

从数据库加载到arraylists中的数据
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);
}

}

1 个答案:

答案 0 :(得分:1)

您在这里遇到的根本困难是,您在尝试利用GlazedLists软件包时没有正确使用它的核心实用程序:EventLists。

如果使用EventLists而不是ArrayLists,则可以轻松地解决困难。

如果你真的想要,你可以保持你的FillCombo方法返回一个ArrayList(也许更好的名称为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);
...