Solved

Setting Focus on JTexField ?

Posted on 2004-08-09
7
471 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.
Suggested Courses

624 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