Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3962 > unrolled thread

Re: Printable character a

Started by"Martin" <martin@THRWHITE.remove-dii-this>
First post2011-04-27 15:47 +0000
Last post2011-04-27 15:48 +0000
Articles 7 — 3 participants

Back to article view | Back to comp.lang.java.gui

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Printable character a "Martin" <martin@THRWHITE.remove-dii-this> - 2011-04-27 15:47 +0000
    Re: Printable character a "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:47 +0000
      Re: Printable character a "Martin" <martin@THRWHITE.remove-dii-this> - 2011-04-27 15:47 +0000
        Re: Printable character a "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:47 +0000
          Re: Printable character a "Martin" <martin@THRWHITE.remove-dii-this> - 2011-04-27 15:47 +0000
            Re: Printable character a "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:47 +0000
              Re: Printable character a "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000

#3962 — Re: Printable character a

From"Martin" <martin@THRWHITE.remove-dii-this>
Date2011-04-27 15:47 +0000
SubjectRe: Printable character a
Message-ID<c35ed499-ccf3-425b-bd38-6c89f6a04e2b@y38g2000hsy.googlegroups.com>
  To: comp.lang.java.gui
On 12 Aug., 13:39, Andrew Thompson <andrewtho...@gmail.com> wrote:

> > =A0I am using a simple character (e.g. 'n') without a CTRL or ALT mask
> > as acceleator for a JMenuItem.
>
> Why?

Because I emulate a known set of keyboard shortcuts (vi).

> Generally an SSCCE* will get more and faster attention
> than code snippets.

Agreed, but I don't see how I can break this down into a few lines of
code.

The question is of more general nature - is this behaviour
("printable" key used as JMenu accelerator not consumed) well-known
and documented? Is there any way programmatically consume the
accelerator key?

Cheers,

Martin

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#3963

From"RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this>
Date2011-04-27 15:47 +0000
Message-ID<48a19954$0$26077$db0fefd9@news.zen.co.uk>
In reply to#3962
  To: comp.lang.java.gui
Martin wrote:
> Andrew Thompson wrote:
>> Martin Wrote
>> 
>>> I am using a simple character (e.g. 'n') without a CTRL or ALT
>>> mask as acceleator for a JMenuItem. ...  When I access this
>>> function through the accelerator the key is not consumed before
>>> the editing mode is entered, i.e. the 'n' appears in the input
>>> field.
> 
>> Generally an SSCCE* will get more and faster attention than code
>> snippets.
> 
> Agreed, but I don't see how I can break this down into a few lines of
>  code.

Are you sure you aren't being just a bit lazy? Here's an SSCCE that
illustrates what you describe:

---------------------------- 8< ----------------------------------
public class TestAccellerator2 {
     public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 new TestAccellerator2();
             }
         });
     }

     TestAccellerator2() {

         JTextArea area = new JTextArea(5,40);

         JMenuBar bar = new JMenuBar();
         JMenu menu = new JMenu("Do");
         JMenuItem item = new JMenuItem("Nothing");
         item.setMnemonic(KeyEvent.VK_N);
         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, 0));
         item.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 System.out.println(e.getActionCommand()+" pressed.");
             }
         });
         menu.add(item);
         bar.add(menu);

         JPanel p = new JPanel();
         p.add(area);

         JFrame f = new JFrame("Test Menu Accelerator");
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.setJMenuBar(bar);
         f.add(p);
         f.pack();
         f.setLocationRelativeTo(null);
         f.setVisible(true);
     }

}
---------------------------- 8< ----------------------------------

I've no idea what you mean by "table.startEditingAt(42,0,0);" but it
appears to be irrelevant.

-- 
RGB

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#3965

From"Martin" <martin@THRWHITE.remove-dii-this>
Date2011-04-27 15:47 +0000
Message-ID<2065b48d-998f-4724-a2b8-38d84c6c0acf@c65g2000hsa.googlegroups.com>
In reply to#3963
  To: comp.lang.java.gui
On 12 Aug., 16:08, RedGrittyBrick <RedGrittyBr...@SpamWeary.foo>
wrote:

> Are you sure you aren't being just a bit lazy? Here's an SSCCE that
> illustrates what you describe:

Not lazy, just ignorant :) I had not been aware that this is
reproducable this easily. Thank you very much for this demonstration.
Next time I'll know better.

As your demo program demonstrates the same problem, have you got an
idea how to get rid of the 'n' typed?

Cheers,

Martin

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#3966

From"RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this>
Date2011-04-27 15:47 +0000
Message-ID<48a1b13d$0$26091$db0fefd9@news.zen.co.uk>
In reply to#3965
  To: comp.lang.java.gui

Martin wrote:
> On 12 Aug., 16:08, RedGrittyBrick <RedGrittyBr...@SpamWeary.foo>
> wrote:
> 
>> Are you sure you aren't being just a bit lazy? Here's an SSCCE that
>> illustrates what you describe:
> 
> Not lazy, just ignorant :) I had not been aware that this is
> reproducable this easily. Thank you very much for this demonstration.
> Next time I'll know better.
> 
> As your demo program demonstrates the same problem, have you got an
> idea how to get rid of the 'n' typed?

No.

To me it doesn't make sense to have a JTextArea (or JTable cell etc) 
into which one can type any letter except the letter 'n'.

Supposing I am typing in the word "tent", when I get to 'n' what should 
happen?

If 'n' is not valid input, Maybe your widget should set an InputDocument 
or use another mechanism that filters out the 'n'.

You mentioned vi earlier. Vi(m) gets round this by being modal. If you 
want to use Vi-like accelerators you should set Esc as an accelerator to 
enter command mode and disable all the input widgets, then have 
'i','o','a' enter input mode and enable the widgets again. Frankly I 
think this would be weird (much as I love vim).


For 'n' without modifiers, you might want to consider replacing
         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, 0));
with
         item.setAccelerator(KeyStroke.getKeyStroke('n'));

It doesn't solve your problem but it is more readable. It also prevents 
'n' being interpreted as BOTH a literal character and an accelerator.


I'd set the accellerator to Alt+n. to avoid the issue.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#3971

From"Martin" <martin@THRWHITE.remove-dii-this>
Date2011-04-27 15:47 +0000
Message-ID<1f8c92e0-fc45-4406-87a2-68e3e6057999@d1g2000hsg.googlegroups.com>
In reply to#3966
  To: comp.lang.java.gui
On 12 Aug., 17:50, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:

> To me it doesn't make sense to have a JTextArea (or JTable cell etc)
> into which one can type any letter except the letter 'n'.

After having entered editing mode the keyboard accelerators are no
longer active if they are visual commands. Thus this problem would not
occur.

Even if the above scenario does not seem to make sense for an
application I would still expect the swing API to support such an
implementation. If - what I find a bit strange - the key stroke which
triggered a menu item as an accelerator is not consumed before
entering the action associated with the menu item, is there maybe a
way to consume the key stroke manually? E.g. before start editing a
table cell, can I empty the keyboard input buffer?

Cheers,

Martin

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#3972

From"RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this>
Date2011-04-27 15:47 +0000
Message-ID<48a2afe6$0$2525$da0feed9@news.zen.co.uk>
In reply to#3971
  To: comp.lang.java.gui

Martin wrote:
> I would still expect the swing API to support such an 
> implementation. If - what I find a bit strange - the key stroke which
>  triggered a menu item as an accelerator is not consumed before 
> entering the action associated with the menu item,

Did you try my earlier suggestion:

>> you might want to consider replacing 
>>    item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, 0)); 
>> with
>>    item.setAccelerator(KeyStroke.getKeyStroke('n')); 
>>
>> ... It ... prevents 'n' being interpreted as BOTH a literal
>> character and an accelerator.


Compile this, run it and press n. Now press n again.

--------------------------------- 8< --------------------------
public class TestAccellerator2 {

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

     TestAccellerator2() {

         final JCheckBox box = new JCheckBox("Pencils");
         final JTextArea area = new JTextArea(5, 40);

         JMenuBar bar = new JMenuBar();
         JMenu menu = new JMenu("Do");
         JMenuItem item = new JMenuItem("Nothing");
         item.setMnemonic(KeyEvent.VK_N);
         item.setAccelerator(KeyStroke.getKeyStroke('n'));
         item.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 System.out.println(e.getActionCommand() + " pressed.");
                 area.requestFocus();
             }
         });
         menu.add(item);
         bar.add(menu);

         JPanel p = new JPanel();
         p.add(box);
         p.add(area);

         SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 box.requestFocus();
             }});

         JFrame f = new JFrame("Test Menu Accelerator");
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.setJMenuBar(bar);
         f.add(p);
         f.pack();
         f.setLocationRelativeTo(null);
         f.setVisible(true);
     }

}
--------------------------------- 8< --------------------------

I am wondering why you are not producing SSCCEs of your own.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#3976

From"Lew" <lew@THRWHITE.remove-dii-this>
Date2011-04-27 15:48 +0000
Message-ID<WuidnelkNdLqSz_VnZ2dnUVZ_g-dnZ2d@comcast.com>
In reply to#3972
  To: comp.lang.java.gui
RedGrittyBrick wrote:
> I am wondering why you are not producing SSCCEs of your own.

Yeah.

-- 
Lew

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web