Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTable keypress 1

Status
Not open for further replies.

Grentis

Programmer
Aug 18, 2003
13
IT
Hi,
I want that when a user click RETURN a particular cell will be selected. I try to set a keypress event but he doesn't work.
How can I do what I want?

Thanks
Grentis
 
KeyPress should work - post your select code.
If you are handling it from JTable, then you need
to get the current selection and set the new one
if there is one (if you are on the last cell, perhaps you want
to cycle to the first, or rather to the next control...).
 
This is the code that I wrote

void table_keyPressed(KeyEvent e) {
if(e.getKeyCode()==10){
if(this.table.getCellEditor()!=null)
this.table.getCellEditor().stopCellEditing();
if(this.table.getSelectedColumn()==2){
if(this.table.getSelectedRow()==this.table.getRowCount()-1)
( (TabellaLavoriFatturaClienteModel)this.table.getModel()).addRow(new Vector());
this.table.setRowSelectionInterval(this.table.getRowCount-1,this.table.getRowCount-1);
this.table.setColumnSelectionInterval(0,0);
} else{
this.table.setColumnSelectionInterval(this.table.getSelectionColumn+1,this.table.getSelectionColumn+1);

.........

}
}
}

Thanks
Grentis
 
Code:
if(e.getKeyCode()==10){
Wasn't it 'VK_Enter' or something like that?
(Using a literal number is bad style and not guaranteed to work.)
And why do you allways use 'this'? Are there other 'table's around there.

Perhaps it depends on your invokation - where do you call table_keyPressed and how?

And what means: 'doesn't work'? no reaction, wrong reaction, exception? - assuming the first.
But did you try to catch another keyEvent which works?

Last but not least: If in doubt, use the 'preview post'-button in this forum. Since you didn't put a blank in your code, it wasn't broken into multiple lines.
While this is what you want in your editor, the forum-layout can get really annoying.

And use [ code] tags [ /code] (without the blanks) to format your code:
Code:
void table_keyPressed (KeyEvent e) 
{
	if (e.getKeyCode ()==10)
	{
//...
		{
			table.setColumnSelectionInterval (table.getSelectionColumn+1, table.getSelectionColumn+1);
 
Thanks for the suggestions.
My problem is that I want customize my JTable navigation...
I want (when I click RETURN key) the same navigation that I have if I press the TAB key.

In fact with TAB I have
cell(0,0) -> cell(0,1) -> cell (0,2) ->
cell(1,0) -> cell(1,1) -> cell(1,2)
...

With RETURN I have
cell(0,0) -> cell(1,0) -> cell(0,1) -> cell(1,1) ->
cell(0,2) -> cell(1,2)

and it's not what I want.

The key_pressed method works but with it I can't personalize the navigation

Grentis
 
Thanks to all...
I resolved it
:p


Grentis
 
You have to create a class that extends JTable and then you have to rewrite the method processKeyBinding ...

Code:
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,int condition, boolean pressed) {
    selRow = getSelectedRow();
    selCol = getSelectedColumn();
    if (ks == KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0)) {
      if(getCellEditor()!=null)
        getCellEditor().stopCellEditing();

      this.getComponentAt((int)getCellRect(selRow,selCol,true).getX(),(int)getCellRect(selRow,selCol,true).getY()).transferFocus();
      if(selCol==2){
        if(selRow==getRowCount()-1){
          ( (TabellaLavoriFatturaClienteModel) getModel()).addRow(new Vector());
        }
        targetRow = selRow + 1;
        targetCol = 0;
      }else{
        targetRow = selRow;
        targetCol = selCol+1;
      }
      this.setRowSelectionInterval(targetRow,targetRow);
      this.setColumnSelectionInterval(targetCol,targetCol);
      SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          getComponentAt((int)getCellRect(targetRow,targetCol,true).getX(),(int)getCellRect(targetRow,targetCol,true).getY()).requestFocus();
        }
      });
      return true;
    }
    return super.processKeyBinding(ks,e,condition,pressed);
  }

That's all

Grentis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top