Java Reference
In-Depth Information
Decision point
How to use the onscreen keypad to insert values into text fields?
The basic idea is to let the user click on the buttons of the keypad to insert
the values into the fields. This is equivalent to touching the keys on the
touch screen that will equip the real counter terminal. This solution has a
practical problem. The insertion of data into a field on a user interface is
based on the concept of the currently active element: the last element
clicked by the user is the active element that will receive the user input.
Both the simulated keypad and the fields are part of the same graphical user
interface and there can be only one active element at a time; thus, the active
element is either on the simulated keypad or on the main user interface. To
solve this problem we have to explicitly keep track of the last active element
(i.e. the one that was active before the current one) and send to it the input
from the keypad.
The main class of the user interface is CounterFrame , it defines the main
elements of the counter user interface. It includes the input fields and the
keypad. In addition this class implements the BarCodeListener interfaces, so
it is able to receive events from the (simulated) bar code reader.
The method input_focusGained() is invoked whenever one of the input
fields gets the focus (i.e. becomes the active element of the GUI); it stores a
reference to the input field in the attribute lastInputField . A click on one of
the buttons of the keypad triggers the execution of method keypad_action
Performed() , which sends a key to the last active input field (i.e.
lastInputField ).
package CounterUI;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CounterFrame extends JFrame
implements BarCodeListener {
private JTextField lastInputField # null ;
void input_focusGained(FocusEvent e) {
lastInputField # (JTextField)e.getComponent();
System.out.println(e.getComponent().getName() !
" got focus");
void keypad_actionPerformed(ActionEvent e) {
String cmd # e.getActionCommand();
Search WWH ::

Custom Search