Is it possible to gain Tab Focus of a JTable Header Cell? - java

I have a JTable that has a table header which contains a JPanel with a JLabel with a mouse listener on the JLabel. When the JLabel is clicked a dialog is shown. I'm trying to trigger this JLabel in the JTable heading without a mouse. In order to trigger this JLabel without a mouse, I need the focus to be on the JTable heading (that is, I need the JTable heading to be in the focus traversal policy). Does anybody know if its possible to Tab to a JTable heading cell?

table.getTableHeader().setFocusable(true)
alternatively, you might add the action "triggered by the JLabel" to the table's action map and bind it to a keyStroke in the table's when_ancestor inputMap

Focus can be placed on the TableHeader by using F8 when focus is on the table.
You can then use the right/left keys to move between the header cells.
You use the Tab key to place focus back on the table.

Related

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.

How to synchronize row header JTable scrolling with another table using keyboard arrow keys

In a component class, I have 2 JTables, one is a fixed row header table and another is a data table that carries the values for the row header. I have a JScrollPane set for the data table such that it will scroll simultaneously for both tables.
In another class, I have a JPanel which I add the component class above to display.
In the panel, the scrolling of the tables is fine when I scroll using the mouse wheel or by clicking the scroll bars. However, if I select the row header and press the down or up arrow key from the keyboard, I notice that only the row header continues to scroll, but the data table does not move. Thus, the tables rows do not align properly anymore.
In another note, I noticed that if I select the data table row instead and scroll using the up/down arrow keys, both the tables are able to scroll properly in synchronization!
Would anyone know why is this so and how I may solve the scrolling problem if the row header table was selected?
Check out Fixed Column Table
The logic there uses a ChangeListener to synchronize the scrolling of the row header with the vertical scrollbar of the scrollpane.
The basic code is:
scrollPane.getRowHeader().addChangeListener( this );
and the listener:
public void stateChanged(ChangeEvent e)
{
// Sync the scroll pane scrollbar with the row header
JViewport viewport = (JViewport) e.getSource();
scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
}

Usage of Jtextarea in JTable

I have a JTABLE with three columns. First column is textfield, the Second and third is JTextarea.. Using tab key I can navigate from one cell to another. While navigating from Jtextfield column to JTextarea column , The JTextarea is not highlighted, how to set a color for JTextarea when it get focused , so I can easily find which column has the focus.. How I will set that color.?
Please give some suggestions..
Thanks in Advance
Vishwa
Create a Color object and invoke the setBackground(Color) method on your JTextArea, after checking if it has focus using the hasFocus().
For example:
JTextArea area = new JTextArea();
Color c = Color.CYAN;
if(area.hasFocus())
{
area.setBackground(c);
}
Don't forget to save the original color of the (accessible by calling area.getBackground()) before you change it, and to set it back to this when it loses focus.

JComboBox sizing issue in a table with Multi line Cells

I recently had a problem where I needed to have a field that can wrap the text and increase the height of the row as the text is wrapped, similar to Microsoft Excel. I managed to get it working correctly, the only problem is that the table contains multiple JComboBoxes. When the row's height increases from the field that wraps the text, the size of the JComboBox window and ArrowButton also increase. I am using a DefaultCellEditor for the JComboBox fields, and created my own Editor/Renderer to be used with the JTextArea field. Once the JComboBox's value is selected, the value gets displayed correctly in the field, the only problem is while I am selecting the value, the JComboBox window and ArrowButton could be HUGE depending on the size of the row. Is there any way to increase the height of the row, but have the JComboBox field height remain the same instead of growing to fill the column that it's in? I am thinking I might need to make a Custom Cell Editor for the JComboBox fields as well instead of using the default. Thanks in advance!
I am thinking I might need to make a
Custom Cell Editor for the JComboBox
fields as well instead of using the
default
That would probably be the solution since the size of the editor is determined by the size of the cell.
I would try using a JPanel with a BorderLayout as the editor component. Then you add your editor to the North of the panel.
It would be the easiest editor to create since all the mouse events and key events are passed to the editor I believe, which means the panel will get the events, not the combo box. So I guess you would need to forward these events to the combo box.
First, is the JComboBox in a BorderLayout and set to BorderLayout.CENTER?
If so, I'd change it to a different layout such as AbsoluteLayout so it does not stretch to fill the cell.
Also, I will also refer you to this post Putting JComboBox into JTable

Is it possible to have a JPanel over JTable

I have a JTable whose associated TableModel could be initially empty. Therefore, it currently shows a JTable with its columns and no rows.
In order to fill this JTable, I want the user to drag and drop elements from another component. The problem is that I would like to hint the user that he/she should drag elements to this table, with some message like "Drag xxx here to add a row".
I thought that I could achieve this by putting a panel over the JTable , but I don't think it is possible with any java layout.
Does anyone know how to do this? Or should I stick to a CardLayout to switch to/from the hint and the JTable?
Thanks a lot
Take a look at OverlayLayout, I think it might do what you want.
A couple of suggestions:
Add a tooltip to the JTable using setToolTipText(String). You'll need to add it to the surrounding JScrollPane in order for the tooltip to be displayed when the user hovers on the empty viewport.
Add a titled Border; e.g. scrollPane.setBorder(BorderFactory.createTitledBorder("Drag items here"));
I would do it like this...Create a JPanel to contain the JTable on top and a JLabel on the bottom. Something like this...
JPanel container = new JPanel( new BorderLayout() );
container.add( table, BorderLayout.CENTER );
JLabel label = new JLabel( "Drag XXX here to add a row" );
container.add( table, BorderLayout.SOUTH );
Then make both the label and table drop targets. You might need to play with the label to make it look and behave right. Set opaque to true possibly? Set its background to white to match the table? Also maybe play with the borders of all three to make them look integrated with each other. You could remove the label after the first row is added if that is the behavior you want.

Categories