Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8414335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:08:34+00:00 2026-06-10T01:08:34+00:00

I have 2 JTextFields: JTextField txtJobType, txtPriorityCode; This is the functionality I need: When

  • 0

I have 2 JTextFields:

JTextField txtJobType, txtPriorityCode;

This is the functionality I need:

When user types in ‘administration’ in txtJobType and hits tab (or clicks away) an error check is done to see whether the field is empty or if the text entered exists in database. The way I have done that is:

private void txtJobTypeFocusLost(java.awt.event.FocusEvent evt) {                                     
    System.out.println("JobType Focus Lost");
    if (!checkFieldExists(txtJobType.getText(), "jobType", "jobCode",
            JobType.class) || txtJobType.getText().isEmpty()) {
        txtJobType.requestFocusInWindow();
        txtJobType.selectAll();
    } else {
    }
} 

So if the field doesn’t exist or the text is empty, then return focus to the txtJobType and highlight all the text (if any)

That works without a problem. However, I have the txtPriorityCode field which needs to have the exact same behaviour. So I did:

private void txtPriorityCodeFocusLost(java.awt.event.FocusEvent evt) {                                          
    System.out.println("PriorityCode Focus Lost");
    if (!checkFieldExists(txtPriorityCode.getText(), "priority", "priorityCode",
            Priority.class) || txtPriorityCode.getText().isEmpty()) {
        txtPriorityCode.requestFocusInWindow();
        txtPriorityCode.selectAll();
    }
}

This is where the problem starts: if user leaves jobType and tabs to Priority then the code attempts to return focus back to jobtype, but because priority is also blank at that point, it will attempt to get focus back from jobtype, resulting in this output:

PriorityCode Focus Lost
JobType Focus Lost
PriorityCode Focus Lost
JobType Focus Lost

Any help on how I could implement this behaviour is appreciated, as I have to do this for at least 10 other textfields.

Thank You!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:08:35+00:00Added an answer on June 10, 2026 at 1:08 am

    You shouldn’t be fiddling with focus lost or other low-level constructs. Instead, why not simply use an InputVerifier as per this example and this one too?

    For example, it could look something like this:

    import javax.swing.*;
    
    public class InputVerifierEg {
       private JPanel mainPanel = new JPanel();
       private JTextField txtJobType = new JTextField(10);
       private JTextField txtPriorityCode = new JTextField(10);
    
       public InputVerifierEg() {
          txtJobType.setInputVerifier(new MyInputVerifier("jobType", "jobCode",
                JobType.class));
          txtPriorityCode.setInputVerifier(new MyInputVerifier("priority", "priorityCode",
                Priority.class));
    
          mainPanel.add(new JLabel("Job Type:"));
          mainPanel.add(txtJobType);
          mainPanel.add(Box.createHorizontalStrut(15));
          mainPanel.add(new JLabel("Priority Code:"));
          mainPanel.add(txtPriorityCode);
    
       }
    
       public JPanel getMainPanel() {
          return mainPanel;
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("InputVerifierEg");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new InputVerifierEg().getMainPanel());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class MyInputVerifier extends InputVerifier {
       private String fieldName;
       private String codeName;
       private Class<?> classType;
    
       public MyInputVerifier(String fieldName, String codeName, Class<?> classType) {
          this.fieldName = fieldName;
          this.codeName = codeName;
          this.classType = classType;
       }
    
       @Override
       public boolean verify(JComponent input) {
          JTextField tField = (JTextField) input;
    
          // assuming that the checkFieldExists is a static method of a utility class
          if (!FieldCheckerUtil.checkFieldExists(tField.getText(), fieldName,
                codeName, classType)) {
             return false;
          }
    
          if (tField.getText().trim().isEmpty()) {
             return false;
          }
          return true;
       }
    
       @Override
       public boolean shouldYieldFocus(JComponent input) {
          JTextField tField = (JTextField) input;
    
          if (verify(input)) {
             return true;
          } else {
             tField.selectAll();
             // show JOptionPane error message?
             return false;
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Java application which adds JTextFields @ runtime to JPanel. Basically user clicks
I have 2 JtextFields called qty and amount. When the user types in qty,
I have a JPanel with two JTextFields. If the user writes some text into
I've a text field which extends javax.swing.JTextField and also have document listener like this
I have a JTextField and need to have 3 lines in it, such as
I have a JPanel full of JTextFields... for (int i=0; i<maxPoints; i++) { JTextField
I have a JTextField . And when the user enters a or j ,
quick question: I have a JTextField for user input, in my focus listener, when
This one's a tough one - I have a JFrame that generates JTextFields. When
I have a table like this. The second column uses a JTextField renderer and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.