tags:

views:

383

answers:

2

I'm writing a search & replace function in a kind of spreadsheet program. What I want is that if you search for a string, the program shows a table with the element that has been found.

So far so good, but I cannot get the element to obtain the focus, with the cursor in it so you can immediately start typing.

I'm using a customized JTable and also a customized TableCellEditor. The following tricks do not seem to work: (within the customized TableCellEditor):

SwingUtilities.invokeLater(new Runnable() { 
                              public void run() { 
                                  my_textfield.requestFocus(); 
                              } 
} );

or:

my_jtable.editCellAt(0, 3);
my_jtable.requestFocus();

or

my_jtable.getEditorComponent().requestFocusInWindow();

Am I missing something? Is there a good description (nice flow diagram) that shows how events take place? Or example code that might do something similar?

A: 

Did you try the editcellat without the requestfocus ?

also make sure that you override/implemenet to return true

    /**
     * Returns true.
     * @param anEvent  an event object
     * @return true
     */
    public boolean shouldSelectCell(EventObject anEvent) { 
    return true; 
    }
Peter
No, this does not work. I also added a println in the above function just to see if it gets called. But it only gets called if I do the call explicitly:StructureCellEditor sce = (StructureCellEditor) my_table.getCellEditor(0, 3);sce.shouldSelectCell(...);
Roalt
Note that above the StructureCellEditor is my own extended TableCellEditor
Roalt
+1  A: 

With some googling i found a forum thread : programmatically start editing a cell in a JTable answered with following idea:

(in a subclass of JTable)

editCellAt(row,column);

requestFocus();
DefaultCellEditor ed = (DefaultCellEditor)
getCellEditor(row,column);

ed.shouldSelectCell(new ListSelectionEvent(this,row,row,true));

Would it work?

Touko
I *think* the last line should be ...(this,row,column,true));I'm not sure if I tried the exact sequence as written above, but I will check it out.
Roalt
I think the (this, row, row, true) is the correct - the constructor is : ListSelectionEvent(Object source, int firstIndex, int lastIndex, boolean isAdjusting) - so the row is the only index to be selected..
Touko