Link to home
Start Free TrialLog in
Avatar of sankars98
sankars98

asked on

JTable Question


 Does anyone know how to activate the  
 CellEditor component when the
 user presses Tab key in a JTable ?

 TIA,

 Sankar S.
Avatar of vivexp
vivexp

Hi,

Visit this site for examples on JTable
http://www2.gol.com/users/tame/swing/examples/JTableExamples2.html
make somethink like this

extend JTable
    /**
    * Override JComponent.processKeyEvent() This allows us to give a special meaning to ENTER and DELETE in the JTable.
    * {@link JComponent#registerKeyboardAction} http://www.javaworld.com/javaworld/javatips/jw-javatip72.html
    * @author TogetherSoft LLC
    */
    protected void processKeyEvent(KeyEvent e) {
       if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_TAB) {
                //System.out.println("row="+row);
        int row = getSelectionModel().getAnchorSelectionIndex()
int column=getSelectedColumn();
                 editCellAt(row, column);
                //e.consume(); ?
       
        } else {
            super.processKeyEvent(e);
        }
    }


also look:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.htm 

http://manning.spindoczine.com/sbe/files/uts2/Chapter18html/Chapter18.htm 
http://users.vnet.net/wwake/swing/faq.html#table
http://www.codeguru.com/java/Swing/JTable/index.shtml
ASKER CERTIFIED SOLUTION
Avatar of shaveri
shaveri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sankars98

ASKER


shaveri,

   Thank you very much man..
   It helped me alot. I included
   one more line to enable the
   cell editor.

   vladi's solution works fine
   but the cursor is not blinking
   inside the cell.

   Here is the code i am using now.
   This will help the others.(
   have to press two TABS. Have to
   do something for that ).

            table.registerKeyboardAction( new AbstractAction() {
                  public void actionPerformed (ActionEvent e) {
                        table.requestFocus();
                        int ancRow = table.getSelectedRow();
                        int ancCol = table.getSelectedColumn();
                        if( (ancRow != -1)  && ( ancCol != -1 )){
                              if ((ancCol + 1) >= table.getColumnCount()) {
                                    ancCol = 0;
                                    ancRow = ((ancRow + 1) >= table.getRowCount()) ? 0 : (ancRow + 1);
                              }else
                                    ancCol++;
                              
                              table.setRowSelectionInterval(ancRow, ancRow);
                              table.setColumnSelectionInterval(ancCol, ancCol);
                              table.editCellAt( ancRow , ancCol );
                              DefaultCellEditor editor = (DefaultCellEditor)table.getCellEditor( ancRow , ancCol );
                              if(editor != null ){
                                    editor.getComponent().requestFocus();
                              }
                        }
                  }},
                  KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0),
                  JComponent.WHEN_FOCUSED);


 Thanks again,

Sankar S.
no points for me :)

 Vladi ,

   Have posted another question
   for you. :-).Lock it please..

   Thanks,

 Sankar S.