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 8706251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:34:14+00:00 2026-06-13T03:34:14+00:00

In my Swing application, the user must insert numbers and values, before switching to

  • 0

In my Swing application, the user must insert numbers and values, before switching to the next window. Now as a clean program should, I check every input if its valid or not, and if not, an error message is displayed and the next window does not open.

The structure of this check is as following (example):

Button buttonToOpenNextWindow = new JButton("next");
button.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){
        if(checkValidty){
            // (...)
            new WindowA();
            frame.dispose(); // (*)
        }
    }
});

(*) Note: I know that the principle of multiple JFrames is ugly, and I’m on to change that, but for this question it’s irrelevant.

Now the focus of this question is this checkValidity(), which I structured like this:

private boolean checkValidity(){

    // check input 1
    try{
        Integer.parseInt(textField1.getText());
    }catch (NumberFormatException e){
        new ErrorDialog("input 1 is invalid!"); // own implemented dialog
        return false;
    }

    // check input 2
    try{
        Integer.parseInt(textField2.getText());
    }catch (NumberFormatException e){
        new ErrorDialog("input 2 is invalid!"); // own implemented dialog
        return false;
    }

    // (...)

    // check input n
    try{
        Integer.parseInt(textField_n.getText());
    }catch (NumberFormatException e){
        new ErrorDialog("input n is invalid!"); // own implemented dialog
        return false;
    }
    return true;
}

This works exactly as I want, BUT the code itself is very ugly, because having multiple input options the method gets 200, 300 or more lines long (as I do not only check if e.g. it’s a number, but also if the number makes sense in context of the program logic and so on). Is there a Swing -own method to check such things? Or has anyone a better idea how to realize exactly this functionality with split methods?

  • 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-13T03:34:15+00:00Added an answer on June 13, 2026 at 3:34 am

    One solution would be to use Swing’s InputVerifier to validate input for every JTextField used. As the validation functionality is the same for each field, a single instance could be used for all components:

    public class MyNumericVerifier extends InputVerifier {
        @Override
        public boolean verify(JComponent input) {
           String text = ((JTextField) input).getText();
           try {
              Integer.parseInt(text);
           } catch (NumberFormatException e) {
              return false;
           }
    
           return true;
        }
    }
    
    InputVerifier verifier = new MyNumericVerifier()
    textField1.setInputVerifier(verifier);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm updating a Java Swing application to support the user switching the app's font
I have a Swing application that connect directly to MySql Database for user authentication.
I developed one swing application but each time you run application new window is
How to prevent to close Java swing Application, when user clicks on close button?
I'm new to programming,now I'm writing Java Swing application. One of the main tasks
I am trying to insert a Map View in a Java Swing application for
When a user clicks a JButton in my Java-Swing application, a string is returned
How would I go about getting the Windows user credentials from a Swing application?
Our Swing application doesn't limit the number of application instance user opens that is
I have swing application in which there is MS excel like functionality. User can

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.