我在表格日期列中使用JCalendar的JDateChooser作为tablecelleditor。问题是,当列单元格单击JDateChooser时,它会出现,但如果它失去了焦点,它就不会触发焦点丢失事件。如何使它触发焦点丢失事件?这样做之后,有什么方法可以防止在单击JCalendar按钮后出现JCalendar时,它的触发焦点丢失吗?
我尝试做的事情是,如果有人通过从日历中选择日期来指定日期,则等待焦点丢失事件停止或cancelCellEditing();

发布于 2011-10-08 12:42:20
我在JDateChooser中发现了一个在选择日期时触发的propertyChanged事件。和jTable1.putClientProperty("terminateEditOnFocusLost", true);使表格在focusLost上终止编辑
编辑:当日历弹出窗口关闭时,如果您想更改使表单元格失去焦点和terminateEditing的年份:(
jDateChooser.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("date")) {
stopCellEditing();
}
}
});编辑(已解决):在TableCellEditor中添加ta FocusListener到JTable并在失去焦点时取消编辑,而不是使用jTable1.putClientProperty("terminateEditOnFocusLost", true);,而是让用户有机会检查JDateChooser's弹出窗口是否可见。但在此之前,应该使用弹出式isVisible方法扩展JDateChooser。因为popup变量是受保护的。单元格编辑器组件不应该是可聚焦的,否则JTable也会失去焦点
发布于 2011-10-08 12:18:06
在@mKorbel提到的source distribution中,com.toedter.calendar.demo.DemoTable是一个使用com.toedter.calendar.JDateChooserCellEditor作为单元格编辑器的示例。基本的步骤是这些。
JTable table = new JTable(…);
table.setDefaultEditor(Date.class, new JDateChooserCellEditor());附录:这是一个显示预期行为的sscce。
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
复制相似问题