Class FormCreator

  • All Implemented Interfaces:
    com.jformdesigner.runtime.BeanProvider, com.jformdesigner.runtime.ComponentProvider

    public class FormCreator
    extends Object
    implements com.jformdesigner.runtime.ComponentProvider, com.jformdesigner.runtime.BeanProvider
    Creates instances of Swing components from a form model and provides methods to access components. Use FormLoader to load a form model from a JFormDesigner .jfd file into memory.

    First invoke one of the create methods. Then you can use the getter methods to access the created Swing components. Use the standard Swing methods to add listeners, set values or get values.

    Example:

     // load the .jfd file into memory
     FormModel formModel = FormLoader.load( "com/jformdesigner/examples/LoaderExample.jfd" );
    
     // create a dialog
     FormCreator formCreator = new FormCreator( formModel );
     JDialog dialog = formCreator.createDialog( null );
    
     // get references to components
     JTextField nameField = formCreator.getTextField( "nameField" );
     JCheckBox checkBox = formCreator.getCheckBox( "checkBox" );
     JButton okButton = formCreator.getButton( "okButton" );
    
     // set values and add listeners
     nameField.setText( "enter name here" );
     okButton.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                    // add your code...
            }
     } );
    
     // show dialog
     dialog.setModal( true );
     dialog.pack();
     dialog.show();
    
     System.out.println( nameField.getText() );
     System.out.println( checkBox.isSelected() );
     

    If you've assigned event handlers in the form, use setTarget(Object) to set a target for the events.

    • Constructor Detail

      • FormCreator

        public FormCreator​(FormModel model)
        Creates a new FormCreator for the specified form model.
        Parameters:
        model - The form model, from which the components should be created.
      • FormCreator

        public FormCreator​(FormModel model,
                           ClassLoader classLoader)
        Creates a new FormCreator for the specified form model using the specified class loader.
        Parameters:
        model - The form model, from which the components should be created.
        classLoader - The class loader used to load components.
      • FormCreator

        public FormCreator​(FormModel model,
                           Locale locale,
                           ClassLoader classLoader)
        Creates a new FormCreator for the specified form model using the specified locale and class loader.
        Parameters:
        model - The form model, from which the components should be created.
        locale - The locale used to load resource bundles.
        classLoader - The class loader used to load components.
        Since:
        2.0
    • Method Detail

      • getModel

        public FormModel getModel()
        Returns the form model.
        Since:
        3.0
      • getLocale

        public Locale getLocale()
        Returns the locale used to load resource bundles.
        Since:
        3.0
      • getLoader

        public ClassLoader getLoader()
        Returns the class loader used to load components.
        Since:
        3.0
      • getTarget

        public Object getTarget()
        Returns the object that handles events.
        Since:
        2.0
      • setTarget

        public void setTarget​(Object target)
        Set the object that will handle events.

        This object must have handler methods as specified in the form. E.g. if you've specified an actionPerformed() event for a JButton and set the handler method to "myButtonPressed", the target object must implement following method:

         private void myButtonPressed() {
             // handler code
         }
         
        If you've set the "Pass parameters" flag to true for the event, you can get the listener parameters:
         private void myButtonPressed(ActionEvent e) {
             // handler code
         }
         
        Since:
        2.0
      • isSetComponentNames

        public boolean isSetComponentNames()
        Returns whether Component.setName(String) will be invoked on all components of the form. Default is false.
        Since:
        2.0.1
      • setSetComponentNames

        public void setSetComponentNames​(boolean setComponentNames)
        Sets whether Component.setName(String) will be invoked on all components of the form.
        Since:
        2.0.1
      • createAll

        public void createAll()
                       throws Exception
        Creates all components. Use one of the getter methods to access beans.
        Throws:
        Exception
        Since:
        2.0
      • create

        public Component create()
                         throws Exception
        Creates the component hierarchy for the first top level form component.
        Returns:
        The created component hierarchy.
        Throws:
        Exception
      • createPanel

        public JPanel createPanel()
                           throws Exception
        Creates the component hierarchy for the first top level form component, which must be an instance of JPanel. Convenience method that invokes create() and casts the result to JPanel.
        Returns:
        The created panel.
        Throws:
        Exception
      • createDialog

        public JDialog createDialog​(Window owner)
                             throws Exception
        Creates the component hierarchy for the first top level form component, which must be an instance of JDialog. Convenience method that invokes createWindow(Window) and casts the result to JDialog.
        Parameters:
        owner - The window to act as owner; or null.
        Returns:
        The created dialog.
        Throws:
        Exception
      • createWindow

        public Window createWindow​(Window owner)
                            throws Exception
        Creates the component hierarchy for the first top level form component, which must be an instance of Window.
        Parameters:
        owner - The window to act as owner; or null.
        Returns:
        The created window.
        Throws:
        Exception
      • create

        public Component create​(String name)
                         throws Exception
        Creates the component hierarchy for the specified name.
        Parameters:
        name - The name of the form component.
        Returns:
        The created component hierarchy.
        Throws:
        Exception
      • create

        public Component create​(String name,
                                Window owner)
                         throws Exception
        Creates the component hierarchy for the specified name and passes the specified owner window to the constructor, if the component is an instance of Window.
        Parameters:
        name - The name of the form component.
        owner - The window to act as owner; or null.
        Returns:
        The created component hierarchy.
        Throws:
        Exception
      • getComponent

        public Component getComponent​(String name)
                               throws NoSuchComponentException
        Returns the component for the specified name.
        Specified by:
        getComponent in interface com.jformdesigner.runtime.ComponentProvider
        Parameters:
        name - The name of the component.
        Returns:
        The component.
        Throws:
        NoSuchComponentException - If a component with the specified name does not exist or the component is not created.
        ClassCastException - If the name specifies a non-visual bean.
      • getBean

        public Object getBean​(String name)
                       throws NoSuchComponentException
        Returns the bean for the specified name. This can be a visual or a non-visual bean.
        Parameters:
        name - The name of the bean.
        Returns:
        The bean.
        Throws:
        NoSuchComponentException - If a bean with the specified name does not exist or the bean is not created.
        Since:
        2.0
      • getBean

        public Object getBean​(String name,
                              boolean create)
                       throws Exception
        Returns the bean for the specified name. This can be a visual or a non-visual bean.
        Specified by:
        getBean in interface com.jformdesigner.runtime.BeanProvider
        Parameters:
        name - The name of the bean.
        create - If true, create the bean if necessary.
        Returns:
        The bean.
        Throws:
        Exception
        Since:
        5.0
      • newComponentInstance

        protected Object newComponentInstance​(Class<?> beanClass,
                                              String compName)
                                       throws Exception
        Creates a new instance of the given bean class. The default implementation is "return beanClass.newInstance();".

        Override this method to implement special component creation.

        Parameters:
        beanClass - The component class.
        compName - The name of the component.
        Returns:
        A new instance of the component.
        Throws:
        Exception
        Since:
        3.0
      • getI18nBundle

        protected ResourceBundle getI18nBundle​(String baseName,
                                               Locale locale,
                                               ClassLoader loader)
        Gets the resource bundle for the given base name. The default implementation is "return ResourceBundle.getBundle(baseName, locale, loader);".

        Override this method to implement special bundle loading.

        Parameters:
        baseName - The base name of the resource bundle, a fully qualified class name.
        locale - The locale for which a resource bundle is desired.
        loader - The class loader from which to load the resource bundle.
        Returns:
        The resource bundle for the given base name and locale.
        Since:
        3.0
      • getI18nString

        protected String getI18nString​(ResourceBundle bundle,
                                       String baseName,
                                       String key)
        Gets a string for the given key from the given resource bundle or one of its parents. The default implementation is "return bundle.getString(key);".

        Override this method to implement special string loading.

        Parameters:
        bundle - The resource bundle to get the string from.
        baseName - The base name of the resource bundle, a fully qualified class name.
        key - The key for the desired string.
        Returns:
        The string for the given key.
        Since:
        3.0