JCalendar Focus Event

喜你入骨 提交于 2019-12-08 05:12:58

问题


I use JCalendar's JDateChooser in a table date column as a tablecelleditor. The problem is that when the column cell clicked JDateChooser appears but if it loses its focus it does not fire focus lost event. How to make it fire focus lost event? And after doing this is there any way to prevent its firing focus lost when JCalendar appeared after clicking the JCalendar Button?

The thing I try to do is if some one specify a date by selecting a date from the calendar stopCellEditing(); Else wait until focus lost event to stop or cancelCellEditing();


回答1:


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




回答2:


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();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/7643893/jcalendar-focus-event

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