JTable and Enter key

  • Follow


Hi,

   I have a standard table with all elements enabled for editing. When
I finish editing and press "enter" key the focus moves down.
   How can I change this behavior to move right instead?

Thanks.
0
Reply Mike314 6/23/2009 7:54:37 AM

Mike314 wrote:
> Hi,
> 
>    I have a standard table with all elements enabled for editing. When
> I finish editing and press "enter" key the focus moves down.
>    How can I change this behavior to move right instead?

Can't see this behaviour:

import java.awt.event.*;
import javax.swing.*;

public class Test {
     private void createAndShowGUI() {
         JTable table = new JTable(new String[][]{
             {"1,1", "1,2"},
             {"2,1", "2,2"}}, new String[]{"A", "B"});
         JFrame frame = new JFrame("Test");
         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         frame.add(new JScrollPane(table));
         frame.pack();
         frame.setVisible(true);
     }

     public static final void main(String args[]) {
         SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 new Test().createAndShowGUI();
             }
         });
     }
}

Bye
Michael
0
Reply Michael 6/23/2009 8:37:05 AM


On Jun 23, 6:37=A0pm, Michael Rauscher <michlm...@gmx.de> wrote:
> Mike314 wrote:
> > Hi,
>
> > =A0 =A0I have a standard table with all elements enabled for editing. W=
hen
> > I finish editing and press "enter" key the focus moves down.
> > =A0 =A0How can I change this behavior to move right instead?
>
> Can't see this behaviour:
>
> import java.awt.event.*;
> import javax.swing.*;
>
> public class Test {
> =A0 =A0 =A0private void createAndShowGUI() {
> =A0 =A0 =A0 =A0 =A0JTable table =3D new JTable(new String[][]{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0{"1,1", "1,2"},
> =A0 =A0 =A0 =A0 =A0 =A0 =A0{"2,1", "2,2"}}, new String[]{"A", "B"});
> =A0 =A0 =A0 =A0 =A0JFrame frame =3D new JFrame("Test");
> =A0 =A0 =A0 =A0 =A0frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE=
);
> =A0 =A0 =A0 =A0 =A0frame.add(new JScrollPane(table));
> =A0 =A0 =A0 =A0 =A0frame.pack();
> =A0 =A0 =A0 =A0 =A0frame.setVisible(true);
> =A0 =A0 =A0}
>
> =A0 =A0 =A0public static final void main(String args[]) {
> =A0 =A0 =A0 =A0 =A0SwingUtilities.invokeLater(new Runnable() {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0public void run() {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0new Test().createAndShowGUI();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0 =A0});
> =A0 =A0 =A0}
>
> }
>
> Bye
> Michael

1. Run
2. Choose first cell (1,1)
3. press some keys (like 123) to change the cell's value. (or even do
nothing - same result)
4. press enter
5. The focus changes to lower cell (2,1).

I need it to change to (1,2) instead.
0
Reply Mike314 6/23/2009 8:57:51 AM

> 
> I need it to change to (1,2) instead.

You could dig in ActionMap of the table. The action associated whith the 
enter key must be in one of the 3 maps (depending of the focus).
0
Reply Albert 6/23/2009 9:02:34 AM

Mike314 wrote:
> On Jun 23, 6:37 pm, Michael Rauscher <michlm...@gmx.de> wrote:
>> Mike314 wrote:
>>> Hi,
>>>    I have a standard table with all elements enabled for editing. When
>>> I finish editing and press "enter" key the focus moves down.
>>>    How can I change this behavior to move right instead?
>> Can't see this behaviour:
>>
> 1. Run
> 2. Choose first cell (1,1)
> 3. press some keys (like 123) to change the cell's value. (or even do
> nothing - same result)
> 4. press enter
> 5. The focus changes to lower cell (2,1).

OK, happens if one doesn't double-click or press F2 to start editing.

> 
> I need it to change to (1,2) instead.
         table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
         put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
             "selectNextColumnCell");

Bye
Michael
0
Reply Michael 6/23/2009 9:55:05 AM

In article 
<b5dfa6e0-26a7-40a4-b464-aa460645d720@12g2000pri.googlegroups.com>,
 Mike314 <michaelst@gmail.com> wrote:

> I have a standard table with all elements enabled for editing. When I 
> finish editing and press "enter" key the focus moves down. How can I 
> change this behavior to move right instead?

Use a key binding, for example: 

JTable table = new JTable(...);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
InputMap map = table.getInputMap(JTable.WHEN_FOCUSED);
map.put(enter, "selectNextColumnCell");

The condition, WHEN_FOCUSED, is one of several defined in JComponent, 
and the actionMapKey, "selectNextColumnCell", is one known to 
BasicTableUI:

<http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
0
Reply John 6/23/2009 10:17:35 AM

On Jun 23, 7:55=A0pm, Michael Rauscher <michlm...@gmx.de> wrote:
> Mike314 wrote:
> > On Jun 23, 6:37 pm, Michael Rauscher <michlm...@gmx.de> wrote:
> >> Mike314 wrote:
> >>> Hi,
> >>> =A0 =A0I have a standard table with all elements enabled for editing.=
 When
> >>> I finish editing and press "enter" key the focus moves down.
> >>> =A0 =A0How can I change this behavior to move right instead?
> >> Can't see this behaviour:
>
> > 1. Run
> > 2. Choose first cell (1,1)
> > 3. press some keys (like 123) to change the cell's value. (or even do
> > nothing - same result)
> > 4. press enter
> > 5. The focus changes to lower cell (2,1).
>
> OK, happens if one doesn't double-click or press F2 to start editing.
>
>
>
> > I need it to change to (1,2) instead.
>
> =A0 =A0 =A0 =A0 =A0table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_=
COMPONENT).
> =A0 =A0 =A0 =A0 =A0put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
> =A0 =A0 =A0 =A0 =A0 =A0 =A0"selectNextColumnCell");
>
> Bye
> Michael

Thank you very much. It is what I was looking for.
0
Reply Mike314 6/24/2009 7:08:49 AM

6 Replies
975 Views

(page loaded in 0.146 seconds)

Similiar Articles:













7/20/2012 5:58:58 AM


Reply: