Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.java.* > Newsgroup comp.lang.java.gui

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-22-2012, 11:09 PM
Adam Beneschan
Guest
 
Posts: n/a
Default Passing KeyEvent upward

Hi,

I'm trying to learn how to program in Java and use some of the GUI features.. Here's something I'm trying to do but haven't succeeded at yet.

I have a text window (JTextPane) object, and I want to set things up so that when a certain something happens, a popup menu (JPopupMenu) will come up.But I'd like it so that if the user presses a key, the key is handled just as if the menu hadn't come up--that is, the key-press event will be handled by the JTextPane (or is it the associated document?) as if the user had typed that key into the document. I've already set up a MenuKeyListener for the popup menu that recognizes the KeyEvent and calls menu.setVisible(false) to get rid of the menu. So far, so good. But I don't know how to get the KeyEvent propagated to the text window. I've tried getKeyListeners() on the JTextPane and then calling the keyTyped explicitly on each listener. No good--the only listener it found was one I had added myself for debugging. I also tried textPane.getInputContext().dispatchEvent(e) on the KeyEvent; that didn't do what I wanted either (seemed to have no effect).

Any thoughts? I haven't found anything else to try.

Thanks,

-- Adam


Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 07-23-2012, 12:26 AM
markspace
Guest
 
Posts: n/a
Default Re: Passing KeyEvent upward

On 7/22/2012 4:09 PM, Adam Beneschan wrote:
> if the user presses a key, the
> key is handled just as if the menu hadn't come up--that is, the
> key-press event will be handled by the JTextPane (or is it the
> associated document?) as if the user had typed that key into the



Sounds like an accelerator to me.

<http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html>

Also take a look at Actions.


> document. I've already set up a MenuKeyListener for the popup menu
> that recognizes the KeyEvent and calls menu.setVisible(false) to get
> rid of the menu. So far, so good.



Ick. Sounds terrible. You seem to be making things worse since you
don't know about the existing APIs that do menu acceleration. I'd stop,
make a super small example to get your thoughts straightened out, then
consider how best to rework your app with your new ideas.

Reply With Quote
  #3 (permalink)  
Old 07-23-2012, 12:39 AM
markspace
Guest
 
Posts: n/a
Default Re: Passing KeyEvent upward

On 7/22/2012 5:26 PM, markspace wrote:

> Sounds like an accelerator to me.



Here's the simplest example I could think of. Tell me if it works like
what you want.



package quicktest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

/**
*
* @author Brenden
*/
public class Accelerator {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();

JTextArea ta = new JTextArea();
frame.add(ta);

JMenuBar mbar = new JMenuBar();
JMenu file = new JMenu("File");
mbar.add(file);
JMenuItem blarg = new JMenuItem("blarg");
blarg.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
blarg.addActionListener( new Blarg( ta ) );
file.add(blarg);
frame.setJMenuBar(mbar);

frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize( 400, 400 );
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

private static class Blarg implements ActionListener {

private final JTextArea ta;
public Blarg(JTextArea ta) {
this.ta = ta;
}

@Override
public void actionPerformed(ActionEvent e) {
ta.append( " blarg" );
}
}
}

Reply With Quote
  #4 (permalink)  
Old 07-23-2012, 01:32 AM
John B. Matthews
Guest
 
Posts: n/a
Default Re: Passing KeyEvent upward

In article <0f05834e-c654-4925-b78a-b8328b23ba0b@googlegroups.com>,
Adam Beneschan <adam@irvine.com> wrote:

> I'm trying to learn how to program in Java and use some of the GUI
> features. Here's something I'm trying to do but haven't succeeded at
> yet.
>
> I have a text window (JTextPane) object, and I want to set things up
> so that when a certain something happens, a popup menu (JPopupMenu)
> will come up. But I'd like it so that if the user presses a key, the
> key is handled just as if the menu hadn't come up--that is, the
> key-press event will be handled by the JTextPane (or is it the
> associated document?) as if the user had typed that key into the
> document. I've already set up a MenuKeyListener for the popup menu
> that recognizes the KeyEvent and calls menu.setVisible(false) to get
> rid of the menu. So far, so good. But I don't know how to get the
> KeyEvent propagated to the text window. I've tried getKeyListeners()
> on the JTextPane and then calling the keyTyped explicitly on each
> listener. No good--the only listener it found was one I had added
> myself for debugging. I also tried
> textPane.getInputContext().dispatchEvent(e) on the KeyEvent; that
> didn't do what I wanted either (seemed to have no effect).
>
> Any thoughts? I haven't found anything else to try.


I would endorse markspace's suggestion. For even more flexibility,
extend AbstractAction and use instances in your popup items, as shown
here:

<http://stackoverflow.com/a/5129757/230513>
<http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html>

JTextPane supports a large number of predefined Action implementations
that operate on the current selection of the Document, as shown in these
examples:

<http://stackoverflow.com/a/10568672/230513>
<http://stackoverflow.com/a/8534162/230513>
<http://www.artima.com/forums/flat.jsp?forum=1&thread=1276>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
Reply With Quote
  #5 (permalink)  
Old 07-24-2012, 04:15 AM
Adam Beneschan
Guest
 
Posts: n/a
Default Re: Passing KeyEvent upward

On Sunday, July 22, 2012 5:26:08 PM UTC-7, markspace wrote:
> On 7/22/2012 4:09 PM, Adam Beneschan wrote:
> &gt; if the user presses a key, the
> &gt; key is handled just as if the menu hadn't come up--that is, the
> &gt; key-press event will be handled by the JTextPane (or is it the
> &gt; associated document?) as if the user had typed that key into the
>
>
> Sounds like an accelerator to me.


No, I don't think so. If I understand the term correctly, an accelerator just gives you a quick way to select a menu item (possibly from a submenu deeper in the menu tree) using the keyboard. That isn't what I want here.

But thanks for the info. I'll look into it. It may be a few days before I can try it, though.

-- Adam

>
> <http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html>
>
> Also take a look at Actions.
>
>
> > document. I've already set up a MenuKeyListener for the popup menu
> > that recognizes the KeyEvent and calls menu.setVisible(false) to get
> > rid of the menu. So far, so good.

>
>
> Ick. Sounds terrible. You seem to be making things worse since you
> don't know about the existing APIs that do menu acceleration. I'd stop,
> make a super small example to get your thoughts straightened out, then
> consider how best to rework your app with your new ideas.


Reply With Quote
  #6 (permalink)  
Old 07-24-2012, 06:08 AM
markspace
Guest
 
Posts: n/a
Default Re: Passing KeyEvent upward

On 7/23/2012 9:15 PM, Adam Beneschan wrote:

> No, I don't think so. If I understand the term correctly, an
> accelerator just gives you a quick way to select a menu item
> (possibly from a submenu deeper in the menu tree) using the keyboard.
> That isn't what I want here.
>



I have no idea what you are asking for then. Could you give a specific
example?

Reply With Quote
  #7 (permalink)  
Old 07-24-2012, 06:46 AM
Roedy Green
Guest
 
Posts: n/a
Default Re: Passing KeyEvent upward

On Sun, 22 Jul 2012 16:09:55 -0700 (PDT), Adam Beneschan
<adam@irvine.com> wrote, quoted or indirectly quoted someone who said
:

>I'm trying to learn how to program in Java and use some of the GUI features=
>. Here's something I'm trying to do but haven't succeeded at yet.


my essay on how events work should help you with the necessary
background.

see http://mindprod.com/jgloss/event11.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
The greatest shortcoming of the human race is our inability to understand the exponential function.
~ Dr. Albert A. Bartlett (born: 1923-03-21 age: 89)
http://www.youtube.com/watch?v=F-QA2rkpBSY


Reply With Quote
  #8 (permalink)  
Old 07-24-2012, 08:15 PM
Knute Johnson
Guest
 
Posts: n/a
Default Re: Passing KeyEvent upward

On 7/22/2012 4:09 PM, Adam Beneschan wrote:
> Hi,
>
> I'm trying to learn how to program in Java and use some of the GUI
> features. Here's something I'm trying to do but haven't succeeded at
> yet.
>
> I have a text window (JTextPane) object, and I want to set things up
> so that when a certain something happens, a popup menu (JPopupMenu)
> will come up. But I'd like it so that if the user presses a key, the
> key is handled just as if the menu hadn't come up--that is, the
> key-press event will be handled by the JTextPane (or is it the
> associated document?) as if the user had typed that key into the
> document. I've already set up a MenuKeyListener for the popup menu
> that recognizes the KeyEvent and calls menu.setVisible(false) to get
> rid of the menu. So far, so good. But I don't know how to get the
> KeyEvent propagated to the text window. I've tried getKeyListeners()
> on the JTextPane and then calling the keyTyped explicitly on each
> listener. No good--the only listener it found was one I had added
> myself for debugging. I also tried
> textPane.getInputContext().dispatchEvent(e) on the KeyEvent; that
> didn't do what I wanted either (seemed to have no effect).
>
> Any thoughts? I haven't found anything else to try.
>
> Thanks,
>
> -- Adam
>
>


You can just call processKeyEvent() on the JTextPane and pass it the
KeyEvent that you got from your JPopupMenu. I don't think I would do
that though, I think I would create a non-modal Dialog instead of the
PopupMenu and put the focus back on to the JTextPane. That's assuming
that you are doing what I think you are doing.
Reply With Quote
  #9 (permalink)  
Old 07-25-2012, 04:08 AM
Adam Beneschan
Guest
 
Posts: n/a
Default Re: Passing KeyEvent upward

On Tuesday, July 24, 2012 1:15:56 PM UTC-7, Knute Johnson wrote:
>
> You can just call processKeyEvent() on the JTextPane and pass it the
> KeyEvent that you got from your JPopupMenu. I don't think I would do
> that though, I think I would create a non-modal Dialog instead of the
> PopupMenu and put the focus back on to the JTextPane. That's assuming
> that you are doing what I think you are doing.


Thanks--processKeyEvent() actually did what I was trying to do. (It's protected so I had to do a little extra work to make things work.) I'm not sure it's the best way to do things, so I'm still planning on looking over everyone else's suggestions and reading the link Roedy pointed me to, so that I can figure out what's going on.

The "actual" situation (it's an experimental program only, to help me learn) is something like this: Suppose you have a text window where you want the user to be able to enter and edit text; but at certain points, depending on what the user just typed in, the program may decide that there are two or more likely "completions" to the word (or whatever) that the user startedtyping. So the program automatically brings up a menu. The user can thenselect one of the completions off the menu. But if she keeps typing, I want the key to work as it normally would if the menu hadn't popped up. That's why I wanted to "pass it back up" (if that's the correct direction ) to the text editing window, and let it handle it the way it would have with no menu. (This kind of user interface is probably more complicated than necessary if the user were just typing in English text, but I have something else in mind.)

Thank you all for your help, I really appreciate it. Even though I have *a* solution, I'm still planning on looking over the rest of the answers to learn whatever I can.

-- Adam


Reply With Quote
  #10 (permalink)  
Old 07-25-2012, 05:50 AM
markspace
Guest
 
Posts: n/a
Default Re: Passing KeyEvent upward

On 7/24/2012 9:08 PM, Adam Beneschan wrote:
> Suppose you have a text window where
> you want the user to be able to enter and edit text; but at certain
> points, depending on what the user just typed in, the program may
> decide that there are two or more likely "completions" to the word
> (or whatever) that the user started typing. So the program
> automatically brings up a menu. The user can then select one of the
> completions off the menu. But if she keeps typing, I want the key to
> work as it normally would if the menu hadn't popped up.



Oh, hmm. I can see a couple of solutions. One is to require the "pick"
keys be different from the normal "edit" keys. Another is just to use
"pick" keys that the use won't miss much.

My IDE does this. I'm pretty sure it captures the keys, and doesn't
"pass them back up". Things would be confusing if it did. There's only
four keys the pop-up menu needs. Up and down arrows to select, enter
key to pick the selection, and escape to dismiss the pop up. Most folks
won't miss those keys, or they can hit escape first if they need to use
the arrow keys or press enter.

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 11:18 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.