Question

I would need to have a MouseListener active on a SwingX JXDatePicker component, enabling me to perform specific actions when the user clicks on the component. Unfortunately the event is never triggered.

Hereby a small piece of code that reproduces the problem:

public class TestDummy4 extends JFrame implements MouseListener{

    private static final long serialVersionUID = -2424758762078571430L;

    public TestDummy4(){
        super();
        this.getContentPane().setLayout(new BorderLayout());

        //Adds date picker
        JXDatePicker dp = new JXDatePicker();
        dp.getEditor().setEditable(false);
        dp.getEditor().setHighlighter(null);
        dp.addMouseListener(this);

        this.getContentPane().add(dp);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args) throws IOException {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Builds GUI
                new TestDummy4();
            }
        });

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("Mouse clicked");
    }

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("Mouse pressed");
    }
    @Override
    public void mouseReleased(MouseEvent e) {}
    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("Mouse Entered");
    }
    @Override
    public void mouseExited(MouseEvent e) {}
}

With this code, I have not a single line of output on the console when clicking on the JXDatePicker.

Any help / hint would be greatly appreciated! Thomas

Was it helpful?

Solution

To add a MouseListener to the JXDatePicker's editor component use:

dp.getEditor().addMouseListener(this);

Update: To add a ActionListener to the component's open JButton you can use:

JButton openButton = (JButton) dp.getComponent(1); 
openButton.addActionListener(myActionListener);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top