0

JList仅在此窗口中不触发任何鼠标事件。当前窗口(图 1)通过单击另一个可以在后台看到的窗口中的“编辑”按钮打开。

在“点名称”列中看到的下拉列表是用aJList添加并使用 显示的。在“点名称”列中输入的文本用作搜索字符串,与键入的文本匹配的项目列表显示在窗口中JPanelJScrollPaneJWindowJList

JList未触发鼠标事件的 窗口:http://i.imgur.com/1n5CTPP.jpg

JList能够在其他窗口中触发鼠标事件。示例窗口如下所示。

JList能够触发鼠标事件的 窗口http://i.imgur.com/05OrjNI.jpg

JList仅当从另一个窗口打开当前窗口时才触发鼠标事件。否则它工作正常。

我已使用该getSource()方法查看触发事件的组件。对于图 2,事件源是JList。在图 1中,JList 没有触发任何鼠标事件

我有一个单独的类来处理整个窗口的鼠标和键事件。当单击下拉菜单或将鼠标移到图 1中的下拉菜单上时,不会调度任何事件。当鼠标单击图 1中的其他位置时,事件处理程序正在接收事件。

请帮我解决这个问题。

我正在扩展 DefaultCellEditor 并创建一个名为 SysCfgComboBoxEditor 的类,以将 Editable 组合框添加到表中。示例代码如下。代码很长,但我已经注释了其中的大部分。

 public class SysCfgComboBoxEditor extends DefaultCellEditor{
    Point MouseLocation1;
    //The two vectors. One to hold all the items and other to hold filtered items
    private Vector<String> vecAllPoints,vecFilteredPoints;
    private int _window_Height;
    private int _window_Width;
    //Model for filtered list of items
    DefaultComboBoxModel moel;
    //Model for original list of items
    DefaultComboBoxModel org;
    //The JList for showing the filtered list of items
    JList popUpItems = null;
    Vector<String> matchingIndicies;
    final JTextField textfield ;
    protected JWindow window = null;
    JScrollPane scrollPane = null;
    private GraphicsConfiguration _gc = null;

    JComboBox comboBox = null;
    //The event handler for handling the events in the dropdown which displays filtered list of items
    private transient PopupWindowHandlers m_popupHandler = null;        

    public SysCfgComboBoxEditor(Vector<String> vecPointNames, JComboBox cBox) {
        super(cBox);
        comboBox = cBox;
        moel=new DefaultComboBoxModel();
        comboBox.setModel(moel);
        comboBox.setUI(new myComboUI());        
        comboBox.getActionMap().put("tab-action", myAction);
        comboBox.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke("TAB"), "tab-action");        
        moel.removeAllElements();
        org = new DefaultComboBoxModel();
        org.removeAllElements();
        Iterator itr = vecPointNames.iterator();
        while(itr.hasNext()){
            Object obj=itr.next();
            moel.addElement(obj);
            org.addElement(obj);
        }

        this.vecAllPoints=vecPointNames;
        ((JComboBox) editorComponent).setEditable(true);
        comboBox.setEditable(true);     
        matchingIndicies=new Vector<String>();
        textfield = (JTextField) ((JComboBox) editorComponent).getEditor().getEditorComponent();

        textfield.addKeyListener(new KeyAdapter() {
            public void keyPressed(final KeyEvent ke) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {

                        if(ke.getKeyCode()==KeyEvent.VK_DOWN || ke.getKeyCode()==KeyEvent.VK_UP ){
                            //display popup items and scroll
                    }

                    if(ke.getKeyCode()==KeyEvent.VK_ENTER){
                        //select popup items
                    }                       

                    if(!((ke.getKeyCode()==KeyEvent.VK_ENTER) ||(ke.getKeyCode()==KeyEvent.VK_TAB) )){
                            //getListModel() method filters the uitems and return a model with filtered list of items
                            DefaultListModel model = getListModel(textfield,false);
                            if(model != null)
                                //show the popup window with filtered list of items
                                showPopupWindow(textfield, true, model);
                        }
                    }
                });
            }
        });   
    }   

    private void showPopupWindow(JTextField textField, boolean showTextInTextField,DefaultListModel model) {

        if(comboBox.isPopupVisible())
               comboBox.setPopupVisible(false);
        if(window!=null){
        //  hidePopupWindow();
        }
        if (popUpItems == null)
        {
            popUpItems = new JList(model);
        }
        else
        {
            popUpItems.setModel(model);
        }
        this.addListeners();
        if (window == null && popUpItems != null && popUpItems.getModel().getSize() > 0)
        {
            //this.initializeGraphicsConfiguration();
            window = new JWindow(this.getComponent().getGraphicsConfiguration());
            //window.setPreferredSize(null);
            JPanel panel = new JPanel(new BorderLayout());
            scrollPane = new JScrollPane(popUpItems);
            //popUpItems.setPreferredSize(null);
            //popUpItems.setUI(new myListUI());
            scrollPane.setPreferredSize(null);
            popUpItems.setBackground(this.getComponent().getBackground());
            popUpItems.setFont(this.getComponent().getFont());
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

            panel.add(scrollPane, BorderLayout.CENTER);
            //panel.add(popUpItems, BorderLayout.CENTER);
            window.getContentPane().add(panel, BorderLayout.CENTER);
            if (_window_Width > 0 && _window_Height > 0)
            {
                window.setSize(_window_Width, _window_Height);
            }
            else
            {
                if(model.getSize()>10){
                    window.setSize(this.getComponent().getWidth(),10*18);
                }
                else{
                    window.setSize(this.getComponent().getWidth(),model.getSize()*18);
                }
            }
            Dimension popupSize = window.getSize();
            window.setLocation(getPopupLocation(popupSize.width, popupSize.height, textField).getLocation());
            if(MouseLocation1 != null){
                if( window.getX() == 0 && window.getY() != 0 )
                {
                    window.setLocation(MouseLocation1);
                }
            }              
            window.setVisible(true);
            textfield.requestFocus(true);
        }
    }

    public void addListeners() {
        if (m_popupHandler == null)
        {
            //THe event handler is initialized
            m_popupHandler = new PopupWindowHandlers(this);
        }
    }     

    //The inner class used as event handler for the dropdown 
    class PopupWindowHandlers extends FocusAdapter implements ChangeListener, AWTEventListener,
                        ComponentListener, WindowListener,KeyListener {

        private Window _parentWindow;
        private SysCfgComboBoxEditor _comboBoxEditor;
        private boolean _isComponentResized = false;
        boolean clicked = false;


        public PopupWindowHandlers(SysCfgComboBoxEditor comboBox){
            MenuSelectionManager msm = MenuSelectionManager.defaultManager();
            msm.addChangeListener(this);
            this._comboBoxEditor = comboBox;    
            addWindowListeners();
        }

        @SuppressWarnings({ "rawtypes", "unchecked" })
        protected void addWindowListeners() {
            java.security.AccessController.doPrivileged(new java.security.PrivilegedAction(){
                public Object run()
                {
                    Toolkit.getDefaultToolkit()
                        .addAWTEventListener(PopupWindowHandlers.this,
                                AWTEvent.MOUSE_EVENT_MASK |
                                AWTEvent.MOUSE_MOTION_EVENT_MASK |
                                AWTEvent.MOUSE_WHEEL_EVENT_MASK | AWTEvent.KEY_EVENT_MASK | AWTEvent.ACTION_EVENT_MASK);
                    return null;
               }
            });

            Component invoker = _comboBoxEditor.getComponent();             
            _parentWindow = invoker instanceof Window ? (Window) invoker :
                    SwingUtilities.getWindowAncestor(invoker);
            if (_parentWindow != null)
            {
                _parentWindow.addKeyListener(this);
                _parentWindow.addComponentListener(this);
                _parentWindow.addWindowListener(this);
                _parentWindow.addFocusListener(this);
            }
        }

        private void hidePopupWindow()
        {
            //hide the popup
        }   

         @SuppressWarnings("unchecked")
         protected void removeWindowListeners()
         {
            //remove the listeners
         }                     


         public void eventDispatched(AWTEvent ev)
         { 
             //This event listener is not getting the mouse events from the JList.
             if (ev instanceof MouseEvent && (SwingUtilities.isRightMouseButton( (MouseEvent) ev)
                           || SwingUtilities.isMiddleMouseButton( (MouseEvent) ev)))
             {
                 return;
             }
             if(_comboBoxEditor.window!=null && editorComponent.isShowing()){   
                 MouseLocation1=_comboBoxEditor.window.getLocation();      
             }
             switch (ev.getID())
             {
                case MouseEvent.MOUSE_PRESSED:
                    // do event
                break;

                case MouseEvent.MOUSE_CLICKED:
                    //do event
                break;

               case MouseEvent.MOUSE_MOVED:
                   //do event

               case MouseEvent.MOUSE_DRAGGED:
                  //do event

               case MouseEvent.MOUSE_RELEASED:
                  //do event

               case KeyEvent.KEY_PRESSED:
                   //do event
                   }
                }
            }

         private void mouseClicked(MouseEvent e)
         {
          //select the item
         }


         private void mouseEnteredOrMoved(MouseEvent e)
         {
         //scroll over the items 
         }


    }
4

0 回答 0