繁体   English   中英

在JDatePicker的文本字段中用于检测移动/单击的适当侦听器是什么?

[英]What is the appropriate listener to use for detecting movement/clicking in the text field of JDatePicker?

在我的GUI中,我有几组单选按钮,每个按钮都有两个单选按钮。 第一个单选按钮与文本一样正常,而另一个单选按钮只是一个单选按钮,它旁边有JDatePicker。 我想模仿单选按钮的行为,以便JDatePicker看起来与它耦合,所以当它悬停在JDatePicker的文本字段上时,单选按钮会亮起,当单击单选按钮时会改变到该字段。

GUI截图

我已经创建了一个动作侦听器并将其添加到JDatePickerImpl,并尝试侦听该组件,但没有任何反应 - 当我单击文本字段或按钮时,不会触发侦听器。

UtilDateModel model = new UtilDateModel();
    Properties p = new Properties();
    p.put("text.today", "Today");
    p.put("text.month", "Month");
    p.put("text.year", "Year");
    JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
    // Don't know about the formatter, but there it is...
    fromDatePicker = new JDatePickerImpl(datePanel, new 
DateLabelFormatter());
    fromDatePicker.setBounds(45, 186, 170, 26);
    JFormattedTextField textField = 
fromDatePicker.getJFormattedTextField();
    textField.setFont(new Font("Tahoma", Font.PLAIN, 14));

nowRdBtn = new JRadioButton("Right This Second");
    nowRdBtn.setBounds(22, 235, 139, 29);
    buttonGroup_1.add(nowRdBtn);
    nowRdBtn.setFont(new Font("Tahoma", Font.PLAIN, 14));


TTListener listener = new TTListener();
    fromDatePicker.addActionListener(listener);


private class TTListener implements ActionListener {        
    @Override
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == fromDatePicker) {
            System.out.print("Done");
            beginningRdBtn.setEnabled(false);
            fromDateRdBtn.setEnabled(true);
        }
    }
}

单独地,组件按预期工作,但是期望的耦合行为不是这样。 从JDatePickerImpl获取相应的JFormattedTextField并监听它也不会产生任何结果。

我猜JDatePickerImpl你用的是org.jdatepicker.impl.JDatePickerImpl 只有在用户选择日期时才会触发ActionListener 您应该尝试添加MouseListener并实现mouseEntered()mouseExited()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM