1

I am using MetaWidget for Swing. I am able to generate the UI and the BeanBinding is working as well. However, the validations like Mandatory Field and Max Length do not work. I am expecting that the first line in the savePerson() method to throw an exception when I leave the "First Name" field blank. Can you kindly help? Is there a Jar file I am missing? Is there something I am missing in my code?

Main UI Class

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();
                }
            }
        });
    }
}

Entity 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:

The Screen with First name Blanked Out

Console Output:

Person: 
 userID = 1
 firstName = 
 lastName = C V
Pramod CS
  • 45
  • 6

1 Answers1

1

The Swing framework does not support data binding or validation out-of-the-box. Therefore neither does SwingMetawidget.

However you can augment Swing with various third-party frameworks such as BeansBinding, Commons BeansUtils, JGoodies Validator or Commons Validator. And therefore you can apply Metawidget plugins for each of those technologies.

You are already applying the BeansBinding third-party plugin, so that your bindings work.

You need to also apply one of the validator plugins. Metawidget directly supports JGoodies Validator, Commons Validator, oVal and Hibernate Validator. Choose whichever suits your existing architecture. Or you can roll your own WidgetProcessor.

Richard Kennard
  • 1,325
  • 11
  • 20
  • Richard, Thank you for your quick response. But, I am not able to figure out which processor class I need to use to get the validators plugged in. I checked the address book example and the validations are working there, but the only WidgetProcessors added ReflectionBindingProcessor and BeansBindingProcessor. So I am wondering how it is working in the address book example? Can you kindly point me to an example source code? It would be of great help. – Pramod CS Aug 19 '16 at 07:42
  • I found the class for JGoodies. It is 'org.metawidget.swing.widgetprocessor.validator.jgoodies.JGoodiesValidatorProcessor'. While it does only Mandatory Field validation, it is a good start to build upon. Thanks @Richard – Pramod CS Aug 20 '16 at 06:51
  • Great. Just FYI: the SwingAddressBook does not use a validation framework - it just 'rolls it own' simple backend validation. Other implementations are org.metawidget.inspector.commons.validator.CommonsValidatorInspector and org.metawidget.inspector.hibernate.validator.HibernateValidatorInspector. – Richard Kennard Aug 21 '16 at 00:22
  • But JGoodies is probably the best for Swing, as it has its own Swing-specific components that actually display inline. – Richard Kennard Aug 21 '16 at 00:23