2014-10-17 74 views
1

我在JComboBox上使用SwingX AutoCompleteDecorator。一切正常,但我希望我的用户可以更改我的对象的名称,名称也显示在组合框中。问题是,我可以刷新我的组合框,但如图片所示,从自动完成装饰所显示的字符串是相同的: Problem使用SwingX AutoCompleteDecorator刷新JComboBox

刷新组合框的代码如下所示:

try { 
    Aannemer a = getNewAannemer(); 
    MainController.getInstance().updateAannemer(a); 
    aannemerBox.revalidate(); 
    aannemerBox.repaint(); 
} catch (Exception ex) { 
    //... 
} 

的当我从组合框中重新选择对象时字符串更新。 我也尝试使用组合框的个性化渲染器和编辑器。

任何想法如何我也可以刷新组合框中显示的字符串?

回答

0

随着当前的代码,很难说出了什么问题。如预期

  • 我可以输入新项目
  • 当使用Add按钮,我可以添加新的项目下面的代码工作得很好,我

    import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator; 
    
    import javax.swing.DefaultComboBoxModel; 
    import javax.swing.JButton; 
    import javax.swing.JComboBox; 
    import javax.swing.JFrame; 
    import javax.swing.WindowConstants; 
    import java.awt.BorderLayout; 
    import java.awt.EventQueue; 
    
    public class AutoCompleteCombobox { 
    
        public static void main(String[] args) { 
        EventQueue.invokeLater(() -> { 
         JFrame frame = new JFrame("TestFrame"); 
    
         JComboBox<String> comboBox = new JComboBox<>(); 
         DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(); 
         model.addElement("First"); 
         model.addElement("Second"); 
         comboBox.setModel(model); 
         comboBox.setEditable(true); 
    
         AutoCompleteDecorator.decorate(comboBox); 
    
         frame.getContentPane().add(comboBox); 
    
         JButton button = new JButton("Add item"); 
         button.addActionListener(e -> { 
         String selectedItem = (String) comboBox.getSelectedItem(); 
         if (comboBox.getSelectedIndex() == -1){ 
          model.addElement(selectedItem); 
         } 
         }); 
         frame.getContentPane().add(button, BorderLayout.SOUTH); 
    
         frame.pack(); 
         frame.setVisible(true); 
         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
        }); 
        } 
    } 
    
    • 自动完成的作品,并自动完成行为很好

    总之,我不能重现你的问题。请在您的问题中发布一段代码,以便我们重现问题。