JTable Question


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

 TIA,

 Sankar S.
LVL 1
sankars98Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
shaveriConnect With a Mentor Commented:
visit the site.............
http://www.deja.com/getdoc.xp?AN=371151822
0
 
vivexpCommented:
Hi,

Visit this site for examples on JTable
http://www2.gol.com/users/tame/swing/examples/JTableExamples2.html
0
 
vladi21Commented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
sankars98Author Commented:

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.
0
 
vladi21Commented:
no points for me :)
0
 
sankars98Author Commented:

 Vladi ,

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

   Thanks,

 Sankar S.
0
All Courses

From novice to tech pro — start learning today.