sbrattla
sbrattla

Reputation: 5386

JTable : is it possible to disable "scroll to column"?

I've got a JTable with a model that has about 20 columns. That's more than you can fit in a single screen, so scrollbars enable the users to scroll up/down and right/left.

Now, if a user scrolls all the way to the right and clicks on a row, then that row gets selected just fine. However, if the user then use the scrollbars to scroll all the way to the left, and then press the down arrow key, the JTable automatically scrolls all the way to the right again (and selects the next row). It is as if the JTable remembers the column the user first clicked in, and when using the down arrow key the JTable just takes that column and moves down one row and scrolls back to that column.

Is there a way to disable this behaviour, so that the user remains in the selected view without JTable doing all this "magic" scrolling?

Upvotes: 3

Views: 2798

Answers (3)

Reto Höhener
Reto Höhener

Reputation: 5808

I'm preventing this behavior with this JTable override.

Note that my table does not care about cell selection, and only paints row selection (no cell selection border).

  @Override
  public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)
  {
    // essentially disabling cell selection, selected column index will always be 0
    columnIndex = 0;
    super.changeSelection(rowIndex, columnIndex, toggle, extend);
  }

Upvotes: 0

Efthymis
Efthymis

Reputation: 1326

You could try setColumnSelectionAllowed(false), so that the user cannot select the column in the first place.

Upvotes: 1

Peter
Peter

Reputation: 5798

Scrolling a JTable isent connected to the cell selection.

Clicking on a cell will make the Jtable put its curose onto that cell. this means all future navigating will be from the last clicked place. Nomatter how much you scroll that last location will be the starting point of the key navigation.

But in fact the behaviour you describe is just the standard in about any gui. Take Intellij, Excel, Word, Editplus,... if you use arrow keys to navigate you always scroll back to where last clicked.

but GUi discussions aside back to your problem

i think you can make it work with

setAutoscrolls(false);

on your jtable

Upvotes: 4

Related Questions