Swing Components Java

/*
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)