2016-08-18 5 views
1

私はSwingにMetaWidgetを使用しています。私はUIを生成することができ、BeanBindingも同様に機能しています。ただし、必須フィールドや最大長などの検証は機能しません。私は、 "First Name"フィールドを空白のままにしておくと、savePerson()メソッドの最初の行が例外をスローすることを期待しています。 親切にお手伝いできますか? Jarファイルがありますか?私のコードに欠けているものはありますか?Swing MetaWidgetでUIの検証が機能しない

メイン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; 
    } 
} 

画面:

The Screen with First name Blanked Out

コンソール出力:

Person: 
userID = 1 
firstName = 
lastName = C V 

答えて

0

Swingのフレームワークは、アウト・オブ・ボックスデータバインディングや検証をサポートしていません。したがって、どちらもSwingMetawidgetを行いません。あなたはなBeansBinding、コモンズBeansUtils、なJGoodiesバリやコモンズの検証など、さまざまなサードパーティ製のフレームワークとスイングを強化することができますしかし

。したがって、これらのテクノロジーごとにMetawidgetプラグインを適用することができます。

既にBeansBindingサードパーティ製プラグインを適用しているので、バインディングが機能します。

また、バリデータプラグインの1つを適用する必要があります。 Metawidgetは、JGoodies Validator、Commons Validator、oVal、およびHibernate Validatorを直接サポートします。既存のアーキテクチャに合ったものを選択してください。または、独自のWidgetProcessorをロールすることもできます。

+0

リチャード、あなたの迅速な対応に感謝します。しかし、私はバリデーターを接続するためにどのプロセッサークラスを使う必要があるのか​​分かりません。私はアドレス帳の例をチェックして検証していますが、唯一のWidgetProcessorsはReflectionBindingProcessorとBeansBindingProcessorを追加しました。だから私はそれがアドレス帳の例でどのように働いているのだろうか?あなたは親切に私のソースコードの例を教えていただけますか?それは大きな助けになるでしょう。 –

+0

JGoodiesのクラスが見つかりました。これは 'org.metawidget.swing.widgetprocessor.validator.jgoodies.JGoodiesValidatorProcessor'です。 Mandatory Fieldの検証だけですが、それは上に構築するための良いスタートです。ありがとう@リチャード –

+0

素晴らしい。ちょうどFYI:SwingAddressBookはバリデーションフレームワークを使用していません。単純なバックエンドの検証を行うだけです。その他の実装はorg.metawidget.inspector.commons.validator.CommonsValidatorInspectorとorg.metawidget.inspector.hibernate.validator.HibernateValidatorInspectorです。 –

関連する問題