Search code examples
javajtextfieldjcheckbox

How to prevent reading input from text field if text field is disabled


I'm currently doing a form which involves text fields and checkboxes. I already linked the check box to the text field, which if the check box is SELECTED, the text field is enabled; while check box is DESELECTED, text field is disabled.

My problem now is if users selects the check box and type some value into the text field and then deselect the check box again, my program would still read the input from the disabled text field. Is there any way to overcome this issue? Below is my coding:

 private void pnl1submitbtnActionPerformed(java.awt.event.ActionEvent evt) {                                              
    moduleFunc moduleFunc;
    String module = moduletxtfield.getText();
    double exam = Double.parseDouble(examtextfield.getText());
    double ct = Double.parseDouble(cttextfield.getText());
    double quiz = Double.parseDouble(quiztextfield.getText());
    double assign = Double.parseDouble(asstextfield.getText());
    try {
        if (module.trim().isEmpty()) {
            moduleerrorlbl.setText("Field cannot be empty");
        }
        else if (!(exam+ct+quiz+assign == 100)) {
            markerrorlbl.setText("Total marks must be 100");
        }
        else
        {
            markerrorlbl.setText("");
            moduleerrorlbl.setText("");
        }

Thanks.

EDITED:

Thanks guys, I've figured out how it works.

 private void pnl1submitbtnActionPerformed(java.awt.event.ActionEvent evt) {                                              
    moduleFunc moduleFunc;
    String module = moduletxtfield.getText();
    double exam = 0.0, assign = 0.0, quiz = 0.0, ct = 0.0;
   try {
        if (module.trim().isEmpty()) {
            moduleerrorlbl.setText("Field cannot be empty");
        }
        else if (OODJ.moduleRecord.containsKey(module)) {
            moduleerrorlbl.setText("Module already exists!");
        }
        else {
            if (examchkbox.isSelected()) {
            exam = Double.parseDouble(examtextfield.getText());
            }
            if(ctchkbox.isSelected()) {
            ct = Double.parseDouble(cttextfield.getText());    
            }
            if(quizchkbox.isSelected()) {
            quiz = Double.parseDouble(quiztextfield.getText());    
            }
            if(asschkbox.isSelected()) {
            assign = Double.parseDouble(asstextfield.getText());    
            }
            if (!(exam + ct + quiz + assign == 100)) {
            markerrorlbl.setText("Total marks must be 100");
            }
            else {
            // self-coded
            }
            }
        }
    catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex);
    }

Solution

  • if you dont want your TF to be read while its disabled, you can easily check about it.

    EDIT: using your example now:

    private void pnl1submitbtnActionPerformed(java.awt.event.ActionEvent evt {                                         
        moduleFunc moduleFunc;
        String module = moduletxtfield.getText();
        double exam = 0, ct = 0, quiz = 0, assign = 0;
        if(examtextfield.isEnabled())
            exam = Double.parseDouble(examtextfield.getText());
        if(cttextfield.isEnabled())
            ct = Double.parseDouble(cttextfield.getText());
        if(quiztextfield.isEnabled())
            quiz = Double.parseDouble(quiztextfield.getText());
        if(asstextfield.isEnabled())
            assign = Double.parseDouble(asstextfield.getText());
        try {
            if (module.trim().isEmpty()) {
                moduleerrorlbl.setText("Field cannot be empty");
            }
            else if (!(exam+ct+quiz+assign == 100)) {
                markerrorlbl.setText("Total marks must be 100");
            }
            else
            {
                markerrorlbl.setText("");
                moduleerrorlbl.setText("");
            }
        }
        //do whatever you want with your double values.
    }