0

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

Tom
  • 1,375
  • 3
  • 24
  • 45
  • same issue with all compound components (f.i. with core combo, see the tutorial): you can't add a low-level listeners to the parent and expect that it'll get notified on events to the children – kleopatra Apr 21 '13 at 21:55
  • *"Java Swing mouse listener.."* Minor point, but `MouseListener` (like most listeners) is pure AWT. – Andrew Thompson Apr 22 '13 at 01:09

1 Answers1

3

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);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thanks a lot for the swift reply: this works indeed when clicking on the editor but unfortunately it does not catch mouse clicks on the drop-down arrow next to it (also part of the JXDatePicker component)... any suggestions? Many thanks – Tom Apr 21 '13 at 20:25
  • Thx but unfortunately this doesn't work. To be more specific: I'm looking to add a listener on the component with the arrow that opens the month view (not the month view itself). Any other suggestions? Many thanks – Tom Apr 21 '13 at 20:47
  • beware: the button is an implementation detail that might change (no immediate plans, but .. it could) @Tom: the eternal question - why or what exactly do you want to achieve? – kleopatra Apr 21 '13 at 21:52
  • _have to resort_ that's the question - most often the requirement can be met somehow else :-) – kleopatra Apr 21 '13 at 22:07