Package com.jformdesigner.runtime
Class FormCreator
- java.lang.Object
-
- com.jformdesigner.runtime.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. UseFormLoaderto load a form model from a JFormDesigner .jfd file into memory.First invoke one of the
createmethods. 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 Summary
Constructors Constructor Description FormCreator(FormModel model)Creates a newFormCreatorfor the specified form model.FormCreator(FormModel model, ClassLoader classLoader)Creates a newFormCreatorfor the specified form model using the specified class loader.FormCreator(FormModel model, Locale locale, ClassLoader classLoader)Creates a newFormCreatorfor the specified form model using the specified locale and class loader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Componentcreate()Creates the component hierarchy for the first top level form component.Componentcreate(String name)Creates the component hierarchy for the specified name.Componentcreate(String name, Window owner)Creates the component hierarchy for the specified name and passes the specified owner window to the constructor, if the component is an instance ofWindow.voidcreateAll()Creates all components.Object[]createBindings()Creates the bindings.JDialogcreateDialog(Window owner)Creates the component hierarchy for the first top level form component, which must be an instance ofJDialog.JPanelcreatePanel()Creates the component hierarchy for the first top level form component, which must be an instance ofJPanel.WindowcreateWindow(Window owner)Creates the component hierarchy for the first top level form component, which must be an instance ofWindow.ObjectgetBean(String name)Returns the bean for the specified name.ObjectgetBean(String name, boolean create)Returns the bean for the specified name.JButtongetButton(String name)Returns the component for the specified name.JCheckBoxgetCheckBox(String name)Returns the component for the specified name.JCheckBoxMenuItemgetCheckBoxMenuItem(String name)Returns the component for the specified name.<E> JComboBox<E>getComboBox(String name)Returns the component for the specified name.ComponentgetComponent(String name)Returns the component for the specified name.JDialoggetDialog(String name)Returns the component for the specified name.JEditorPanegetEditorPane(String name)Returns the component for the specified name.JFormattedTextFieldgetFormattedTextField(String name)Returns the component for the specified name.JFramegetFrame(String name)Returns the component for the specified name.protected ResourceBundlegetI18nBundle(String baseName, Locale locale, ClassLoader loader)Gets the resource bundle for the given base name.protected StringgetI18nString(ResourceBundle bundle, String baseName, String key)Gets a string for the given key from the given resource bundle or one of its parents.JLabelgetLabel(String name)Returns the component for the specified name.<E> JList<E>getList(String name)Returns the component for the specified name.ClassLoadergetLoader()Returns the class loader used to load components.LocalegetLocale()Returns the locale used to load resource bundles.JMenugetMenu(String name)Returns the component for the specified name.JMenuBargetMenuBar(String name)Returns the component for the specified name.JMenuItemgetMenuItem(String name)Returns the component for the specified name.FormModelgetModel()Returns the form model.JPanelgetPanel(String name)Returns the component for the specified name.JPasswordFieldgetPasswordField(String name)Returns the component for the specified name.JPopupMenugetPopupMenu(String name)Returns the component for the specified name.JProgressBargetProgressBar(String name)Returns the component for the specified name.JRadioButtongetRadioButton(String name)Returns the component for the specified name.JRadioButtonMenuItemgetRadioButtonMenuItem(String name)Returns the component for the specified name.JScrollBargetScrollBar(String name)Returns the component for the specified name.JScrollPanegetScrollPane(String name)Returns the component for the specified name.JSeparatorgetSeparator(String name)Returns the component for the specified name.JSlidergetSlider(String name)Returns the component for the specified name.JSpinnergetSpinner(String name)Returns the component for the specified name.JSplitPanegetSplitPane(String name)Returns the component for the specified name.JTabbedPanegetTabbedPane(String name)Returns the component for the specified name.JTablegetTable(String name)Returns the component for the specified name.ObjectgetTarget()Returns the object that handles events.JTextAreagetTextArea(String name)Returns the component for the specified name.JTextFieldgetTextField(String name)Returns the component for the specified name.JTextPanegetTextPane(String name)Returns the component for the specified name.JToggleButtongetToggleButton(String name)Returns the component for the specified name.JToolBargetToolBar(String name)Returns the component for the specified name.JTreegetTree(String name)Returns the component for the specified name.JWindowgetWindow(String name)Returns the component for the specified name.booleanisSetComponentNames()Returns whetherComponent.setName(String)will be invoked on all components of the form.protected ObjectnewComponentInstance(Class<?> beanClass, String compName)Creates a new instance of the given bean class.voidsetSetComponentNames(boolean setComponentNames)Sets whetherComponent.setName(String)will be invoked on all components of the form.voidsetTarget(Object target)Set the object that will handle events.
-
-
-
Constructor Detail
-
FormCreator
public FormCreator(FormModel model)
Creates a newFormCreatorfor 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 newFormCreatorfor 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 newFormCreatorfor 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 whetherComponent.setName(String)will be invoked on all components of the form. Default isfalse.- Since:
- 2.0.1
-
setSetComponentNames
public void setSetComponentNames(boolean setComponentNames)
Sets whetherComponent.setName(String)will be invoked on all components of the form.- Since:
- 2.0.1
-
createAll
public void createAll() throws ExceptionCreates 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 ofJPanel. Convenience method that invokescreate()and casts the result toJPanel.- 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 ofJDialog. Convenience method that invokescreateWindow(Window)and casts the result toJDialog.- Parameters:
owner- The window to act as owner; ornull.- 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 ofWindow.- Parameters:
owner- The window to act as owner; ornull.- 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 ofWindow.- Parameters:
name- The name of the form component.owner- The window to act as owner; ornull.- Returns:
- The created component hierarchy.
- Throws:
Exception
-
createBindings
public Object[] createBindings() throws Exception
Creates the bindings.- Throws:
Exception- Since:
- 5.0
-
getComponent
public Component getComponent(String name) throws NoSuchComponentException
Returns the component for the specified name.- Specified by:
getComponentin interfacecom.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:
getBeanin interfacecom.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
-
getButton
public JButton getButton(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJButton.
-
getCheckBox
public JCheckBox getCheckBox(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJCheckBox.
-
getCheckBoxMenuItem
public JCheckBoxMenuItem getCheckBoxMenuItem(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJCheckBoxMenuItem.- Since:
- 2.0.5
-
getComboBox
public <E> JComboBox<E> getComboBox(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJComboBox.
-
getDialog
public JDialog getDialog(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJDialog.
-
getEditorPane
public JEditorPane getEditorPane(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJEditorPane.
-
getFormattedTextField
public JFormattedTextField getFormattedTextField(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJFormattedTextField.
-
getFrame
public JFrame getFrame(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJFrame.
-
getLabel
public JLabel getLabel(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJLabel.
-
getList
public <E> JList<E> getList(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJList.
-
getMenu
public JMenu getMenu(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJMenu.- Since:
- 2.0.5
-
getMenuBar
public JMenuBar getMenuBar(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJMenuBar.- Since:
- 2.0.5
-
getMenuItem
public JMenuItem getMenuItem(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJMenuItem.- Since:
- 2.0.5
-
getPanel
public JPanel getPanel(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJPanel.
-
getPasswordField
public JPasswordField getPasswordField(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJPasswordField.
-
getPopupMenu
public JPopupMenu getPopupMenu(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJPopupMenu.- Since:
- 2.0.5
-
getProgressBar
public JProgressBar getProgressBar(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJProgressBar.
-
getRadioButton
public JRadioButton getRadioButton(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJRadioButton.
-
getRadioButtonMenuItem
public JRadioButtonMenuItem getRadioButtonMenuItem(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJRadioButtonMenuItem.- Since:
- 2.0.5
-
getScrollBar
public JScrollBar getScrollBar(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJScrollBar.
-
getScrollPane
public JScrollPane getScrollPane(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJScrollPane.
-
getSeparator
public JSeparator getSeparator(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJSeparator.
-
getSlider
public JSlider getSlider(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJSlider.
-
getSpinner
public JSpinner getSpinner(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJSpinner.
-
getSplitPane
public JSplitPane getSplitPane(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJSplitPane.
-
getTabbedPane
public JTabbedPane getTabbedPane(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJTabbedPane.
-
getTable
public JTable getTable(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJTable.
-
getTextArea
public JTextArea getTextArea(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJTextArea.
-
getTextField
public JTextField getTextField(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJTextField.
-
getTextPane
public JTextPane getTextPane(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJTextPane.
-
getToggleButton
public JToggleButton getToggleButton(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJToggleButton.
-
getToolBar
public JToolBar getToolBar(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJToolBar.
-
getTree
public JTree getTree(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJTree.
-
getWindow
public JWindow getWindow(String name) throws NoSuchComponentException
Returns the component for the specified name.- 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 component is not a instance ofJWindow.
-
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
-
-