I am able to setfocus to a single cell in JTable using mouseclick, but while using the tab to move between cells, the next selected tab cell just seems to be highlighted, not focused.
Is there a way out to set the focus of a cell using "Tab" key?
Override the changeSelection() method of JTable:
JTable table = new JTable(...)
{
// Place cell in edit mode when it 'gains focus'
public void changeSelection(
int row, int column, boolean toggle, boolean extend)
{
super.changeSelection(row, column, toggle, extend);
if (editCellAt(row, column))
{
Component editor = getEditorComponent();
editor.requestFocusInWindow();
// ((JTextComponent)editor).selectAll();
}
}
};
For me, a problem appeared with method #2, where tabbing after a mouse selection took the focus to the first column rather than the next column. I fixed it by calling the super.changeSelection
after the if
statement.
public void changeSelection(final int row, final int column, boolean toggle, boolean extend)
{
if (editCellAt(row, column))
{
getEditorComponent().requestFocusInWindow();
}
super.changeSelection(row, column, toggle, extend);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With