Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable and tab order.

 
tim johnson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple JTable. The table is populated dynamically using data from the database that is retrieved based on the search criteria entered by the user via a text field.
On pressing tab key from the search button above the table, I want the first row of the table to be selected. Similarly after the last row is selected and tab key is pressed, I want the next button to be the focus. How can this be done?
Right now, when I press tab key from the search button, it seems to go to the table header.
Also, when I press tab key from the last row, it goes again to the first row of the table instead of the next component.
Please help,
Thanks,
 
lavanya subramanian
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

the solution is not perfect but a try add keyListener to the JTable
table1.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode() == KeyEvent.VK_TAB)
{
if(table1.isCellSelected(table1.getRowCount()-1,table1.getColumnCount()-1) == true)
{
table1.setNextFocusableComponent(button2);
table1.setManagingFocus(false);(see MYTable class at the end)

}
}
}
});
the problem with this solution is you can change the selection as you wish only once(if looped). the second time if you try the last cell in the JTable will be always selected.

sorry i couldn't give you perfect solution,
if not looped with the components(button1--->JTable--->button2---->button1),this works fine.

import javax.swing.JTable;
public class MyTable extends JTable
{
boolean result = true;
public MyTable()
{
super();
}
public void setManagingFocus(boolean state)
{
this.result=state;
}
public boolean isManagingFocus()
{
return result;
}
}

please let me know ,
lavanya
 
tim johnson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lavanya,
Thanks for the reply. I was able to get what I want by adding keyListeners to the JTable exactly like you have explained.
I have one more requirement.
I want to be able to select non-sequential rows in the table. This can be achieved using <CTRL> key and the mouse. How can this be achieved using only the keyboard?
Thanks,
 
lavanya subramanian
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Tim,
sorry i couldn't understand what is non sequential rows ,is there any sequence created in the table and you want to select the odd ones,kindly explain.
thanks,
lavanya
 
tim johnson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lavanya,
Sorry for not being clear.
By non-sequential rows, I meant the ability of the user to select rows in any order. For example the user may want to select the second, third and fifth row in a table of five rows OR he may want to select the first, third and fifth row OR the first, fourth and fifth row, and so on.
This can be achieved by using the <CTRL> key and the mouse. The user holds the <CTRL> key and makes whatever selection he wants using the mouse. I want to provide the same feature using the keyboard keys only, without the use of the mouse.
If he wants to select sequential rows, this can be done using the keys only: select the first row to be selected using the down arrow key. Then hold the <SHIFT> key and keep using the down arrow key until you reach the last row that needs to be selected.
How can the selection of non-sequential rows be done using the keys only.
Thanks,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic