Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextArea Focus using "Tab" in JTable

Tags:

java

swing

jtable

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?

like image 224
Akash Avatar asked Sep 12 '11 15:09

Akash


2 Answers

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();
        }
    }

};
like image 68
camickr Avatar answered Sep 30 '22 16:09

camickr


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);
}    
like image 26
Dave R. Avatar answered Sep 30 '22 14:09

Dave R.