Question

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());
            }
        }
    });
Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top