37914

How to point out which jTextfield is empty [duplicate]

<div class="question-status question-originals-of-duplicate">

This question already has an answer here:

    <li> How to check which JTextFields are empty and inform the user about them? <span class="question-originals-answer-count"> 2 answers </span> </li> </ul>

    I have this code sample to see which Jtextfield is empty. I have attached a image of my application too. All I need to know is that, when a user was not entering the details in a specific jTextfield, and click "Register" button, I want the user to be informed about his mistake/s like;

    "You haven't entered Student Middle Name" or "You have not entered Student Address" or "You haven't entered Student middle name and Address"

    I want the user to inform SPECIFICALLY which jTextfield/s is/are EMPTY and set its/Their background/s RED and stop saving the details into database until he fills all the JtextFields. I have tried many codes but any of it didn't work :(

    Here's my Code. I have used the array to check the Jtextfield/s are empty or not, but I don't know how to inform the user which Jtextfield/s is/are causing the problem. Please Help Me :(

    public void checkEmpty() { String fname = jTextField1.getText(); String mname = jTextField2.getText(); String lname = jTextField3.getText(); String lineone = jTextField4.getText(); String linetwo = jTextField5.getText(); String linethree = jTextField6.getText(); int fnam = fname.length(); int mnam = mname.length(); int lnam = lname.length(); int lineon = lineone.length(); int linetw = linetwo.length(); int linethre = linethree.length(); int[] check = {fnam, mnam, lnam, lineon, linetw, linethre}; for (int i = 0; i < check.length; i++) { if (check[i] == 0) { //needs to show which jTextfield/s is/are empty and make their backgrounds RED } else { //save to database----> I know what I have to do here. } } }

    Thank you very much :)This is my Application

    Answer1:

    Do something like below:

    public void checkEmpty() { JTextField [] textFields = {jTextField1,jTextField2,jTextField3,jTextField4,jTextField5,jTextField6}; isInputValid = true; for (int i = 0; i < textFields.length; i++) { JTextField jTextField = textFields[i]; String textValue = jTextField.getText().trim(); if (textValue.length() == 0) { //turn background into red jTextField.setBackground(Color.RED); isInputValid = false; } } // now check if input are valid if(!isInputValid) return; //save to database----> I know what I have to do here. }

    Answer2:

    For that you need to add change listener (a DocumentListener which reacts for change in the text) for your JTextField, and within actionPerformed(), you need to update the loginButton to enabled/disabled depending on the whether the JTextfield is empty or not.

    You can add action listener to know does your textfield has changed, or not.

    yourJTextField.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { changed(); } public void removeUpdate(DocumentEvent e) { changed(); } public void insertUpdate(DocumentEvent e) { changed(); } public void changed() { if (yourJTextField.getText().equals("")){ loginButton.setEnabled(false); } else { loginButton.setEnabled(true); } } });

    Then function checkempty would check for flags that could be set in changed(), Example changed() code:

    boolean changedField = false; public static void changed(){ changedField = true; }

    And check does changedField is true.

Recommend

  • JTextFields automatically resizing
  • JFrame & JPanel sizing issue
  • How to insert data into a Microsoft Access Database?
  • XGBOOST - DMATRIX
  • Getting EOFError along with exceptions when using ftplib
  • Android Oreo JobIntentService Keep running in background for Android 7 &below and crashing often
  • Multilingual set up of codeigniter
  • Is there an HTML code that can make my background picture transparent and my text non-transparent?
  • remove unicode characters but keep all special and English characters with preg_replace
  • Criterion causing memory consumption to explode, no CAFs in sight
  • Floated image with variable width and heading with background image
  • TextToSpeech.setEngineByPackageName() triggers NullPointerException
  • Position: fixed nav does not stay fixed
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • How can I speed up CURL tasks?
  • Google Custom Search with transparent background
  • Android fill_parent issue
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Join two tables and save into third-sql
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • Rearranging Cells in UITableView Bug & Saving Changes
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Android Google Maps API OnLocationChanged only called once
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • failed to connect to specific WiFi in android programmatically
  • Converting MP3 duration time
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?