🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
3 messages
3 total messages Started by John T. Dow Thu, 16 Jun 2005 04:01
Swap Enter and Ctrl-Enter for JTextArea
#540
Author: John T. Dow
Date: Thu, 16 Jun 2005 04:01
22 lines
1917 bytes
Recently there was a discussion of "Swap TAB and Ctrl-TAB for 
JTextArea". I want to swap Enter and Ctrl-Enter also.

Using keyPressed and keyReleased events I have been able to get Tab and 
Enter to leave the field.

However, I don't know how to make Ctrl-Enter put an "Enter" into the 
text, that is, a hard return.

That's where I need help. It seems that you can't use setKeyChar in 
keyReleased or keyPressed events, only in keyTyped events. But trying 
e.setKeyChar((char)KeyEvent.VK_ENTER) doesn't work.

By the way, my client wants this behavior (his legacy app behaves that 
way) so it doesn't matter what's standard. Actually, it's not 
unreasonable for him to expect to lean on the tab or enter key to move 
through the fields - that is, all fields should behave the same. Text 
areas are used for comments, which sometimes have paragraph breaks.

Thanks.

John
Re: Swap Enter and Ctrl-Enter for JTextArea
#541
Author: Tony
Date: Thu, 16 Jun 2005 05:55
40 lines
2350 bytes
>
> However, I don't know how to make Ctrl-Enter put an "Enter" into the text, 
> that is, a hard return.
>

 add the following code to the constructor of the small wrapper class
(e.g. MyTextArea) which extends the JTextArea

        this.addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent ke)
            {
                if (ke.getKeyCode() == KeyEvent.VK_TAB &&
                    ke.isShiftDown())
                {
                     ke.consume();
                    TTextArea.this.transferFocusBackward();
                }
                else if (ke.getKeyCode() == KeyEvent.VK_TAB)
                {
                    ke.consume();
                    TTextArea.this.transferFocus();
                }
                else if (ke.getKeyCode() == KeyEvent.VK_ENTER
                          && ke.isControlDown())
                {
                    ke.setKeyCode(KeyEvent.VK_ENTER);
                    // The following uses a deperecated API,
                    // it seems that there is not workaround
                    ke.setModifiers(0);
                    return;
                }
                else if (ke.getKeyCode() == KeyEvent.VK_ENTER)
                {
                    ke.consume();
                    TTextArea.this.transferFocus();
                }
            }
        });
Re: Swap Enter and Ctrl-Enter for JTextArea
#542
Author: John T. Dow
Date: Thu, 16 Jun 2005 14:48
46 lines
2600 bytes
This is what I was missing:  ke.setModifiers(0);

Thanks.

Tony wrote:
>>However, I don't know how to make Ctrl-Enter put an "Enter" into the text, 
>>that is, a hard return.
>>
> 
> 
>  add the following code to the constructor of the small wrapper class
> (e.g. MyTextArea) which extends the JTextArea
> 
>         this.addKeyListener(new KeyAdapter()
>         {
>             public void keyPressed(KeyEvent ke)
>             {
>                 if (ke.getKeyCode() == KeyEvent.VK_TAB &&
>                     ke.isShiftDown())
>                 {
>                      ke.consume();
>                     TTextArea.this.transferFocusBackward();
>                 }
>                 else if (ke.getKeyCode() == KeyEvent.VK_TAB)
>                 {
>                     ke.consume();
>                     TTextArea.this.transferFocus();
>                 }
>                 else if (ke.getKeyCode() == KeyEvent.VK_ENTER
>                           && ke.isControlDown())
>                 {
>                     ke.setKeyCode(KeyEvent.VK_ENTER);
>                     // The following uses a deperecated API,
>                     // it seems that there is not workaround
>                     ke.setModifiers(0);
>                     return;
>                 }
>                 else if (ke.getKeyCode() == KeyEvent.VK_ENTER)
>                 {
>                     ke.consume();
>                     TTextArea.this.transferFocus();
>                 }
>             }
>         });
> 
> 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads