Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Setting Focus on JTexField ?

Posted on 2004-08-09
7
Medium Priority
?
480 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:simple21
  • 4
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11759295
1)
Use a DocumentListener
2)
textfield.requestFocusInWindow();
textfield.requestFocus();
0
 

Author Comment

by:simple21
ID: 11759521
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);            
      }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 11759540
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.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:simple21
ID: 11759687
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
0
 
LVL 92

Accepted Solution

by:
objects earned 360 total points
ID: 11759717
try this:

          d1.addDocumentListener(new DocumentListener()
          {
                    public void changedUpdate(DocumentEvent e)
                     {
                     if(e.getDocument().getLength()>=5) {
                       f2.requestFocusInWindow();
                     }    
                     }
                     
                     public void insertUpdate(DocumentEvent e)
                     {
                     if(e.getDocument().getLength()>=5) {
                       f2.requestFocusInWindow();
                     }    
                     }
                     public void removeUpdate(DocumentEvent e) {}
           });
0
 

Author Comment

by:simple21
ID: 11759745
That was awesome !!!
0
 
LVL 92

Expert Comment

by:objects
ID: 11760983
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question