Results 1 to 3 of 3

Thread: Tab Listener

  1. #1
    teckno101 is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Cool 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);
    		}
    	}
    
    }

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

    Default

    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);
        }
      }

  3. #3
    teckno101 is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Wink Thanks

    Thank you very much. This is exactly what I needed.;)

Similar Threads

  1. J2ME sms listener
    By bharani in forum CLDC and MIDP
    Replies: 4
    Last Post: 11-21-2010, 06:33 PM
  2. J2ME sms listener
    By bharani in forum CLDC and MIDP
    Replies: 4
    Last Post: 10-31-2009, 07:02 AM
  3. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-20-2008, 11:07 PM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 02:00 AM
  5. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM

Tags for this Thread

Posting Permissions

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