JDateChooser : MouseClicked event doesn't get fired

Deadly 提交于 2019-12-24 09:38:51

问题


I want to double click on a JDateChooser to make it enabled. So I use a MouseListener :

jDateChooser1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            System.out.println("mouse clicked");
        }
    });

But this event doesn't get fired, nothing happend.

The date chooser is the com.toedter.calendar one :

Any suggestion ?

Solution

The JDateChooser is a Panel, and I have to listen to a mouse event from on component in the panel. JDateChooser has a getDateEditor(), witch is the textfield.

Here is the solution :

this.jDateChooser1.getDateEditor().getUiComponent().addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            if(evt.getClickCount()==2){               
                Component c = ((Component)evt.getSource()).getParent();
                c.setEnabled(!c.isEnabled());
            }
        }
    });

回答1:


The class JDateChooser extends JPanel. I guess that the area where you are clicking is found inside another Container that is added to the root JPanel. You should try to identify which Container is the one that fires the events and add the listener to it.

to test if this is correct, try to recursively add the listener to all containers and if you see that it gets fired, you can remove the recrusive setting of listeners and try to locate which one of them you need to add the MouseListener to. (Note i write the code directly without testing so please fix any mistake)

private void addMouseListenerRecrusively(Container container){

   for (Component component:container.getComponents()){
     if (component instanceof Container)
        addMouseListenerRecrusively(component); 
   }

   container.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            System.out.println("mouse clicked");
        }
    });

}

and call the method on your chooser

addMouseListenerRecrusively(jDateChooser1);


来源:https://stackoverflow.com/questions/7179100/jdatechooser-mouseclicked-event-doesnt-get-fired

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!