How to enable using arrows keys to move row selection in JTable? - java

I noticed that I can you arrows to move row selection of my JTable object only when I press tab key. Is it possible to use arrows after row selection by mouse-click (instead of using TAB)?

In order for the arrow keys to change row selection, the JTable must have focus. Pressing the tab key changes focus to the next (or first) "focussable" Component on the page which is likely a SubComponent in the JTable.
To get it to focus automatically when it becomes visible, add a ComponentListener with the an componentShown(...) method implemented to call the JTable's requestFocusInWindow() method.
Is it possible to use arrows after row selection by mouse-click (instead of using TAB)
Yes; if you click the mouse on a row, that should also focus the row, allowing you to use the arrow keys as well.
Updated: corrected method used to get input focus, with thanks to camickr (see comments)

Related

How to add a control panel on a JTable row on MouseOver (hover)

I have a non editable table, on which I'd like to display a row contextual panel (likely a JPanel). Somewhat like Gmail is doing: when moving the mouse over mail rows there's a simple tool bar showing up on that specific row.
Like in gmail the action of controls I'd like to display won't edit the values, instead they will use the value in the row to perform some offer work.
I have played with the following :
TableCellRenderer, the display mostly works, but it has limitations:
the component is only used for rendering, so one cannot use it to simply add multiple buttons
it requires another column
for the hovering behavior (ie display on row only when the mouse is hovering the row) it requires collaboration with the table's MouseListener
TableCellEditor, my table is not editable so the cell editor is neither called
it also requires a specific column
it also requires collaboration with the table's MouseListener
MouseMotionListener can be used to display a popup for certain coordinates
the popup feels like it's the right component for this
there's quite some code to handle the popup lifecycle (closing it when the mouse move out of the row, don't re-open a popup if there is already one open)
tool tips: as far as I am aware the swing tooltips do not allow to have control components like buttons, etc
I did related question and answer on stack overflow. But they all require to add a column to display and use these swing components.
Given that you have posted no code, this question is a bit broad.
Nevertheless, the way to do it would be to stick a JPanel in a JPopupMenu. You need to create a listener on your GUI to know when and where the JPopupMenu should appear
--- Edit ---
I think you have to add JMenus to a JPopupMenu, and what I suggested about adding a JPanel won't work cleanly. You can either use JPopupMenu, or use a JWindow and put your JPanel in that.

while selecting rows on jtable using keyboard up and down keys focus is lost

aPanel.getTable().setRowSelectionAllowed(true);
aPanel.getTable().requestFocusInWindow();
Still the focus gets lost and we had to regain it using mouse click.
jtable focus is lost on scrolling using up and down arrow keys and focus gets shifted to other component say button. How to regain focus to continue scrolling on rows the problem occurs for java 8.
tableName.requestFocus();
Solution: Your table doesn't know when to lose or gain focus, we have to mention explicitly this into our Table in ListSelectionListener event. requestFocus() will ensure this functionality.
will work in this case as it will prevent losing focus from the table. Please Note: no changes required in fireDataTableChanged(); method

JTable setRowSelectionInterval has no effect for JButton

Using a JTable, my Table Model setValueAt() method moves the selection to the next row in certain cases, using setRowSelectionInterval() and setColumnSelectionInterval(). When it's called from the (default) cell editor (by user typing in the cell and hitting tab), the code works: the desired next cell is selected (the first one on the next row).
However, if the user uses Return rather than Tab to commit the edit, the selection doesn't happen; instead the cell below is selected. That's fine with me.
I also have a JButton to clear a row. The button's action function calls the model's setValueAt() function for the desired cells. Unfortunately, the setRowSelectionInterval() and setColumnSelectionInterval() methods have no apparent effect; instead, no cells are selected.
I've tried table.requestFocusInWindow() and table.getParent().requestFocusInWindow(), as well as table.changeSelection(row, 0, false, false), all to no apparent effect.
Is there anything basic I'm missing here, before I go to the trouble of building the SSCCE?
In case it matters, here's the container hierarchy:
parent JPanel
button rows JPanel
button row 1 JPanel
button row 2 JPanel
table JScrollpane
JTable
The button in question is in button row 1.
Thanks!
Maybe you can use the Table Cell Listener to listen for edits to the table. It listens for actual changes done by the JTable editor.
Then in the supplied Action you can select the appropriate row. You may need to wrap the Action code in a SwingUtilities.invokeLater(...) to make sure the code executes after the table is completely finished editing.

JTable cell value passed to textarea on single click

On my form I have a jtable and a textarea. My table only has 2 columns - ID and Comment
Is it possible that when a user clicks on a cell in the comment column. The cell value will appear in the textarea in edit mode?
I did set the cell editor to singleclick
selectTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
TableColumn col = selectTable.getColumnModel().getColumn(1);
DefaultCellEditor singleclick = new DefaultCellEditor(new JTextField());
singleclick.setClickCountToStart(1);
col.setCellEditor(singleclick);
I have a method outputSelection() that gets called from a edit button. The method gets the value from the selected cell and puts the value in the textarea for edit.
Can the click activate the method so the user does not have to click a edit button?
You could attach a mouse listener to the table and monitor the mouse clics from there, getting the selected column/row & thus the value
You could supply your own cell editor that updates the text area when the editors value is set
You could extend te jtable & when cell editing is started, update the text area
Yes, this is a process I learned to use after having duplicate code throughout my swing application. I started making standalone methods that did the work I wanted, and then I call those methods from the action events from the button or mouse click. That way they all execute the same code.
Even if you have a tab or enter key command, you can also have it execute your same method as the others for more consistent code.
If your button performs specific code with cell values, just extract all of that code out into a method that takes the cell value as input. Then you can call that same method from any event and pass in the input data you want to display in the text area.

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?
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
You could try setColumnSelectionAllowed(false), so that the user cannot select the column in the first place.
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);
}

Categories