Results 1 to 3 of 3
Thread: Tab Listener
- 09-04-2009, 04:52 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
Tab Listener
Hi. I am new to this forum and I hope I am posting this in the right section.
I want to attach a listener to a JTextField that when pressing the tab button on the keyboard it would activate the listener. This way you could enter data faster without having to hit enter every time to activate the listener.
Any help on how to do this?
Java Code:/** * GUI to check out beach taggers * Max Matthews * v 1.0 * 2009 */ import javax.swing.*; //JButton import java.awt.*; //Container and Layouts import java.awt.event.*; //Listeners import java.text.NumberFormat; //CurrencyFormatter public class CheckOut extends JFrame { String cityName = "Ocean City"; JTextField nameTextField = new JTextField("Enter Employee Name Here"); TextReader text = new TextReader(); String employeeNameInBox = "No Name"; JTextField dailyDist = new JTextField("50"); JTextField dailySold = new JTextField("0"); JTextField dailyRet = new JTextField("50"); JTextField weeklyDist = new JTextField("25"); JTextField weeklySold = new JTextField("0"); JTextField weeklyRet = new JTextField("25"); //String[][] database public CheckOut() { setTitle("Check Out " + cityName); //sets the title of window to Bank Teller setSize(300,200); //Sets the size of the window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Makes it so the "X" button works setLocationRelativeTo(null);//creates GUI in center of screen Container contentPane = getContentPane(); //new ContentPane contentPane.setLayout(new BorderLayout()); JPanel contentNorth = new JPanel(); JPanel contentCenter = new JPanel(); JPanel contentSouth = new JPanel(); contentCenter.setLayout(new GridLayout (2,3,1,1)); contentSouth.setLayout(new GridLayout (2,3,1,1)); contentNorth.setLayout(new BorderLayout()); contentNorth.add(nameTextField); contentCenter.add(new JLabel("D. Distributed")); contentCenter.add(new JLabel("D. Returned")); contentCenter.add(new JLabel("D. Sold")); contentCenter.add(dailyDist); contentCenter.add(dailyRet); contentCenter.add(dailySold); contentSouth.add(new JLabel("W. Distributed")); contentSouth.add(new JLabel("W. Returned")); contentSouth.add(new JLabel("W. Sold")); contentSouth.add(weeklyDist); contentSouth.add(weeklyRet); contentSouth.add(weeklySold); contentPane.add(contentNorth, BorderLayout.NORTH); contentPane.add(contentCenter, BorderLayout.CENTER); contentPane.add(contentSouth, BorderLayout.SOUTH); nameTextField.addActionListener(new EmployeeFieldListener()); } public static void main (String[] args) { CheckOut window = new CheckOut(); //makes the window window.setVisible(true); //makes the window visible } private class EmployeeFieldListener implements ActionListener { public void actionPerformed(ActionEvent e) { employeeNameInBox = nameTextField.getText(); if (employeeNameInBox.equals("Enter Employee Name Here")) employeeNameInBox = "No Name Entered"; System.out.println(employeeNameInBox); } } }
-
You want to use a focus listener here. e.g.,
Java Code:public class CheckOut extends JFrame { public static final String ENTER_EMPLOYEE_NAME_HERE = "Enter Employee Name Here"; String cityName = "Ocean City"; JTextField nameTextField = new JTextField(ENTER_EMPLOYEE_NAME_HERE); //.... code deleted public CheckOut() { // .... code deleted nameTextField.addFocusListener(new EmployeeFocusListener()); } private class EmployeeFocusListener extends FocusAdapter { @Override public void focusLost(FocusEvent arg0) { employeeNameInBox = nameTextField.getText(); if (employeeNameInBox.equals(ENTER_EMPLOYEE_NAME_HERE) || employeeNameInBox.isEmpty()) employeeNameInBox = "No Name Entered"; JOptionPane.showMessageDialog(nameTextField, "No Name Entered", "Name Field Error", JOptionPane.ERROR_MESSAGE); System.out.println(employeeNameInBox); } }
- 09-29-2009, 09:40 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
J2ME sms listener
By bharani in forum CLDC and MIDPReplies: 4Last Post: 11-21-2010, 06:33 PM -
J2ME sms listener
By bharani in forum CLDC and MIDPReplies: 4Last Post: 10-31-2009, 07:02 AM -
Regarding Listener
By adeeb in forum AWT / SwingReplies: 2Last Post: 06-20-2008, 11:07 PM -
Regarding Listener
By adeeb in forum AWT / SwingReplies: 2Last Post: 06-10-2008, 02:00 AM -
Listener for SWT event
By Java Tip in forum Java TipReplies: 0Last Post: 01-08-2008, 09:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks