Solved

Setting Focus on JTexField ?

Posted on 2004-08-09
7
458 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
tomcat not starting 6 68
how to see all occupied ports on windows 10 laptop 15 76
Notify sent to other threads in Java 9 33
java mysql insert application 14 26
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

840 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