开发者

JDateChooser : MouseClicked event doesn't get fired

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开发者_JAVA百科, 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());
            }
        }
    });


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜