2012-11-21 17 views
3

這是我的第一篇文章,我認爲我做對了。JComboBox與DefaultComboBoxModel不允許添加或刪除

我有一個程序,它接受來自AutoComplete jComboBox的用戶輸入,然後發送輸入以存儲到文本文件中(自動完成使用庫glazedlists_java15/1.8.0完成)。

使用Autocomplet功能後,我必須將jComboBox設置爲DefaultComboBoxModel。

當用戶按下回車鍵,則的JComboBox應該更新與從鍵盤輸入新的項目列表中,這樣用戶可以在JComboBox的列表,請參閱最後鍵入項目

從JComboBox中刪除所有項目和完成然後再插入他們。

的問題是,以前具有自動完成功能,我可以只說jComboBox1.removeAllItems();但現在因爲模型,我必須使用model.removeAllElements();

public class Test { 
    final static DefaultComboBoxModel model = new DefaultComboBoxModel(); 
    static JComboBox c    = new JComboBox(model); 
    private static final long serialVersionUID = 1L; 
    private static JButton b = new JButton(); 
    static JFrame f = new JFrame(); 
    /** 
    * @param args 
    */ 
    public static void TestFrame() { 
     String[] a = {"hi1" , "hi2", "hi3", "hi4","hi5"}; 
     AutoCompleteSupport support = AutoCompleteSupport.install(c, 
       GlazedLists.eventListOf(a)); 
     JPanel test = new JPanel(); 
     test.add(b); 
     test.add(c); 
     model.addElement(a); 
     b.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 

       model.removeAllElements(); 

      } 



     }); 
     f.add(test); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setVisible(true); 
     f.setSize(500,500); 

    } 

的問題是,model.removeAllElements();model.addElement(s);不能正常工作,所以我無法更新jComboBox。你可以請你花些時間幫助我找到解決辦法。謝謝!

+2

1.無想法什麼是目標,那麼也不能用GlazeList做什麼,2.將DocumentListener添加到派生編輯器中,3.是否存在JComboBox#setEditable(true)以及另一個基於==的3-4問題,以便更好地幫助更快地發佈[ SSCCE](HTTP://小號scce.org/) – mKorbel

+0

你試過了repaint(),validate()等? – SomeJavaGuy

+0

交叉發佈[這裏](https://groups.google.com/d/topic/comp.lang.java.programmer/lEqmO359MEk/discussion)。 – trashgod

回答

2

編輯:

我不知道你的問題是,這完全是爲我工作

final DefaultComboBoxModel model = new DefaultComboBoxModel(); 
JComboBox c    = new JComboBox(model); 
private static final long serialVersionUID = 1L; 
private JButton b = new JButton(); 

public TestFrame() { 
    JPanel test = new JPanel(); 
    test.add(b); 
    test.add(c); 
    model.addElement("hi"); 

    b.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      model.removeAllElements(); 

     } 
    }); 
    this.add(test); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setVisible(true); 
    this.setSize(500,500); 

} 

也許你鴕鳥政策達到你的KeyListener

+0

我會嘗試你的例子,看看是否工作,然後我會添加庫,看看這是否正在改變一些東西。感謝您的幫助 – Cristian

+0

我試過你的例子,並且正在工作。再次感謝。我將GlazedList庫添加到您的示例中,並且不再工作。 – Cristian

+0

我使用基於您的示例的新測試代碼編輯了我的問題中的代碼,以便您可以看看是否有時間... – Cristian

0
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

import ca.odell.glazedlists.EventList; 
import ca.odell.glazedlists.GlazedLists; 
import ca.odell.glazedlists.swing.AutoCompleteSupport; 

public class TestFrame 
{ 
    private static JComboBox c = new JComboBox(); 
    private static JButton b = new JButton(); 
    private static JFrame f = new JFrame(); 
    private static String[] a = {"hi1", "hi2", "hi3", "hi4", "hi5"}; 

    public static void TestFrame() 
    { 
     final EventList<String> items = GlazedLists.eventListOf(a); 
     AutoCompleteSupport.install(c, items); 
     JPanel test = new JPanel(); 
     test.add(b); 
     test.add(c); 
     c.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       c = (JComboBox) e.getSource(); 

       if (e.getActionCommand().equals("comboBoxEdited")) 
       { 
        items.clear(); 
       } 
      } 
     }); 
     b.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       items.clear(); 
      } 
     }); 
     f.add(test); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setVisible(true); 
     f.setSize(500, 500); 
    } 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       TestFrame(); 
      } 
     }); 
    } 
}