When using the method below, it returns false, even when the correct value is entered in the JTable cell. What could be the error?

boolean checkTime(JTable table,int indexRow,int indexColOfValue){//возвращает правильность формата необходимого столбца времени TableModel tableModel= table.getModel(); //используем модель таблицы TimeZone tz = TimeZone.getTimeZone("Europe/Moscow"); DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); dateFormat.setLenient(false); try { dateFormat.parse(tableModel.getValueAt(indexRow, indexColOfValue).toString()); } catch (ParseException e) { return false; } return true; } 

The result, after setting the output of the cell value before parse ():

 14:12:40 // прошло 11:15:60 // не прошло по условию 11:15:60 // должно пройти, так как менял значение на верное 11:15:60 // должно пройти, так как менял значение на верное 

Call this method in the list box:

 table.getColumnModel().getColumn(3). setCellEditor(new DefaultCellEditor(txtFld){ @Override public boolean stopCellEditing() { System.out.println("DefaultCellEditor "+ row+" "+col); if(!checkTime(table, col, row)){ return false; } else return true; } }); 
  • Before parsing the date, make System.out.println(tableModel.getValueAt(indexRow, indexColOfValue).toString()) and show what will be output to the console. - post_zeew
  • @post_zeew added, after an erroneous value, does not change. Maybe the problem is the licensee? (also add) - Vladislav Solopov
  • What txtFld is passed to DefaultCellEditor and where is it used internally? - iksuy
  • @iksuy this adapter can only be used by passing in the parameters JTextField, JCombobox or JCheakBox. It passes the value from the cell used. Nowhere has this tapestfield been used since there is no sense in modifying the method, as it is used in other linsectors without tapstfildov - Vladislav Solopov
  • @iksuy but this does not apply to the question, as I understand it, the method uses the same "natural" value (not zeroed). How can a new value be checked? - Vladislav Solopov

1 answer 1

The data enters the model only after stopCellEditing completed, so you need to check the contents of the editor component:

 table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new JTextField()) { @Override public boolean stopCellEditing() { if ( !checkTime( ((JTextField)getComponent()).getText() ) ) { return false; } // родительский метод уведомляет таблицу о завершении редактирования // можно вызвать отдельно fireEditingStopped() return super.stopCellEditing(); } }); 
  • Doesn't true tell the table about the completion of editing? And what does separate use of fireEditingStopped () give? Are there any advantages to calling a parent method? - Vladislav Solopov
  • fireEditingStopped() tells listeners that editing is complete. One of the listeners is JTable itself, which, in response to an event, gets a new value from the editor, places the editor in the model and removes it. Without this (or calling super.stopCellEditing() , which calls fireEditingStopped() ), nothing works for me at all. Moreover, the enter click handling code in DefaultCellEditor.EditorDelegate.actionPerformed ignores the return value altogether. - zRrr