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.