How do I update a JComboBox's list from a Document Listener?

I'm writing a custom JComboBox and whenever the user types something I want to update the drop down menu of my JComboBox. The issue I'm having is that once my DocumentListener sees an update I get an error when I try to add an item to the list. Here's a basic example of what isn't working: public class InputField extends JComboBox implements DocumentListener{ //when something is typed, gets suggestions and adds them to the popup @Override public void insertUpdate(DocumentEvent ev) { try{ giveSuggestions(ev); } catch(StringIndexOutOfBoundsException e){ } } private void giveSuggestions(DocumentEvent ev){ this.addItem("ok"); } This isn't actually how my program will work (I'm not just going to add OK each time someone types something), but getting this to work would allow me to implement my custom JComboBox the way it needs to work. Thanks in advance for any help. EDIT: The error message I get is: > Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification
1) To help us best, please post your complete error message. Is it that you're trying to change the Document from inside the DocumentListener? If so, the API will tell you that you can't do this (not directly), and really shouldn't do this. 2) No, no, no to the empty catch block. 3) I also wonder if what you really want to use is a DocumentFilter. 4) To really help us best, create and post an sscce that shows us what you're trying to do.

以上就是How do I update a JComboBox's list from a Document Listener?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准