【问题标题】:getSelectedItem JComboBox with GlazedLists AutocompleteSupport Returns Null带有 GlazedLists AutocompleteSupport 的 getSelectedItem JComboBox 返回 Null
【发布时间】:2014-12-22 09:33:04
【问题描述】:

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

返回 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);
}

}

【问题讨论】:

    标签: java autocomplete nullpointerexception jcombobox glazedlists


    【解决方案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);
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 2017-09-18
      • 1970-01-01
      相关资源
      最近更新 更多