需要做一个可编辑的JComboBox,即可以在里面输入查找,也可以选择下拉框选择。但两者都必须在操作完后,comboBox的输入框都必须高亮显示,如图:
但是把鼠标点击输入框或者在里面输入数字按回车进行匹配时,都可是得输入框高亮显示,但是按那个下拉框按钮进行选择时却不能使其高亮显示,不知道怎么会事,希望高手指点。
以下为测试代码:
package kissJava.JComboBox;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.ComboBoxEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class EditComboBox{
String labels[] = { "Chardonnay", "Sauvignon", "Riesling", "Cabernet",
"Zinfandel", "Merlot", "Pinot Noir", "Sauvignon Blanc",
"Syrah", "Gewurztraminer" };
String code[] = { "1", "2", "3", "4","5", "6", "7", "8", "9", "10" };
JComboBox comboBox =null;
final KJComboBoxEditor kjce = new KJComboBoxEditor();
public EditComboBox(){
JFrame frame = new JFrame("Editable JComboBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
comboBox = new JComboBox();
comboBox.setMaximumRowCount(5);
comboBox.setEditable(true);
initCombox();
comboBox.setEditor(kjce);
contentPane.add(comboBox, BorderLayout.NORTH);
final JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(scrollPane, BorderLayout.CENTER);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
textArea.append("Selected: " + comboBox.getSelectedItem());
textArea.append(", Position: " + comboBox.getSelectedIndex());
textArea.append(System.getProperty("line.separator"));
comboBox.requestFocus();
kjce.selectAll();
}
};
comboBox.addActionListener(actionListener);
frame.setSize(300, 200);
frame.setVisible(true);
}
private void initCombox(){
final Vector<String> v = new Vector<String>(code.length);
String[] str = new String[code.length];
for(int i = 0,length = code.length; i<length;i++) {
str[i] = code[i] + " - " + labels[i];
v.add(str[i]);
}
comboBox.setModel(new DefaultComboBoxModel(v));
}
public static void main(String args[]) {
new EditComboBox();
}
class KJComboBoxEditor implements ComboBoxEditor {
JTextField jtf = new JTextField();
public KJComboBoxEditor(){
jtf.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e) {
boolean isExit = false;
String codeStr = jtf.getText().trim();
for(int i = 0,length = code.length; i<length;i++) {
if(codeStr.equals(code[i])){
//jtf.setText(code[i] + " - " + labels[i]);
setItem(code[i] + " - " + labels[i]);
comboBox.setSelectedIndex(i);
isExit = true;
}
}
if(!isExit){
jtf.setText("");
}
selectAll();
}
});
jtf.addMouseListener(new java.awt.event.MouseListener(){
public void mouseClicked(MouseEvent e) {
selectAll();
comboBox.setPopupVisible(false);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
selectAll();
comboBox.setPopupVisible(false);
}
public void mouseReleased(MouseEvent e) {
selectAll();
}
});
}
public void addActionListener(ActionListener l) {
}
public JTextField getEditorComponent() {
return jtf;
}
public Object getItem() {
return jtf.getText();
}
public void removeActionListener(ActionListener l) {
}
public void selectAll() {
jtf.selectAll();
jtf.requestFocus();
}
public void setItem(Object anObject) {
jtf.setText(anObject.toString());
}
}
}