Validation Results View Example : Data Validation « Swing Components « Java






Validation Results View Example

Validation Results View Example
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.com/downloads/
*/

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.validation.ValidationResult;
import com.jgoodies.validation.util.DefaultValidationResultModel;
import com.jgoodies.validation.util.ValidationResultModel;
import com.jgoodies.validation.view.ValidationResultViewFactory;

public class ValidationResultsViewExample extends JPanel {
    private ValidationResultModel validationResultModel;

    public ValidationResultsViewExample() {
        PanelBuilder panelBuilder = new PanelBuilder(new FormLayout("right:pref, 3dlu, p:g"));
        panelBuilder.setDefaultDialogBorder();

        this.validationResultModel = new DefaultValidationResultModel();

        CellConstraints cc = new CellConstraints();

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 4));
        panel.add(new JButton(new SetValidationResults("Empty")));
        panel.add(new JButton(new SetValidationResults("Errors")));
        panel.add(new JButton(new SetValidationResults("Warnings")));
        panel.add(new JButton(new SetValidationResults("Mixed")));

        panelBuilder.appendRow("t:30dlu");
        panelBuilder.add(panel, cc.xywh(1, panelBuilder.getRow(), 3, 1));
        panelBuilder.nextLine();

        panelBuilder.appendRow("t:30dlu");
        panelBuilder.addLabel("Icon and Text Label", cc.xy(1, panelBuilder.getRow()));
        panelBuilder.add(ValidationResultViewFactory.createReportIconAndTextLabel(this.validationResultModel), cc.xy(3, panelBuilder.getRow()));
        panelBuilder.nextLine();

        panelBuilder.appendRow("2dlu");
        panelBuilder.nextLine();
        panelBuilder.appendRow("t:30dlu");
        panelBuilder.addLabel("Icon and TextPane", cc.xy(1, panelBuilder.getRow()));
        panelBuilder.add(ValidationResultViewFactory.createReportIconAndTextPane(this.validationResultModel), cc.xy(3, panelBuilder.getRow()));
        panelBuilder.nextLine();

        panelBuilder.appendRow("2dlu");
        panelBuilder.nextLine();
        panelBuilder.appendRow("t:30dlu");
        panelBuilder.addLabel("Icon Label", cc.xy(1, panelBuilder.getRow()));
        panelBuilder.add(ValidationResultViewFactory.createReportIconLabel(this.validationResultModel), cc.xy(3, panelBuilder.getRow()));
        panelBuilder.nextLine();

        panelBuilder.appendRow("2dlu");
        panelBuilder.nextLine();
        panelBuilder.appendRow("t:30dlu");
        panelBuilder.addLabel("Report List", cc.xy(1, panelBuilder.getRow()));
        panelBuilder.add(ValidationResultViewFactory.createReportList(this.validationResultModel), cc.xy(3, panelBuilder.getRow()));
        panelBuilder.nextLine();

        panelBuilder.appendRow("2dlu");
        panelBuilder.nextLine();
        panelBuilder.appendRow("t:30dlu");
        panelBuilder.addLabel("Report List with Color", cc.xy(1, panelBuilder.getRow()));
        panelBuilder.add(ValidationResultViewFactory.createReportList(this.validationResultModel, Color.white), cc.xy(3, panelBuilder.getRow()));
        panelBuilder.nextLine();

        panelBuilder.appendRow("2dlu");
        panelBuilder.nextLine();
        panelBuilder.appendRow("t:30dlu");
        panelBuilder.addLabel("Text Area", cc.xy(1, panelBuilder.getRow()));
        panelBuilder.add(ValidationResultViewFactory.createReportTextArea(this.validationResultModel), cc.xy(3, panelBuilder.getRow()));
        panelBuilder.nextLine();

        panelBuilder.appendRow("2dlu");
        panelBuilder.nextLine();
        panelBuilder.appendRow("t:30dlu");
        panelBuilder.addLabel("Text Area with Color", cc.xy(1, panelBuilder.getRow()));
        panelBuilder.add(ValidationResultViewFactory.createReportTextArea(this.validationResultModel, Color.white), cc.xy(3, panelBuilder.getRow()));
        panelBuilder.nextLine();

        panelBuilder.appendRow("2dlu");
        panelBuilder.nextLine();
        panelBuilder.appendRow("t:30dlu");
        panelBuilder.addLabel("Text Pane", cc.xy(1, panelBuilder.getRow()));
        panelBuilder.add(ValidationResultViewFactory.createReportTextPane(this.validationResultModel), cc.xy(3, panelBuilder.getRow()));
        panelBuilder.nextLine();

        panelBuilder.appendRow("2dlu");
        panelBuilder.nextLine();
        panelBuilder.appendRow("t:30dlu");
        panelBuilder.addLabel("Text Pane with Color", cc.xy(1, panelBuilder.getRow()));
        panelBuilder.add(ValidationResultViewFactory.createReportTextPane(this.validationResultModel, Color.white), cc.xy(3, panelBuilder.getRow()));
        panelBuilder.nextLine();

        add(panelBuilder.getPanel());
    }

    private class SetValidationResults extends AbstractAction {
        public SetValidationResults(String name) {
            super(name);
        }

        public void actionPerformed(ActionEvent e) {
            if ("Errors".equals(this.getValue(Action.NAME))) {
                validationResultModel.setResult(createErrorResult());
            } else if ("Mixed".equals(this.getValue(Action.NAME))) {
                validationResultModel.setResult(createMixedResult());
            } else if ("Warnings".equals(this.getValue(Action.NAME))) {
                validationResultModel.setResult(createWarningResult());
            } else if ("Empty".equals(this.getValue(Action.NAME))) {
                validationResultModel.setResult(createEmptyResult());
            }

        }
    }

    private ValidationResult createEmptyResult() {
        return ValidationResult.EMPTY;
    }

    private ValidationResult createErrorResult() {
        ValidationResult validationResult = new ValidationResult();
        validationResult.addError("Error message one.");
        validationResult.addError("Error message two.");
        validationResult.addError("Error message three.");
        return validationResult;
    }

    private ValidationResult createWarningResult() {
        ValidationResult validationResult = new ValidationResult();
        validationResult.addWarning("Warning message one.");
        validationResult.addWarning("Warning message two.");
        validationResult.addWarning("Warning message three.");
        return validationResult;
    }

    private ValidationResult createMixedResult() {
        ValidationResult validationResult = new ValidationResult();
        validationResult.addError("Error message one.");
        validationResult.addWarning("Warning message one.");
        validationResult.addWarning("Warning message two.");
        validationResult.addError("Error message two.");
        validationResult.addError("Error message three.");
        validationResult.addWarning("Warning message three.");
        return validationResult;
    }

    public static void main(String[] a){
      JFrame f = new JFrame("Validation Results View Example");
      f.setDefaultCloseOperation(2);
      f.add(new ValidationResultsViewExample());
      f.pack();
      f.setVisible(true);
    }
}

           
       








jgoodiesValidation.zip( 277 k)

Related examples in the same category

1.Component Hints ExampleComponent Hints Example
2.Error Dialog ExampleError Dialog Example
3.Field List Validator ExampleField List Validator Example
4.Info Assist ExampleInfo Assist Example
5.Input Verifier Example
6.Validating Domain Object ExampleValidating Domain Object Example
7.Validating On Focus Lost ExampleValidating On Focus Lost Example
8.Validating On Key Typed ExampleValidating On Key Typed Example
9.Validating Presentation Model ExampleValidating Presentation Model Example
10.Validation How to ExampleValidation How to Example
11.Validation Icons ExampleValidation Icons Example
12.Different styles how to indicate that a component contains invalid dataDifferent styles how to indicate that a component
 contains invalid data
13.how to provide input hintshow to provide input hints
14.Different validation result viewsDifferent validation result views
15.different validation times: key-typed, focus lost, commit (OK pressed)different validation times: key-typed, focus lost, commit (OK pressed)
16.Different configurations of JFormattedTextFieldDifferent configurations of JFormattedTextField
17.different configurations of JFormattedTextField: Numberdifferent configurations of JFormattedTextField: Number
18.Different styles to mark mandatory fieldsDifferent styles to mark mandatory fields