Focus to next cell in Jtable after inputting date

I have a custom dateField that I am using in my application and have inserted it into my table.

What happens is when I click button which is part of custom dateField, it pops up a calendar.

After this I want the focus to go to the next cell in the table.

So can anyone tell me what is the best way to get focus onto the next cell after choosing the date in the popup window?

The code I am using is below.

publicclass ChequeDetailsTableextends JTable{

protected EtchedBorder chequeTableEtchedBorder =new EtchedBorder();

protected ChequeTypeComboBox chequeTypeColEditor =new ChequeTypeComboBox();

protected DateField dateEditor =new DateField();

/**

* Default Constructor for UncompletedSessionNotesTable

*/

public ChequeDetailsTable(){

super(new ChequeDetailsTableModel());

setColumnEditors();

this.getScrollPane().setBorder(chequeTableEtchedBorder);

this.setPreferredScrollableViewportSize(new Dimension(800, 150));

}

/* (non-Javadoc)

* @see com.bankframe.bfa.fe.ui.ETable#setupColumns()

*/

publicvoid setupColumns(){

this.setupColumn(0, ClientMessages.BANK_NAME, 200,true);

this.setupColumn(1, ClientMessages.ISSUING_DATE, 100,true);

this.setupColumn(2, ClientMessages.CHEQUE_NUMBER, 200,true);

this.setupColumn(3, ClientMessages.AMOUNT, 130,true);

this.setupColumn(4, ClientMessages.CHEQUE_TYPE, 170, true,ConstantsKeysImpl.CHEQUE_TYPE);

}

publicvoid setColumnEditors(){

TableColumn bankNameColumn = this.getColumn(ClientMessages.BANK_NAME);

ETextField editableTextField0 =new ETextField();

DefaultCellEditor editableEditor0 =new DefaultCellEditor(editableTextField0);

editableTextField0.setDocument(EDocumentUtilities.getAlphaNumericDocument(35));

bankNameColumn.setCellEditor(editableEditor0);

TableColumn issuingDateColumn = this.getColumn(ClientMessages.ISSUING_DATE);

issuingDateColumn.setCellEditor(new DateEditor());

TableColumn chequeNumberColumn = this.getColumn(ClientMessages.CHEQUE_NUMBER);

ETextField editableTextField2 =new ETextField();

DefaultCellEditor editableEditor2 =new DefaultCellEditor(editableTextField2);

editableTextField2.setDocument(EDocumentUtilities.getAlphaNumericDocument(35));

chequeNumberColumn.setCellEditor(editableEditor2);

TableColumn amountColumn = this.getColumn(ClientMessages.AMOUNT);

ETextField editableTextField3 =new ETextField();

DefaultCellEditor editableEditor3 =new DefaultCellEditor(editableTextField3);

editableTextField3.setDocument(EDocumentUtilities.getDoubleDocument());

amountColumn.setCellEditor(editableEditor3);

chequeTypeColEditor.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);

TableColumn chequeTypeColumn = this.getColumn(ClientMessages.CHEQUE_TYPE);

TableCellEditor chequeTypeEditor =new DefaultCellEditor(chequeTypeColEditor);

final JComboBox renderer =new JComboBox(new Object[]{"1","2","3"});

TableCellRenderer cellRenderer =new TableCellRenderer(){

public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected,boolean hasFocus,int row,int col){

renderer.setSelectedItem(value);

return renderer;

}

};

chequeTypeColumn.setCellEditor(chequeTypeEditor);

}

publicclass DateEditorextends DateFieldimplements TableCellEditor, ActionListener{

protected EventListenerList listenerList =new EventListenerList();

protected ChangeEvent changeEvent =new ChangeEvent(this);

protectedint rowNumber = 0;

protectedint colNumber = 0;

public DateEditor(){

addActionListener(this);

}

publicvoid actionPerformed(ActionEvent e){

String returnValue = this.openCalendar();

if (!DataTypeValidator.isNullOrEmpty(returnValue)){

this.setText(returnValue);

fireEditingStopped();

}else{

fireEditingCanceled();

}

//this dont work here

changeSelection(getSelectedRow(), 2, false,false);

}

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

rowNumber = row;

colNumber = column;

this.setText((String)value);

returnthis;

}

publicvoid addCellEditorListener(CellEditorListener listener){

listenerList.add(CellEditorListener.class, listener);

}

publicvoid removeCellEditorListener(CellEditorListener listener){

listenerList.remove(CellEditorListener.class, listener);

}

protectedvoid fireEditingStopped(){

CellEditorListener listener;

Object[] listeners = listenerList.getListenerList();

for (int i = 0; i < listeners.length; i++){

if (listeners[i] == CellEditorListener.class){

listener = (CellEditorListener) listeners[i + 1];

listener.editingStopped(changeEvent);

}

}

}

protectedvoid fireEditingCanceled(){

CellEditorListener listener;

Object[] listeners = listenerList.getListenerList();

for (int i = 0; i < listeners.length; i++){

if (listeners[i] == CellEditorListener.class){

listener = (CellEditorListener) listeners[i + 1];

listener.editingCanceled(changeEvent);

}

}

}

publicvoid cancelCellEditing(){

fireEditingCanceled();

}

publicboolean stopCellEditing(){

fireEditingStopped();

returntrue;

}

publicboolean isCellEditable(EventObject event){

returntrue;

}

publicboolean shouldSelectCell(EventObject event){

returntrue;

}

public Object getCellEditorValue(){

return this.getText();

}

}

}

publicclass DateFieldextends CDataField{

protected GridBagLayout gridBagLayout1 =new GridBagLayout();

protected ETextField dateTextField =new ETextField();

protected CalendarButton dateCalendarButton =new CalendarButton();

public DateField(){

initializeGUI();

}

public DateField(int length){

dateTextField.setColumns(length);

initializeGUI();

}

publicvoid setNextFocusableComponent(Component c){

dateCalendarButton.setNextFocusableComponent(c);

}

public Component getField(){

return dateTextField;

}

/**

* This method initializes the DateField

*/

protectedvoid initializeGUI(){

FlowLayout flowLayout =new FlowLayout(FlowLayout.LEFT);

this.setLayout(gridBagLayout1);

dateTextField.setBorder(Borders.getDateFieldBorder());

dateCalendarButton.getInsets().left = 0;

dateCalendarButton.getInsets().right = 0;

dateCalendarButton.getInsets().top = 0;

dateCalendarButton.getInsets().bottom = 0;

this.setBorder(BorderFactory.createLineBorder(Color.BLACK));

this.add(dateTextField,new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH,new Insets(0, 0, 0, 0), 0, 0));

this.add(dateCalendarButton,new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE,new Insets(0, 0, 0, 0), 0, 0));

}

publicvoid setActionCommand(String actionCommand){

dateCalendarButton.setActionCommand(actionCommand);

}

publicvoid addKeyListener(KeyListener l){

dateTextField.addKeyListener(l);

}

publicvoid removeKeyListener(KeyListener l){

dateTextField.removeKeyListener(l);

}

publicvoid addActionListener(ActionListener listener){

dateCalendarButton.addActionListener(listener);

}

publicvoid removeActionListener(ActionListener listener){

dateCalendarButton.removeActionListener(listener);

}

public String openCalendar(){

return dateCalendarButton.openCalendar(dateCalendarButton.getLocationOnScreen());

}

//public CalendarButton getDateCalendarButton(){

//return this.dateCalendarButton;

//}

publicvoid setColumns(int columns){

dateTextField.setColumns(columns);

}

publicvoid setSelectionEnd(int p){

dateTextField.setSelectionEnd(p);

}

publicvoid setMaximumTextSize(int textSize){

dateTextField.setMaximumTextSize(textSize);

}

publicvoid setCaretPosition(int caretPosition){

dateTextField.setCaretPosition(caretPosition);

}

public String getText(){

return dateTextField.getText();

}

publicvoid setText(String text){

dateTextField.setText(text);

}

publicvoid setDocument(Document doc){

dateTextField.setDocument(doc);

}

public Document getDocument(){

return dateTextField.getDocument();

}

publicvoid selectAll(){

dateTextField.selectAll();

}

publicvoid setEnabled(boolean enabled){

dateTextField.setEnabled(enabled);

dateCalendarButton.setEnabled(enabled);

}

publicvoid setHorizontalAlignment(int alignment){

dateTextField.setHorizontalAlignment(alignment);

}

publicvoid setEditable(boolean editable){

dateTextField.setEditable(editable);

}

}

[17598 byte] By [seamy_maca] at [2007-10-3 9:32:36]
# 1

> //this dont work here

> changeSelection(getSelectedRow(), 2, false, false);

Did you display the value of getSelectRow() to make sure it is correct?

I would try wrapping the code in a SwingUtilities.invokeLater() so the code gets executed after the editor has been removed.

camickra at 2007-7-15 4:47:39 > top of Java-index,Desktop,Core GUI APIs...