Link to home
Start Free TrialLog in
Avatar of simple21
simple21

asked on

Setting Focus on JTexField ?

Hi Experts;

I'm making a product key dialog, I just have a quick questions ?

1.) How do I limit the text input on the JTextField to 5 keys ?
2.) How do I transfer the next focus to the next JTextField after the limit was reach ?

Thanks, Hoping for examples.
Simple_21
Avatar of Mick Barry
Mick Barry
Flag of Australia image

1)
Use a DocumentListener
2)
textfield.requestFocusInWindow();
textfield.requestFocus();
Avatar of simple21
simple21

ASKER

Can you check if this is correct ? I tried creating one but it gave me an error :

C:\Sample\Sample.java:76: <anonymous Sample$1> is not abstract and does not override abstract method
changedUpdate(javax.swing.event.DocumentEvent) in javax.swing.event.DocumentListener
public void changeUpdate(KeyEvent e)


/*
 * @(#)Sample.java 1.0 04/08/10
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_1\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;

public class Sample extends JFrame {
      
      private Document d1;
      private Document d2;
      private Document d3;
      
      private JLabel l1;
      private JTextField f1;
      private JTextField f2;
      private JTextField f3;
      private JButton b1;
      
      private JPanel p1;
      private JPanel p2;
      private JPanel p3;
      private JPanel p4;
      private JPanel p5;
      
      public Sample() {
            
            super("Sample . . .");
            
            p1 = new JPanel();
            p2 = new JPanel();
            p3 = new JPanel();
            p4 = new JPanel();
            p5 = new JPanel();
            
            l1 = new JLabel("Pls. Enter the 15 digit product numers :");
            
            f1 = new JTextField(4);
            f2 = new JTextField(4);
            f3 = new JTextField(4);
            
            b1 = new JButton("Next");
            b1.setEnabled(false);
            
            d1 = f1.getDocument();
            d2 = f2.getDocument();
            d3 = f3.getDocument();
            
            p1.setLayout(new GridLayout(4,1));
            p1.add(p2); p1.add(p3);
            p1.add(p4); p1.add(p5);
            
            p3.add(l1); p4.add(f1);
            p4.add(f2); p4.add(f3);
            p4.add(b1);
            
            getContentPane().add(p1, BorderLayout.CENTER);
            
            setSize(300, 250);
            setVisible(true);
            
            d1.addDocumentListener(new DocumentListener()
            {
                        public void changeUpdate(KeyEvent e)
                         {
                         if(f1.equals("12345")) {
                         f2.requestFocusInWindow();
                         f2.requestFocus();      
                         }      
                         }
             });

      }

      public static void main(String args[]) {
            
            System.out.println("Starting Sample...");
            
            Sample x = new Sample();
            x.setDefaultCloseOperation(EXIT_ON_CLOSE);            
      }
}
A DocumentListener needs the following 4 methods defined:

void changedUpdate(DocumentEvent e)
          Gives notification that an attribute or set of attributes changed.
 void insertUpdate(DocumentEvent e)
          Gives notification that there was an insert into the document.
 void removeUpdate(DocumentEvent e)
          Gives notification that a portion of the document has been removed.



>    f2.requestFocusInWindow();
>     f2.requestFocus();    

sorry I meant to say use *one* of these :)  preferably the first one.

I tried this but it does not transfer the focus :

            d1.addDocumentListener(new DocumentListener()
            {
                        public void changedUpdate(DocumentEvent e)
                         {
                         if(f1.equals("12345")) {
                         //f2.requestFocusInWindow();
                         f2.requestFocus();      
                         }      
                         }
                         
                         public void insertUpdate(DocumentEvent e)
                         {
                         if(f1.equals("12345")) {
                         //f2.requestFocusInWindow();
                         f2.requestFocus();      
                         }      
                         }
                         public void removeUpdate(DocumentEvent e) {}
             });

How would I get the length and compare it to 5 digit and the transfer ?

Thanks : simple_21
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That was awesome !!!