我现在处于尴尬境地,所以我来这里得到一些专家意见。我有Editable JComboBox
。我已经实现了actionPerformed
事件,它可以正常工作。但是我实现了mouseListener
无效。
component = jcbItemType.getEditor().getEditorComponent();
component.addMouseListener(new java.awt.event.MouseListener() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jcbItemTypeMouseClicked(evt);
}
public void mousePressed(java.awt.event.MouseEvent evt) {
jcbItemTypeMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
jcbItemTypeMouseReleased(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jcbItemTypeMouseReleased(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
jcbItemTypeMouseReleased(evt);
}
});
我也尝试过以下代码:
Component[] comps = jcbItemType.getComponents();
for(int i = 0; i < comps.length; i++)
{
comps[i].addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jcbItemTypeMouseClicked(evt);
}
public void mousePressed(java.awt.event.MouseEvent evt) {
jcbItemTypeMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
jcbItemTypeMouseReleased(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jcbItemTypeMouseReleased(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
jcbItemTypeMouseReleased(evt);
}
});
}
我知道JComboBox
是一个复合元素,因此直接鼠标处理事件将不起作用。但是我使用了单个组件的mouseListener
,但它也没有用。
有人可以询问stateChanged
事件,但我想在完全选中项目后加载一些信息,而不是在我只输入项目的2/3字符时。在stateChanged
事件中,如果仅在2/3个字符后显示项目信息,则会加载很多,因为我对每个项目的信息都很重。
那么,如何通过鼠标点击选择项目来获取事件?
答案 0 :(得分:2)
以下链接解决了我的问题。
http://engin-tekin.blogspot.com/2009/10/hrefhttpkfd.html
该链接的代码段如下:
import java.lang.reflect.InvocationTargetException;
import java.awt.event.MouseAdapter;
import java.lang.reflect.Field;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.BasicComboBoxUI;
import java.awt.event.MouseEvent;
/**
* User: destiny
* Date: Oct 19, 2009
* Time: 9:18:34 PM
*/
public class JComboPopupMouseListenerDemo {
private static void addPopupMouseListener(JComboBox box) {
try {
Field popupInBasicComboBoxUI = BasicComboBoxUI.class.getDeclaredField("popup");
popupInBasicComboBoxUI.setAccessible(true);
BasicComboPopup popup = (BasicComboPopup) popupInBasicComboBoxUI.get(box.getUI());
Field scrollerInBasicComboPopup = BasicComboPopup.class.getDeclaredField("scroller");
scrollerInBasicComboPopup.setAccessible(true);
JScrollPane scroller = (JScrollPane) scrollerInBasicComboPopup.get(popup);
scroller.getViewport().getView().addMouseListener(this);
// ((JViewport) ((JScrollPane) ((BasicComboPopup) popupInBasicComboBoxUI.get(box.getUI())).getComponents()[0]).getComponents()[0]).getComponents()[0].addMouseListener(this);
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private static MouseAdapter listener() {
return new MouseAdapter(){
public void mouseClicked(MouseEvent mouseEvent) {
System.out.println("mouseClicked");
}
public void mousePressed(MouseEvent mouseEvent) {
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent mouseEvent) {
System.out.println("mouseReleased");
}
public void mouseEntered(MouseEvent mouseEvent) {
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent mouseEvent) {
System.out.println("mouseExited");
}
};
}
public static void main(String[] args) throws InvocationTargetException, InterruptedException {
JComboBox box = new JComboBox(new String[]{"val 1","val 2","val ......","val 4","val 5","val 6"});
box.setEditable(true);
addPopupMouseListener(box);
JFrame frame = new JFrame();
frame.add(box);
frame.pack();
frame.setVisible(true);
}
}