How to focus on JTextField in a table

I write a search and replace function in the form of a spreadsheet program. I want the program to show the table with the item found when searching for a row.

So far so good, but I can’t get the element to get focus using the cursor so that you can immediately start typing.

I use custom JTableas well as custom TableCellEditor. The following tricks do not seem to work: (as part of a custom 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 (good flowchart) showing how events happen? Or an example of code that can do something like this?

+3
source share
3

googling : JTable :

( JTable)

editCellAt(row,column);

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

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

?

+2

editcellat ?

, /implemenet, true

    /**
     * Returns true.
     * @param anEvent  an event object
     * @return true
     */
    public boolean shouldSelectCell(EventObject anEvent) { 
    return true; 
    }
0

Make sure you enable the selection in your custom table instance, for example, below:

table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);

In this case, as a rule, the call table.editCellAt(row, col);starts editing. Example:

JTable myTable = new JTable(rows, cols);
myTable.setColumnSelectionAllowed(true);
myTable.setRowSelectionAllowed(true);

and somewhere else. where change is required

boolean wasEditStarted = table.editCellAt(row, col);
if (wasEditStarted) {
  table.changeSelection(row, col, false, false);
}
0
source

All Articles