mouseclicked事件不起作用

mouseclicked事件不起作用

本文介绍了可编辑的JCombobox mouseclicked事件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在处在一个尴尬的境地,所以我在这里寻求一些专家建议.我有一个Editable JComboBox.我已经实现了actionPerformed事件,它工作正常.但是我已经实现了mouseListener,它无法正常工作.

I am now in an awkward situation and so I am here to get some expert advice. I have an Editable JComboBox. I have implement actionPerformed event which just works fine. But I have implement mouseListener which is not working.

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,但是它也不起作用.

I know that JComboBox is a compound element and hence direct mouse handling event will not work. But I have use the individual component's mouseListener but it is not working too.

有人可以询问stateChanged事件,但是我想在完全选择一个项目后加载一些信息,而不是在我只键入一个项目的2/3个字符时加载一些信息.在stateChanged事件中,如果仅在2/3个字符后才显示项目信息,则该项目会很重,因为我针对每个项目的信息权重很大.

Someone can ask about stateChanged event but I want to load some information after an item is fully selected and NOT when I just type 2/3 characters of an item. In stateChanged event, it's got heavily loaded if it shows the item information only after 2/3 characters since my information against each item is heavy weighted.

因此,当通过鼠标单击选择一个项目时如何获得事件?

推荐答案

以下链接解决了我的问题.

The following link solve my problem.

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);
    }
}

这篇关于可编辑的JCombobox mouseclicked事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:26