Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Keyboard Listener or different approach

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Keyboard Listener or different approach

    Ok I'll admit I don't know all about programming so perhaps I am taking the wrong approach but need some assistance.

    At work we connect to a server that has the Sales software. There are times the connection goes down and we are stuck doing the receipts by hand. I am trying to emulate the software so we can have it print out the receipts and enter them when the connection is back. This way we can save time and have them still look professional.

    The issue I am running into is not much, but it is slightly annoying me since I am trying to make the function of the Offline POS as close as possible as the original. Ok so in the original POS software there are buttons you can click to do a (sale, layaway, etc..) or it also listens for key presses like F1 = Sale, F2 = Layaway and so on. I am unable to emulate this completely and I am trying to figure out if it is possible to do this.

    I've made a small example to demonstrate what I mean.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Listeners extends JFrame{
      public static void main(String[] args){
        Listeners frmMain = new Listeners();
      }
     
      // Declare components
      JButton btnF1;
      JButton btnF2;
      JLabel lblMessage;
     
      public Listeners(){
        setupComponents();
        ////
        // Setup the parameters for the window
        ////
        setTitle("Listeners testing");
        setLayout(null);
        setSize(600, 400);
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
      }
     
      public void setupComponents(){
        ////
        // Sales button
        ////
        btnF1 = new JButton("F1");
        btnF1.setSize(60, 20);
        btnF1.setLocation(20, 20);
        btnF1.addKeyListener(new KeyAdapter(){
          public void keyPressed(KeyEvent e){
            if (e.getKeyCode() == 112)
              lblMessage.setText("F1 pressed");
          }
        });
        add(btnF1);
     
        ////
        // Layaway button
        ////
        btnF2 = new JButton("F2"); 
        btnF2.setSize(60, 20);
        btnF2.setLocation(100, 20);
        btnF2.addKeyListener(new KeyAdapter(){
          public void keyPressed(KeyEvent e){
            if (e.getKeyCode() == 113)
              lblMessage.setText("F2 pressed");
          }
        });
        add(btnF2);
     
        ////
        // Labels to dispaly testing information
        ///
        lblMessage = new JLabel("No action captured");
        lblMessage.setSize(200, 30);
        lblMessage.setLocation(20, 200);
        add(lblMessage);
     
        ////
        // Add mouse listener to frame for debugging purposes
        ////
        addMouseListener(new MouseAdapter(){
          public void mouseClicked(MouseEvent e){
            btnF1.setFocusable(btnF1.isFocusable() ? false : true);
            btnF2.setFocusable(btnF2.isFocusable() ? false : true);
            lblMessage.setText("Buttons " + (btnF1.isFocusable() ? " are focusable" : " are not focusable"));
            btnF1.grabFocus();
          }
        });
     
        ////
        // Add a Keyboard Listener to frame to test if will get all
        // Input even when other components are in focus.
        ////
        addKeyListener(new KeyAdapter(){
          public void keyPressed(KeyEvent e){
            lblMessage.setText(e.getKeyChar() + " key pressed");
          }
        });
      }
    }

    I tried different approaches in hopes of finding a solution but none of my attempts worked out. When attaching the KeyListener to the frame it works up until buttons are added. The issue being is it only captures the key of the component that is in focus. So if btnF1 is in focus it will only capture the F1 keypress. If I swtich focus to btnF2 it won't catch me pressing F1. I am hoping there is a possible way without making it sloppily.

    Thanks in advance for any input you may have.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,045
    My Mood
    Mellow
    Thanks
    184
    Thanked 649 Times in 636 Posts

    Default Re: Keyboard Listener or different approach

    You probably need to use a combination of KeyListener and Key Bindings to get the effect you're after.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Ubiquitous (October 5th, 2013)

  4. #3
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Keyboard Listener or different approach

    Thank you for the solid advice key bindings worked out wonderfully now to learn to use them well and make sure I use them cleanly

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,045
    My Mood
    Mellow
    Thanks
    184
    Thanked 649 Times in 636 Posts

    Default Re: Keyboard Listener or different approach

    Glad that helped. Yes, they are a bit tricky to use but definitely an important tool to have.

Similar Threads

  1. What approach should I use?
    By techflyer in forum Java Theory & Questions
    Replies: 2
    Last Post: June 9th, 2013, 07:59 PM
  2. Best way to approach this problem
    By Myshkin in forum Collections and Generics
    Replies: 1
    Last Post: September 30th, 2012, 09:19 AM
  3. How would I approach this?
    By thatguywhoprograms in forum Java Theory & Questions
    Replies: 5
    Last Post: December 20th, 2011, 09:08 PM
  4. keyboard listener not working
    By DougFane in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2011, 06:06 PM
  5. How would you approach this?
    By Staticity in forum Java Theory & Questions
    Replies: 3
    Last Post: October 10th, 2011, 12:09 AM