Java UI验证在Swing MetaWidget中不起作用

Java UI验证在Swing MetaWidget中不起作用,java,swing,metawidget,Java,Swing,Metawidget,我正在使用MetaWidget进行Swing。我能够生成UI,BeanBinding也能正常工作。但是,诸如必填字段和最大长度之类的验证不起作用。我希望savePerson()方法中的第一行在我将“first Name”字段留空时引发异常。 你能帮忙吗?有我丢失的Jar文件吗?我的代码中是否缺少某些内容 主UI类 public class MetaWidgetFrame extends JFrame { private JPanel contentPane; private S

我正在使用MetaWidget进行Swing。我能够生成UI,BeanBinding也能正常工作。但是,诸如必填字段和最大长度之类的验证不起作用。我希望savePerson()方法中的第一行在我将“first Name”字段留空时引发异常。 你能帮忙吗?有我丢失的Jar文件吗?我的代码中是否缺少某些内容

主UI类

public class MetaWidgetFrame extends JFrame {

    private JPanel contentPane;
    private SwingMetawidget metawidget = new SwingMetawidget();


    /**
     * Create the frame.
     */
    public MetaWidgetFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel contentPanel = new JPanel();
        contentPane.add(contentPanel, BorderLayout.CENTER);

        JPanel controlsPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout(0, 0));

        contentPane.add(controlsPanel, BorderLayout.SOUTH);

        JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                savePerson();
            }
        });
        controlsPanel.add(btnSave);

        Person person = new Person();
        person.setUserID(new Integer(1));
        person.setFirstName("Raman");
        person.setLastName("C V");

        CompositeInspectorConfig inspectorConfig = new CompositeInspectorConfig().setInspectors(
                  new JpaInspector()
                , new PropertyTypeInspector()
                );

        BeansBindingProcessor bbp = new BeansBindingProcessor();


        metawidget.setInspector( new CompositeInspector( inspectorConfig ) );
        metawidget.addWidgetProcessor(new ReflectionBindingProcessor());
        metawidget.addWidgetProcessor(bbp);
        metawidget.setToInspect(person);

        contentPanel.add(metawidget, BorderLayout.CENTER);
        pack();
    }

    public void savePerson() {
        metawidget.getWidgetProcessor(BeansBindingProcessor.class).save(metawidget );
        Person personSaved = metawidget.getToInspect();
        System.out.println("" + personSaved);
    }
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MetaWidgetFrame frame = new MetaWidgetFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "person", catalog = "mydb")
public class Person {

    private Integer userID;
    private String firstName;
    private String lastName;

    public Person() {
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "userID", unique = true, nullable = false)
    public Integer getUserID() {
        return userID;
    }

    @Column(name = "firstName", nullable = false, length = 10)
    public String getFirstName() {
        return firstName;
    }

    @Column(name = "lastName", nullable = false, length = 45)
    public String getLastName() {
        return lastName;
    }

    public void setUserID(Integer userID) {
        this.userID = userID;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "Person: "
                + "\n userID = " + userID  
                + "\n firstName = " + firstName  
                + "\n lastName = " + lastName;
    }
}
实体类

public class MetaWidgetFrame extends JFrame {

    private JPanel contentPane;
    private SwingMetawidget metawidget = new SwingMetawidget();


    /**
     * Create the frame.
     */
    public MetaWidgetFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel contentPanel = new JPanel();
        contentPane.add(contentPanel, BorderLayout.CENTER);

        JPanel controlsPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout(0, 0));

        contentPane.add(controlsPanel, BorderLayout.SOUTH);

        JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                savePerson();
            }
        });
        controlsPanel.add(btnSave);

        Person person = new Person();
        person.setUserID(new Integer(1));
        person.setFirstName("Raman");
        person.setLastName("C V");

        CompositeInspectorConfig inspectorConfig = new CompositeInspectorConfig().setInspectors(
                  new JpaInspector()
                , new PropertyTypeInspector()
                );

        BeansBindingProcessor bbp = new BeansBindingProcessor();


        metawidget.setInspector( new CompositeInspector( inspectorConfig ) );
        metawidget.addWidgetProcessor(new ReflectionBindingProcessor());
        metawidget.addWidgetProcessor(bbp);
        metawidget.setToInspect(person);

        contentPanel.add(metawidget, BorderLayout.CENTER);
        pack();
    }

    public void savePerson() {
        metawidget.getWidgetProcessor(BeansBindingProcessor.class).save(metawidget );
        Person personSaved = metawidget.getToInspect();
        System.out.println("" + personSaved);
    }
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MetaWidgetFrame frame = new MetaWidgetFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "person", catalog = "mydb")
public class Person {

    private Integer userID;
    private String firstName;
    private String lastName;

    public Person() {
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "userID", unique = true, nullable = false)
    public Integer getUserID() {
        return userID;
    }

    @Column(name = "firstName", nullable = false, length = 10)
    public String getFirstName() {
        return firstName;
    }

    @Column(name = "lastName", nullable = false, length = 45)
    public String getLastName() {
        return lastName;
    }

    public void setUserID(Integer userID) {
        this.userID = userID;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "Person: "
                + "\n userID = " + userID  
                + "\n firstName = " + firstName  
                + "\n lastName = " + lastName;
    }
}
屏幕:

Person: 
 userID = 1
 firstName = 
 lastName = C V

控制台输出:

Person: 
 userID = 1
 firstName = 
 lastName = C V

Swing框架不支持开箱即用的数据绑定或验证。因此SwingMetawidget也没有

但是,您可以使用各种第三方框架来增强Swing,如BeansBinding、Commons BeansUtils、JGoodies验证器或Commons验证器。因此,您可以为每种技术应用Metawidget插件

您已经在应用BeansBinding第三方插件,以便绑定工作


您还需要应用一个验证器插件。Metawidget直接支持JGoodies验证器、Commons验证器、oVal和Hibernate验证器。选择适合您现有体系结构的。或者您可以使用自己的WidgetProcessor。

Richard,感谢您的快速响应。但是,我无法确定需要使用哪个处理器类来插入验证器。我检查了地址簿示例,验证正在那里进行,但唯一的WidgetProcessors添加了ReflectionBindingProcessor和BeansBindingProcessor。所以我想知道它在地址簿示例中是如何工作的?你能给我举个源代码的例子吗?那会很有帮助的。我为JGoodies找了一门课。它是“org.metawidget.swing.widgetprocessor.validator.jgoodies.JGoodiesValidatorProcessor”。虽然它只进行强制性的字段验证,但这是一个良好的开端。谢谢@RichardGreat。仅供参考:SwingAddressBook没有使用验证框架——它只是“滚动自己的”简单后端验证。其他实现有org.metawidget.inspector.commons.validator.commons validator inspector和org.metawidget.inspector.hibernate.validator.hibernateValidator.HibernateValidatorInspector。但JGoodies可能最适合Swing,因为它有自己的Swing特定组件,实际上以内联方式显示。