public class FormCreator extends Object implements com.jformdesigner.runtime.ComponentProvider, com.jformdesigner.runtime.BeanProvider
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 and Description |
---|
FormCreator(FormModel model)
Creates a new
FormCreator for the specified form model. |
FormCreator(FormModel model,
ClassLoader classLoader)
Creates a new
FormCreator for the specified form model
using the specified class loader. |
FormCreator(FormModel model,
Locale locale,
ClassLoader classLoader)
Creates a new
FormCreator for the specified form model
using the specified locale and class loader. |
Modifier and Type | Method and Description |
---|---|
Component |
create()
Creates the component hierarchy for the first top level form component.
|
Component |
create(String name)
Creates the component hierarchy for the specified name.
|
Component |
create(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 of
Window . |
void |
createAll()
Creates all components.
|
Object[] |
createBindings()
Creates the bindings.
|
JDialog |
createDialog(Window owner)
Creates the component hierarchy for the first top level form component,
which must be an instance of
JDialog . |
JPanel |
createPanel()
Creates the component hierarchy for the first top level form component,
which must be an instance of
JPanel . |
Window |
createWindow(Window owner)
Creates the component hierarchy for the first top level form component,
which must be an instance of
Window . |
Object |
getBean(String name)
Returns the bean for the specified name.
|
Object |
getBean(String name,
boolean create)
Returns the bean for the specified name.
|
JButton |
getButton(String name)
Returns the component for the specified name.
|
JCheckBox |
getCheckBox(String name)
Returns the component for the specified name.
|
JCheckBoxMenuItem |
getCheckBoxMenuItem(String name)
Returns the component for the specified name.
|
<E> JComboBox<E> |
getComboBox(String name)
Returns the component for the specified name.
|
Component |
getComponent(String name)
Returns the component for the specified name.
|
JDialog |
getDialog(String name)
Returns the component for the specified name.
|
JEditorPane |
getEditorPane(String name)
Returns the component for the specified name.
|
JFormattedTextField |
getFormattedTextField(String name)
Returns the component for the specified name.
|
JFrame |
getFrame(String name)
Returns the component for the specified name.
|
protected ResourceBundle |
getI18nBundle(String baseName,
Locale locale,
ClassLoader loader)
Gets the resource bundle for the given base name.
|
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.
|
JLabel |
getLabel(String name)
Returns the component for the specified name.
|
<E> JList<E> |
getList(String name)
Returns the component for the specified name.
|
ClassLoader |
getLoader()
Returns the class loader used to load components.
|
Locale |
getLocale()
Returns the locale used to load resource bundles.
|
JMenu |
getMenu(String name)
Returns the component for the specified name.
|
JMenuBar |
getMenuBar(String name)
Returns the component for the specified name.
|
JMenuItem |
getMenuItem(String name)
Returns the component for the specified name.
|
FormModel |
getModel()
Returns the form model.
|
JPanel |
getPanel(String name)
Returns the component for the specified name.
|
JPasswordField |
getPasswordField(String name)
Returns the component for the specified name.
|
JPopupMenu |
getPopupMenu(String name)
Returns the component for the specified name.
|
JProgressBar |
getProgressBar(String name)
Returns the component for the specified name.
|
JRadioButton |
getRadioButton(String name)
Returns the component for the specified name.
|
JRadioButtonMenuItem |
getRadioButtonMenuItem(String name)
Returns the component for the specified name.
|
JScrollBar |
getScrollBar(String name)
Returns the component for the specified name.
|
JScrollPane |
getScrollPane(String name)
Returns the component for the specified name.
|
JSeparator |
getSeparator(String name)
Returns the component for the specified name.
|
JSlider |
getSlider(String name)
Returns the component for the specified name.
|
JSpinner |
getSpinner(String name)
Returns the component for the specified name.
|
JSplitPane |
getSplitPane(String name)
Returns the component for the specified name.
|
JTabbedPane |
getTabbedPane(String name)
Returns the component for the specified name.
|
JTable |
getTable(String name)
Returns the component for the specified name.
|
Object |
getTarget()
Returns the object that handles events.
|
JTextArea |
getTextArea(String name)
Returns the component for the specified name.
|
JTextField |
getTextField(String name)
Returns the component for the specified name.
|
JTextPane |
getTextPane(String name)
Returns the component for the specified name.
|
JToggleButton |
getToggleButton(String name)
Returns the component for the specified name.
|
JToolBar |
getToolBar(String name)
Returns the component for the specified name.
|
JTree |
getTree(String name)
Returns the component for the specified name.
|
JWindow |
getWindow(String name)
Returns the component for the specified name.
|
boolean |
isSetComponentNames()
Returns whether
Component.setName(String) will be invoked
on all components of the form. |
protected Object |
newComponentInstance(Class<?> beanClass,
String compName)
Creates a new instance of the given bean class.
|
void |
setSetComponentNames(boolean setComponentNames)
Sets whether
Component.setName(String) will be invoked
on all components of the form. |
void |
setTarget(Object target)
Set the object that will handle events.
|
public FormCreator(FormModel model)
FormCreator
for the specified form model.model
- The form model, from which the components should be created.public FormCreator(FormModel model, ClassLoader classLoader)
FormCreator
for the specified form model
using the specified class loader.model
- The form model, from which the components should be created.classLoader
- The class loader used to load components.public FormCreator(FormModel model, Locale locale, ClassLoader classLoader)
FormCreator
for the specified form model
using the specified locale and class loader.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.public FormModel getModel()
public Locale getLocale()
public ClassLoader getLoader()
public Object getTarget()
public void setTarget(Object target)
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 }
public boolean isSetComponentNames()
Component.setName(String)
will be invoked
on all components of the form. Default is false
.public void setSetComponentNames(boolean setComponentNames)
Component.setName(String)
will be invoked
on all components of the form.public void createAll() throws Exception
Exception
public Component create() throws Exception
Exception
public JPanel createPanel() throws Exception
JPanel
.
Convenience method that invokes create()
and
casts the result to JPanel
.Exception
public JDialog createDialog(Window owner) throws Exception
JDialog
.
Convenience method that invokes createWindow(Window)
and
casts the result to JDialog
.owner
- The window to act as owner; or null
.Exception
public Window createWindow(Window owner) throws Exception
Window
.owner
- The window to act as owner; or null
.Exception
public Component create(String name) throws Exception
name
- The name of the form component.Exception
public Component create(String name, Window owner) throws Exception
Window
.name
- The name of the form component.owner
- The window to act as owner; or null
.Exception
public Object[] createBindings() throws Exception
Exception
public Component getComponent(String name) throws NoSuchComponentException
getComponent
in interface com.jformdesigner.runtime.ComponentProvider
name
- The name of the component.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.public Object getBean(String name) throws NoSuchComponentException
name
- The name of the bean.NoSuchComponentException
- If a bean with the specified name
does not exist or the bean is not created.public Object getBean(String name, boolean create) throws Exception
getBean
in interface com.jformdesigner.runtime.BeanProvider
name
- The name of the bean.create
- If true, create the bean if necessary.Exception
public JButton getButton(String name) throws NoSuchComponentException
name
- The name of the component.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 of JButton
.public JCheckBox getCheckBox(String name) throws NoSuchComponentException
name
- The name of the component.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 of JCheckBox
.public JCheckBoxMenuItem getCheckBoxMenuItem(String name) throws NoSuchComponentException
name
- The name of the component.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 of JCheckBoxMenuItem
.public <E> JComboBox<E> getComboBox(String name) throws NoSuchComponentException
name
- The name of the component.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 of JComboBox
.public JDialog getDialog(String name) throws NoSuchComponentException
name
- The name of the component.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 of JDialog
.public JEditorPane getEditorPane(String name) throws NoSuchComponentException
name
- The name of the component.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 of JEditorPane
.public JFormattedTextField getFormattedTextField(String name) throws NoSuchComponentException
name
- The name of the component.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 of JFormattedTextField
.public JFrame getFrame(String name) throws NoSuchComponentException
name
- The name of the component.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 of JFrame
.public JLabel getLabel(String name) throws NoSuchComponentException
name
- The name of the component.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 of JLabel
.public <E> JList<E> getList(String name) throws NoSuchComponentException
name
- The name of the component.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 of JList
.public JMenu getMenu(String name) throws NoSuchComponentException
name
- The name of the component.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 of JMenu
.public JMenuBar getMenuBar(String name) throws NoSuchComponentException
name
- The name of the component.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 of JMenuBar
.public JMenuItem getMenuItem(String name) throws NoSuchComponentException
name
- The name of the component.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 of JMenuItem
.public JPanel getPanel(String name) throws NoSuchComponentException
name
- The name of the component.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 of JPanel
.public JPasswordField getPasswordField(String name) throws NoSuchComponentException
name
- The name of the component.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 of JPasswordField
.public JPopupMenu getPopupMenu(String name) throws NoSuchComponentException
name
- The name of the component.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 of JPopupMenu
.public JProgressBar getProgressBar(String name) throws NoSuchComponentException
name
- The name of the component.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 of JProgressBar
.public JRadioButton getRadioButton(String name) throws NoSuchComponentException
name
- The name of the component.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 of JRadioButton
.public JRadioButtonMenuItem getRadioButtonMenuItem(String name) throws NoSuchComponentException
name
- The name of the component.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 of JRadioButtonMenuItem
.public JScrollBar getScrollBar(String name) throws NoSuchComponentException
name
- The name of the component.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 of JScrollBar
.public JScrollPane getScrollPane(String name) throws NoSuchComponentException
name
- The name of the component.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 of JScrollPane
.public JSeparator getSeparator(String name) throws NoSuchComponentException
name
- The name of the component.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 of JSeparator
.public JSlider getSlider(String name) throws NoSuchComponentException
name
- The name of the component.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 of JSlider
.public JSpinner getSpinner(String name) throws NoSuchComponentException
name
- The name of the component.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 of JSpinner
.public JSplitPane getSplitPane(String name) throws NoSuchComponentException
name
- The name of the component.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 of JSplitPane
.public JTabbedPane getTabbedPane(String name) throws NoSuchComponentException
name
- The name of the component.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 of JTabbedPane
.public JTable getTable(String name) throws NoSuchComponentException
name
- The name of the component.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 of JTable
.public JTextArea getTextArea(String name) throws NoSuchComponentException
name
- The name of the component.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 of JTextArea
.public JTextField getTextField(String name) throws NoSuchComponentException
name
- The name of the component.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 of JTextField
.public JTextPane getTextPane(String name) throws NoSuchComponentException
name
- The name of the component.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 of JTextPane
.public JToggleButton getToggleButton(String name) throws NoSuchComponentException
name
- The name of the component.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 of JToggleButton
.public JToolBar getToolBar(String name) throws NoSuchComponentException
name
- The name of the component.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 of JToolBar
.public JTree getTree(String name) throws NoSuchComponentException
name
- The name of the component.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 of JTree
.public JWindow getWindow(String name) throws NoSuchComponentException
name
- The name of the component.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 of JWindow
.protected Object newComponentInstance(Class<?> beanClass, String compName) throws Exception
return beanClass.newInstance();
".
Override this method to implement special component creation.
beanClass
- The component class.compName
- The name of the component.Exception
protected ResourceBundle getI18nBundle(String baseName, Locale locale, ClassLoader loader)
return ResourceBundle.getBundle(baseName, locale, loader);
".
Override this method to implement special bundle loading.
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.protected String getI18nString(ResourceBundle bundle, String baseName, String key)
return bundle.getString(key);
".
Override this method to implement special string loading.
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.Copyright (C) 2004-2019 FormDev Software GmbH. All rights reserved.