2013-07-26 777 views
1

我被困在添加一個按鈕到JComboBox編輯器,我成功添加了一個按鈕,但我有一些問題,如當我第一次進入編輯器時,一個動作執行事件被觸發,這是不可接受的,另一個是我無法輸入文字。自定義JComboBox編輯器

結果: result

問題:

problems

@Override 
     public Component getEditorComponent() { 
      return panel; 
     } 

這是問題,如果我回到panel.jtexfield我只能得到一個文本字段沒有一個按鈕,所以這裏有什麼訣竅?

這裏是我的代碼

import Store.util.DatabaseHelper; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.ArrayList; 
import javax.swing.*; 
import javax.swing.border.Border; 
import javax.swing.plaf.basic.BasicComboBoxEditor; 
import org.hibernate.HibernateException; 
import org.netbeans.lib.awtextra.AbsoluteLayout; 

public class NewComboTest extends JFrame { 

    private ArrayList<Object> shopCart = new ArrayList<Object>(); 
    private JComboBox cb; 
    private static final Object[] comboContents = { 
     "First", "Second", "Third", "Fourth", "Fifth" 
    }; 

    public NewComboTest() { 

     super("New Combo Test"); 
     setLayout(null); 
     cb = new JComboBox(); 
     cb.setRenderer(new NewComboRenderer()); 

     cb.setEditor(new NewComboEditor()); 
     cb.setEditable(true); 

     cb.setSize(new Dimension(350, 100)); 
     for (int i = 0; i < comboContents.length; i++) { 
      cb.addItem(comboContents[ i]); 
     } 
     cb.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("_____________" + cb.getSelectedItem()); 

       shopCart.add(cb.getSelectedItem()); 
       System.out.println("items added" + shopCart); 
      } 
     }); 
     cb.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() { 
      @Override 
      public void keyReleased(KeyEvent e) { 
       System.out.println("KeyReleased" + cb.getEditor().getItem().toString()); 
       populateModel(cb.getEditor().getItem().toString()); 
      } 
     }); 

     getContentPane().add(cb, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 200, 480, 50)); 
     setSize(1200, 450); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    public static void main(String[] arg) { 
     new NewComboTest(); 
    } 

    private class NewComboEditor extends JPanel implements ComboBoxEditor { 

     JTextField tf; 
     JButton eraseButton; 
     textPanel panel = new textPanel(); 

     public NewComboEditor() { 

     } 

     @Override 
     public void addActionListener(ActionListener l) { 
      tf.addActionListener(l); 
     } 

     @Override 
     public Component getEditorComponent() { 
      return panel; 
     } 
     public Component getEditorComponent2() { 
      return panel; 
     } 

     @Override 
     public Object getItem() { 
      return tf.getText(); 

     } 

     @Override 
     public void removeActionListener(ActionListener l) { 
      tf.removeActionListener(l); 
     } 

     @Override 
     public void selectAll() { 
      tf.selectAll(); 
     } 

     @Override 
     public void setItem(Object o) { 
      if (o != null) { 
       tf.setText(tf.getText()); 
      } else { 
       tf.setText(""); 
      } 
     } 

     private class textPanel extends JPanel { 

      JTextField jTextField1 = new JTextField(); 
      JButton jButton1 = new JButton(); 

      public textPanel() { 
       setLayout(new BorderLayout()); 

       jButton1.setBackground(new java.awt.Color(255, 255, 255)); 
       jButton1.setForeground(new java.awt.Color(0, 51, 51)); 
       jButton1.setText("X"); 
       jButton1.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         jTextField1.setText(""); 
        } 
       }); 

       add(jTextField1, BorderLayout.CENTER); 
       add(jButton1, BorderLayout.EAST); 





      } 

      public String getText(){ 
       return jTextField1.getText(); 
      } 
     } 
    } 

    private class NewComboRenderer extends JLabel implements ListCellRenderer { 

     public NewComboRenderer() { 
      setOpaque(true); 
     } 

     public Component getListCellRendererComponent( 
       JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
      setText(value.toString()); 

      setBackground(isSelected ? Color.BLUE : Color.white); 
      setForeground(isSelected ? Color.white : Color.red); 
      return this; 
     } 
    } 

    /* public void populateModel(String text) throws HibernateException { 
     java.util.List l = DatabaseHelper.GetProductsBy(text); 

     for (Object object : l) { 
      cb.addItem(object); 
     } 
ignore this its unnecessary. 
*/ 

    } 
} 

我還希望文字的字體和大小設置爲相同的設置組合框。

+0

您是否嘗試將按鈕和textField添加到面板?另一個問題是textField默認不會獲得焦點。我也關注關鍵聽衆,但是我沒有任何關於你想要實現的內容來真正能夠說更多。你也可以看看最新的SwingX中的BuddySupport,這將允許你在文本組件 – MadProgrammer

+0

中「夥伴」一個其他組件,它們確實添加了編輯器有一個內部類textPanel(一個按鈕和一個jtextfield)。我試圖把按鈕只是爲了清除編輯器中鍵入的所有文本。如果您鍵入文本查詢是製作的 – GoAntonio

+0

其實,已經通讀了問題,您認爲在窗格上的字段不是 – MadProgrammer

回答

6

第一組的問題,我可以看到的是,您可以定義一個NewComboEditorJTextFieldJButton,還定義了一個textPanel,它包含了所有這些事情的任何方式。但是不要使用textPane上的組件,而是使用新創建的組件(在NewComboEditor中),而不是......實際上,我甚至不確定這是如何工作的,因爲您從不初始化這些組件(在NewComboEditor中) ,所以應該有一個NullPointerException ...

如果這還不夠的問題,在JTextFieldJButton不加入任何東西反正...

,而不是...

  • NewComboEditor應該不需要分機結束任何事情(或者如果你真的想要,它可以從textPane延伸)。
  • 到現場的所有引用應到文本字段進行的textPane

舉個例子...

enter image description here

import java.awt.Component; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionListener; 
import javax.swing.ComboBoxEditor; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class CustomComboBoxEditor { 

    public static void main(String[] args) { 
     new CustomComboBoxEditor(); 
    } 

    public CustomComboBoxEditor() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JComboBox cb = new JComboBox(); 
       cb.addItem("Apple"); 
       cb.addItem("Banana"); 
       cb.addItem("Orange"); 
       cb.setEditable(true); 
       cb.setEditor(new MyComboBoxEditor()); 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new GridBagLayout()); 
       frame.add(cb); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class MyComboBoxEditor implements ComboBoxEditor { 

     private EditorPane editorPane; 

     public MyComboBoxEditor() { 
      editorPane = new EditorPane(); 
     } 

     @Override 
     public Component getEditorComponent() { 
      return editorPane; 
     } 

     @Override 
     public void setItem(Object anObject) { 
      editorPane.setText(anObject == null ? null : anObject.toString()); 
     } 

     @Override 
     public Object getItem() { 
      return editorPane.getText(); 
     } 

     @Override 
     public void selectAll() { 
      editorPane.selectAll(); 
     } 

     @Override 
     public void addActionListener(ActionListener l) { 
      editorPane.addActionListener(l); 
     } 

     @Override 
     public void removeActionListener(ActionListener l) { 
      editorPane.removeActionListener(l); 
     } 

    } 

    public class EditorPane extends JPanel { 

     private JTextField field; 
     private JButton button; 

     public EditorPane() { 
      field = new JTextField(10); 
      button = new JButton("X"); 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.weightx = 1; 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.gridx = 0; 
      add(field, gbc); 
      gbc.weightx = 0; 
      gbc.fill = GridBagConstraints.NONE; 
      gbc.gridx++; 
      add(button, gbc); 
     } 

     @Override 
     public void addNotify() { 
      super.addNotify(); 
      field.requestFocusInWindow(); 
     } 

     public void selectAll() { 
      field.selectAll(); 
     } 

     public void setText(String text) { 
      field.setText(text); 
     } 

     public String getText() { 
      return field.getText(); 
     } 

     public void addActionListener(ActionListener listener) { 
      field.addActionListener(listener); 
     } 

     public void removeActionListener(ActionListener listener) { 
      field.removeActionListener(listener); 
     } 

    } 

} 

現在,如果你想設置字段屬性與組合框相同,我只需將組合框的引用傳遞給編輯器並允許它提取所需的屬性(即字體,顏色等)

+0

謝謝你隊友=)它很好地工作。我只是想知道你是怎麼想出來的(addNotify方法),我應該閱讀一本特別的搖擺書,考慮到我是新來的java和自定義swing類。 – GoAntonio

+0

這是我前段時間推薦給我的東西。我只是推薦閱讀JavaDocs – MadProgrammer