Solved

Setting Focus on JTexField ?

Posted on 2004-08-09
7
427 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 90 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now