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


Groups > comp.lang.java.gui > #5198

Re: Passing KeyEvent upward

From markspace <-@.>
Newsgroups comp.lang.java.gui
Subject Re: Passing KeyEvent upward
Date 2012-07-22 17:39 -0700
Organization A noiseless patient Spider
Message-ID <jui6fj$47d$1@dont-email.me> (permalink)
References <0f05834e-c654-4925-b78a-b8328b23ba0b@googlegroups.com> <jui5n3$vq$1@dont-email.me>

Show all headers | View raw


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_CLOSE);
             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" );
       }
    }
}

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Passing KeyEvent upward Adam Beneschan <adam@irvine.com> - 2012-07-22 16:09 -0700
  Re: Passing KeyEvent upward markspace <-@.> - 2012-07-22 17:26 -0700
    Re: Passing KeyEvent upward markspace <-@.> - 2012-07-22 17:39 -0700
    Re: Passing KeyEvent upward Adam Beneschan <adam@irvine.com> - 2012-07-23 21:15 -0700
      Re: Passing KeyEvent upward markspace <-@.> - 2012-07-23 23:08 -0700
  Re: Passing KeyEvent upward "John B. Matthews" <nospam@nospam.invalid> - 2012-07-22 21:32 -0400
  Re: Passing KeyEvent upward Roedy Green <see_website@mindprod.com.invalid> - 2012-07-23 23:46 -0700
  Re: Passing KeyEvent upward Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2012-07-24 13:15 -0700
    Re: Passing KeyEvent upward Adam Beneschan <adam@irvine.com> - 2012-07-24 21:08 -0700
      Re: Passing KeyEvent upward markspace <-@.> - 2012-07-24 22:50 -0700
  Re: Passing KeyEvent upward Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-07-25 00:32 +0200

csiph-web