我被困在添加一個按鈕到JComboBox
編輯器,我成功添加了一個按鈕,但我有一些問題,如當我第一次進入編輯器時,一個動作執行事件被觸發,這是不可接受的,另一個是我無法輸入文字。自定義JComboBox編輯器
結果:
問題:
@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.
*/
}
}
我還希望文字的字體和大小設置爲相同的設置組合框。
您是否嘗試將按鈕和textField添加到面板?另一個問題是textField默認不會獲得焦點。我也關注關鍵聽衆,但是我沒有任何關於你想要實現的內容來真正能夠說更多。你也可以看看最新的SwingX中的BuddySupport,這將允許你在文本組件 – MadProgrammer
中「夥伴」一個其他組件,它們確實添加了編輯器有一個內部類textPanel(一個按鈕和一個jtextfield)。我試圖把按鈕只是爲了清除編輯器中鍵入的所有文本。如果您鍵入文本查詢是製作的 – GoAntonio
其實,已經通讀了問題,您認爲在窗格上的字段不是 – MadProgrammer