UI 验证在 Swing MetaWidget 中不起作用
UI Validation not working in Swing MetaWidget
我正在为 Swing 使用 MetaWidget。我能够生成 UI 并且 BeanBinding 也能正常工作。但是,强制字段和最大长度等验证不起作用。当我将 "First Name" 字段留空时,我希望 savePerson() 方法中的第一行抛出异常。
你能帮忙吗?是否缺少一个 Jar 文件?我的代码中是否缺少某些内容?
主要UIClass
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();
}
}
});
}
}
实体Class
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;
}
}
画面:
The Screen with First name Blanked Out
控制台输出:
Person:
userID = 1
firstName =
lastName = C V
Swing 框架不支持开箱即用的数据绑定或验证。因此 SwingMetawidget 也没有。
但是您可以增加 Swing 与各种第三方框架,例如 BeansBinding、Commons BeansUtils、JGoodies Validator 或 Commons Validator。因此,您可以为这些技术中的每一种应用 Metawidget 插件。
您已经在应用 BeansBinding 第三方插件,因此您的绑定有效。
您还需要应用其中一个验证器插件。 Metawidget 直接支持 JGoodies Validator、Commons Validator、oVal 和 Hibernate Validator。选择适合您现有架构的任何一个。或者您可以推出自己的 WidgetProcessor。
我正在为 Swing 使用 MetaWidget。我能够生成 UI 并且 BeanBinding 也能正常工作。但是,强制字段和最大长度等验证不起作用。当我将 "First Name" 字段留空时,我希望 savePerson() 方法中的第一行抛出异常。 你能帮忙吗?是否缺少一个 Jar 文件?我的代码中是否缺少某些内容?
主要UIClass
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();
}
}
});
}
}
实体Class
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;
}
}
画面:
The Screen with First name Blanked Out
控制台输出:
Person:
userID = 1
firstName =
lastName = C V
Swing 框架不支持开箱即用的数据绑定或验证。因此 SwingMetawidget 也没有。
但是您可以增加 Swing 与各种第三方框架,例如 BeansBinding、Commons BeansUtils、JGoodies Validator 或 Commons Validator。因此,您可以为这些技术中的每一种应用 Metawidget 插件。
您已经在应用 BeansBinding 第三方插件,因此您的绑定有效。
您还需要应用其中一个验证器插件。 Metawidget 直接支持 JGoodies Validator、Commons Validator、oVal 和 Hibernate Validator。选择适合您现有架构的任何一个。或者您可以推出自己的 WidgetProcessor。