문제

I'm using slightly modified JDateChooserCellEditor class which allows me to put jDateChooser inside my jTable cell. Here is the code of class:

    public class JDateChooserCellEditor extends AbstractCellEditor implements
    TableCellEditor {


private JDateChooser dateChooser = new JDateChooser();

public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) {

    Date date = null;
    if (value instanceof Date) {
        date = (Date) value;
    }
    dateChooser.setDateFormatString("dd-MM-yyyy");
    dateChooser.setDate(date);

    return dateChooser;
}

public Object getCellEditorValue() {

    dateChooser.setDateFormatString("dd-MM-yyyy");
    return dateChooser.getDate();
}

One thing does not work and I cannot find a solution. When I click for the first time on a cell that has jDateChooser inside, select date and hit enter key - nothing happens. The component maintains its focus but never confirms data. But if I after that select different cell the enter key magically works and date is saved to my jTable. After another try it does not work.. Next try - it works. It is so confusing. Thank you all for any help.

도움이 되었습니까?

해결책 2

Well, I have found solution for my problem. I think its not the best way but it works and I have lost too much time trying to fix this. Adding listener to jDateChooser component and notifying to stop editing on property change as user kleopatra stated seems to solve the problem.

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

Thank you all for help.

다른 팁

Your TableCellEditor is incorrect. Assuming that your Table model stores instances of Date, invoke setDefaultEditor() as shown with DemoTableModel, which is found in the class com.toedter.calendar.demo.DemoTable and illustrated here.

table.setDefaultEditor(Date.class, new JDateChooserCellEditor());

Addendum: Don't set the date format in the cell editor; specify it in the JDateChooser constructor or using setDateFormatString().

  1. A variation of your answer worked for me

    private AbstractTableModel model;
    .....
    ... in method getTableCellEditorComponent(JTable table, Object value, boolean isSelected, final int row, final int column) .....
    try{
        model = (AbstractTableModel) table.getModel();
    } catch( Exception e){};
    
    dateChooser.addPropertyChangeListener(new PropertyChangeListener() {
         @Override
         public void propertyChange(PropertyChangeEvent evt) {
             String pname = evt.getPropertyName();
             if ( "date".equals( pname)) {
                try{
                    model.fireTableCellUpdated( row, column); 
                } catch( Exception e){};
             }
         }
    });     
    
  2. Super side effect - if you make your table smaller, with your mouse, and the JDateChooser exits the window table (margins are outside the table) while editing, then everything works perfect - no workaround necessary - It is a Swing bug that the refresh is done only when you have to take care of other windows outside you (probably an auto refresh all is called - that's why this bug was not discovered yet)

******
yourtable.getColumnModel().getColumn(7).setCellEditor(getDateChooserCellEditor());
or
yourtable.setDefaultEditor(java.util.Date.class, getDateChooserCellEditor());
******

public JDateChooserCellEditor getDateChooserCellEditor() {
    JDateChooserCellEditor cellEditor = new JDateChooserCellEditor(){
        @Override
        public Component getTableCellEditorComponent(JTable jtable, Object o, boolean bln, int i, int i1) {
            java.awt.event.ActionListener l = getStopCellEditorActionListener(jtable);
            Component component = super.getTableCellEditorComponent(jtable, o, bln, i, i1);
            JDateChooser dateChooser = (JDateChooser) component;
            JTextField dateEditor = (JTextField) dateChooser.getDateEditor().getUiComponent();
            try {
                dateEditor.removeActionListener(l);
                dateEditor.addActionListener(l);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
            
            return component;
        }
    };
    return cellEditor;
}
private java.awt.event.ActionListener getStopCellEditorActionListener(JTable table) {
    return new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    table.editingStopped(new ChangeEvent(table.getCellEditor()));
                }
            };
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top