Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a code snippet that I put together for a project that I was working on. The code has been tested and verified for a table that has non-editable cells in the first and last column. The class restricts tabbing to only the editable cells of the table. It also supports shift-tabbing to tab in reverse.</p> <pre><code>public class JTableCellTabbing { /** * * Creates a new {@code JTableCellTabbing} object. * * */ private JTableCellTabbing() { } /** * * Set Action Map for tabbing and shift-tabbing for the JTable * * * @param theTable - Jtable with NRows and MCols of cells * @param startRow - valid start row for tabbing [ 0 - (numRows-1) ] * @param numRows - Number of rows for tabbing * @param startCol - valid start col for tabbing [ 0 - (numCols-1) ] * @param numCols - Number of columns for tabbing */ @SuppressWarnings("serial") static public void setTabMapping(final JTable theTable, final int startRow, final int numRows, final int startCol, final int numCols) { if (theTable == null) { throw new IllegalArgumentException("theTable is null"); } // Calculate last row and column for tabbing final int endRow = startRow + (numRows - 1); final int endCol = startCol + (numCols - 1); // Check for valid range if ((startRow &gt; endRow) || (startCol &gt; endCol)) { throw new IllegalArgumentException("Table Size incorrect"); } // Get Input and Action Map to set tabbing order on the JTable InputMap im = theTable.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); ActionMap am = theTable.getActionMap(); // Get Tab Keystroke KeyStroke tabKey = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0); am.put(im.get(tabKey), new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { int row = theTable.getSelectedRow(); int col = theTable.getSelectedColumn(); col++; // Move to next row and left column if (col &gt; endCol) { col = startCol; row++; } // Move to top row if (row &gt; endRow ) { row = startRow; } // Move cell selection theTable.changeSelection(row, col, false, false); } }); // Get Shift tab Keystroke KeyStroke shiftTab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, java.awt.event.InputEvent.SHIFT_DOWN_MASK); am.put(im.get(shiftTab), new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { int row = theTable.getSelectedRow(); int col = theTable.getSelectedColumn(); col--; // Move to top right cell if (col &lt; startCol) { col = endCol; row--; } // Move to bottom row if (row &lt; startRow ) { row = endRow; } // Move cell selection theTable.changeSelection(row, col, false, false); } }); } </code></pre> <p>}</p> <p>And here's how the class is used for your table:</p> <pre><code>JTable myTable = new JTable(); // Set up table attributes.... JTableCellTabbing.setTabMapping(myTable, 0, NUM_ROWS, 1, (NUM_COLS-1)); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload