JCalendar Focus Event

邮差的信 提交于 2019-12-08 17:27:37
MOD

I found a propertyChanged event in JDateChooser which is fired when a date selected. And jTable1.putClientProperty("terminateEditOnFocusLost", true); make the table terminate edit on focusLost

Edit : When calendar popup is down if you want to change the year that make table cell lose focus and terminateEditing :(

jDateChooser.addPropertyChangeListener(new PropertyChangeListener() {

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("date")) {
            stopCellEditing();
        }
    }
});

Edit(Solved) : Instead of using jTable1.putClientProperty("terminateEditOnFocusLost", true); adding ta FocusListener to JTable in TableCellEditor and cancel editing when focus lost give a chance to check if the JDateChooser's popup is visible or not. But before this, JDateChooser should be extended with a popup isVisible method. Because popup variable is protected. And cell editor components should not be focusable else JTable also loses its focus

In the source distribution mentioned by @mKorbel, com.toedter.calendar.demo.DemoTable is an example that uses com.toedter.calendar.JDateChooserCellEditor as a cell editor. The essential steps are these.

JTable table = new JTable(…);
table.setDefaultEditor(Date.class, new JDateChooserCellEditor());

Addendum: Here is an sscce that shows the expected behavior.

import com.toedter.calendar.demo.DemoTable;
import java.awt.EventQueue;
import javax.swing.JFrame;

/** @see http://stackoverflow.com/questions/7643893 */
public class CalendarTable {

    private void display() {
        JFrame f = new JFrame("CalendarTable");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new DemoTable());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new CalendarTable().display();
            }
        });
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!