Results 1 to 6 of 6
  1. #1
    mandelbrot is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default ActionListener+KeyListener

    Hello!
    I'm new to this forum so be gentle.
    I have to write a program that counts the number of times the button
    is pushed. That is, the button is pushed by mouse click or by pressing
    Alt+O. Now, the first part is easy, but i don't know the second part. How
    do i combine those two events together?
    The program does the first part, but it doesn't count Alt+0 presses, not
    even prints out Alt+O.



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

    class Counter extends JFrame implements KeyListener
    {
    private static String txt = "Counter: ";
    private int count = 0;

    public Counter()
    {
    setTitle("Title");
    setSize(300,200);
    addKeyListener(this);
    final JLabel label = new JLabel(txt + "0 ");
    final JButton button = new JButton("Push!");

    getContentPane().setLayout(new GridLayout(0, 1));
    getContentPane().add(label);
    getContentPane().add(button);
    pack();
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    count++;
    label.setText(txt + count);
    }
    });
    }


    public void keyTyped ( KeyEvent e ){ }
    public void keyPressed(KeyEvent e)
    {
    int code = e.getKeyCode();
    //System.out.println("keyPressed : code = "+code);

    if(code == KeyEvent.VK_O && e.isAltDown())
    {
    count++;
    System.out.println("keyPressed : Alt_O"+count);
    }

    }
    public void keyReleased ( KeyEvent e ){ }


    public static void main(String[] args)
    {
    Counter frame = new Counter();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E );
    frame.setVisible(true);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by mandelbrot View Post
    Hello!
    I'm new to this forum so be gentle.
    I have to write a program that counts the number of times the button
    is pushed. That is, the button is pushed by mouse click or by pressing
    Alt+O. Now, the first part is easy, but i don't know the second part. How
    do i combine those two events together?
    The program does the first part, but it doesn't count Alt+0 presses, not
    even prints out Alt+O.
    You don't want to use a key listener. Instead set the JButton's mnemonic via the setMnemonic method

    Then when the user presses the alt-key combination the JButton will be pressed and its action listener will be activated.

    Oh, and welcome to the forum, Mr. Benoît, sir.

  3. #3
    mandelbrot is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default

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

    class Counter extends JFrame implements ActionListener
    {
    private static String txt = "Counter: ";
    private int count = 0;
    JLabel label;
    JButton button;

    public Counter()
    {
    setTitle("Title");
    setSize(300,200);
    label = new JLabel(txt + "0 ");
    button = new JButton("Push!");

    getContentPane().setLayout(new GridLayout(0, 1));
    getContentPane().add(label);
    getContentPane().add(button);
    pack();

    button.setMnemonic(KeyEvent.VK_O);
    button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
    count++;
    label.setText(txt + count);
    }


    public static void main(String[] args)
    {
    Counter frame = new Counter();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }

    }


    It took a little but it's done. Thanks!
    Any room for improvement?

  4. #4
    m-i-l-s-o-n is offline Member
    Join Date
    Sep 2010
    Posts
    24
    Rep Power
    0

    Default

    U didnt add alt key in ur code

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by m-i-l-s-o-n View Post
    U didnt add alt key in ur code
    Why should he? Have you read the API regarding the setMnemonic method of AbstractButton?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by mandelbrot View Post
    It took a little but it's done. Thanks!
    Any room for improvement?[/QUOTE]

    Sure. Add color:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ColorCounter extends JPanel {
       private static final int IMAGE_SIZE = 300;
       private BufferedImage image = null;
       private static String txt = "Counter: ";
       private int count = 0;
       private JLabel label;
       private JButton button;
    
    
       public ColorCounter() {
          setPreferredSize(new Dimension(IMAGE_SIZE, IMAGE_SIZE));
          setBackground(Color.black);
          image = bilinearGradient();
          
          label = new JLabel(txt + "0 ", SwingConstants.CENTER);
          label.setForeground(Color.white);
          
          button = new JButton("Push!");
          button.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                count++;
                label.setText(txt + count);
             }
          });
          button.setMnemonic(KeyEvent.VK_O);
          JPanel buttonPanel = new JPanel(new GridBagLayout());
          buttonPanel.setOpaque(false);
          buttonPanel.add(button);
    
          setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
          setLayout(new BorderLayout(10, 10));
          add(label, BorderLayout.NORTH);
          add(buttonPanel, BorderLayout.CENTER);
       }
       
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          if (image != null) {
             g.drawImage(image, 0, 0, null);
          }
       }
    
       private BufferedImage bilinearGradient() {
          int imageSize = IMAGE_SIZE;
    
          BufferedImage image = new BufferedImage(imageSize, imageSize, 
                   BufferedImage.TYPE_INT_ARGB);
          int colorInt = 0;
          for (int x = 0; x < imageSize; x++) {
             for (int y = 0; y < imageSize; y++) {
                colorInt = getColorInt(x, y);
                image.setRGB(x, y, colorInt);
             }
          }
          return image;
       }
    
       private int getColorInt(int x, int y) {
          double xRatio = (double) x / IMAGE_SIZE;
          double yRatio = (double) y / IMAGE_SIZE;
    
          int red = (int) (0xFF * (xRatio) * (1 - yRatio));
          int green = (int) (0xFF * (1 - xRatio) * (1 - yRatio));
          int blue = (int) (0xFF * (xRatio) * (yRatio));
    
          int rgb = (0xFF << 0x18) | (red << 0x10) | (green << 0x8) | (blue);
    
          // Color c = new Color(red, green, blue);
          // return c.getRGB();
          
          return rgb;
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Counter");
          frame.getContentPane().add(new ColorCounter());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

Similar Threads

  1. keyListener not doing anything
    By imorio in forum AWT / Swing
    Replies: 10
    Last Post: 08-17-2010, 10:46 PM
  2. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 04:14 PM
  3. KeyListener Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:46 PM
  4. How to use KeyListener and ActionListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:19 PM
  5. how to add a KeyListener
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •