General

Components

Community

Development

TDF

Documents > Cookbook >Forms



Form
Since version 0.8, new APIs are added to support forms. Because controls are implementation-dependent, the default form provider will follow the capability defined by OpenOffice.org, it may not be fully compatible with other ODF editors. You can use the following code to create a form.

			TextDocument textDoc = TextDocument.newTextDocument();
Form form = textDoc.createForm("Form1");

Following code shows how to get a form in a text document and remove it.

			Iterator<Form> iterator = textDoc.getFormIterator();
while (iterator.hasNext()) {
deleteForm = iterator.next();
if (deleteForm.getFormName().equals("Form1"))
break;
}
textDoc.removeForm(deleteForm);


Controls

Button
Below codes will create a button and add it to the text document with a paragraph as the anchor position. The FrameRectangle specifies an area and the position of this button. The last two parameters are used to specify the control name and the initialized label value.

			Paragraph para = doc.addParagraph("Add form button here:");
FrameRectangle btnRtg = new FrameRectangle(0.5, 2, 2.9433, 0.5567, SupportedLinearMeasure.IN);
Button btn = (Button)form.createButton(para, btnRtg, "Button1", "Push Button 1");

You can get an iterator of the button as follows.

			Iterator<FormControl> iterator = Button.getSimpleIterator(form);
while (iterator.hasNext()) {
btn = (Button) iterator.next();
}

Label
Below codes will create a label and add it to the text document with a paragraph as the anchor position.

			Paragraph para = doc.addParagraph("Add form label here:");
FrameRectangle labelRtg = new FrameRectangle(0.5, 1.2553, 1.2, 0.5, SupportedLinearMeasure.IN);
Label label = (Label) form.createLabel(doc, labelRtg, "Label2","This is a label.");

You can get an iterator of the label as follows.

			Iterator<FormControl> iterator = Label.getSimpleIterator(form);
while (iterator.hasNext()) {
label = (Label) iterator.next();
}

TextBox
Below codes will create a text box and add it to the text document with a paragraph as the anchor position. The last parameter are used to specify whether this text box supports multiple-line input.

			Paragraph para = doc.addParagraph("Add text box here:");
FrameRectangle textBoxRtg = new FrameRectangle(0.5, 0.2846, 2.9432, 0.8567, SupportedLinearMeasure.IN);
TextBox textbox = (TextBox)form.createTextBox(para, textBoxRtg, "TextBox1", "Please input your value here", true);

You can get an iterator of the text box as follows.

			Iterator<FormControl> iterator = TextBox.getSimpleIterator(form);
while (iterator.hasNext()) {
textBox = (TextBox) iterator.next();
}

ListBox
Below codes will create a list box and add it to the text document with a paragraph as the anchor position. The fourth parameter is used to specify whether this list box supports multiple selection. And the last parameter is used to specify the visibility of a drop-down list.

			Paragraph para = doc.addParagraph("Add list box here:");
FrameRectangle listBoxRtg = new FrameRectangle(0.5752, 0.1429, 2.3307, 0.8398, SupportedLinearMeasure.IN);
ListBox listBox = (ListBox)form.createListBox(para, listBoxRtg, "ListBox", true, false);

You can get an iterator of the list box as follows.

			Iterator<FormControl> iterator = ListBox.getSimpleIterator(form);
while (iterator.hasNext()) {
listBox = (ListBox) iterator.next();
}

ComboBox
Below codes will create a combo box and initialize its entry list with a string array. The last parameter is used to specify the visibility of a drop-down list.

			FormControl comboBox = form.createComboBox(doc, new FrameRectangle(0.7972, 1.2862, 2.4441, 0.2669, SupportedLinearMeasure.IN), "combo1", "dd", true);
String[] items = { "aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj" };
((ComboBox) comboBox).addItems(items);

You can get an iterator of the combo box as follows.

			Iterator<FormControl> iterator = ComboBox.getSimpleIterator(form);
while (iterator.hasNext()) {
comboBox = (ComboBox) iterator.next();
}

RadioButton
Below codes will create three radio buttons in a group, named "Group1", but each of them has different item value, from 1 to 3.

			FrameRectangle radioRtg = new FrameRectangle(0.7972, 1.2862, 2.4441, 0.2669, SupportedLinearMeasure.IN);
RadioButton radiobutton = (RadioButton) form.createRadioButton(doc, radioRtg, "Group1", "RadioButton 1", "1");
RadioButton radiobutton = (RadioButton) form.createRadioButton(doc, radioRtg, "Group1", "RadioButton 2", "2");
RadioButton radiobutton = (RadioButton) form.createRadioButton(doc, radioRtg, "Group1", "RadioButton 3", "3");

You can get an iterator of the radio button as follows.

			Iterator<FormControl> iterator = RadioButton.getSimpleIterator(form);
while (iterator.hasNext()) {
radioBtn = (RadioButton) iterator.next();
}

CheckBox
Below code will create a check box and initialize its label and value in the last two parameters.

			FrameRectangle checkBoxRtg = new FrameRectangle(0.7972, 1.2862, 2.4441, 0.2669, SupportedLinearMeasure.IN);
CheckBox checkBox = (CheckBox) form.createCheckBox(doc, checkBoxRtg, "CheckBox 1", "This is choice 1", "1");

You can get an iterator of the check box as follows.

			Iterator<FormControl> iterator = CheckBox.getSimpleIterator(form);
while (iterator.hasNext()) {
checkBox = (CheckBox) iterator.next();
}

Date Field
Below code will create a date field, set the spin button and drop-down button visible and also set the date format as "12/07/15".

			FrameRectangle fieldRtg = new FrameRectangle(0.5, 2.0, 2.9433, 0.5567, SupportedLinearMeasure.IN);
DateField dateField = (DateField)form.createDateField(para, fieldRtg, "DateField", "20120715");
dateField.setSpinButonVisible(true);
dateField.setDropDownVisible(true);
dateField.formatDate("yy/MM/dd", Locale.US);

You can get an iterator of the date field as follows.

			Iterator<FormControl> iterator = DateField.getSimpleIterator(form);
while (iterator.hasNext()) {
dateField = (DateField) iterator.next();
}

Time Field
Below code will create a time field, set the spin button visible and set the date format as "15:23:40".

			FrameRectangle fieldRtg = new FrameRectangle(0.5, 2.0, 2.9433, 0.5567, SupportedLinearMeasure.IN);
TimeField timeField = (TimeField) form.createTimeField(para, fieldRtg, "TimeField", "15234000");
timeField.setSpinButonVisible(true);
timeField.formatTime("HH:mm a", Locale.US);

You can get an iterator of the time field as follows.

			Iterator<FormControl> iterator = TimeField.getSimpleIterator(form);
while (iterator.hasNext()) {
timeField = (TimeField) iterator.next();
}

Numeric Field
Below code will create a numeric field, set the spin button visible and set the decimal accurcy to 3.

			FrameRectangle fieldRtg = new FrameRectangle(0.5, 2.0, 2.9433, 0.5567, SupportedLinearMeasure.IN);
NumericField numericField = (NumericField) form.createNumericField(para, fieldRtg, "NumericField", "-154.3567");
numericField.setDecimalAccuracy(3);
numericField.setSpinButonVisible(true);

You can get an iterator of the numeric field as follows.

			Iterator<FormControl> iterator = NumericField.getSimpleIterator(form);
while (iterator.hasNext()) {
numericField = (NumericField) iterator.next();
}

Pattern Field
Below code will create a pattern field, set the spin button visible and set the literal mask and edit mask, which only allows 5 digits of numbers.

			FrameRectangle fieldRtg = new FrameRectangle(0.5, 2.0, 2.9433, 0.5567, SupportedLinearMeasure.IN);
PatternField patternField = (PatternField) form.createPatternField(para, fieldRtg, "PatternField", "12345");
patternField.setEditMask("NNLNNN");
patternField.setLiteralMask("##.###");
patternField.setSpinButonVisible(true);

You can get an iterator of the pattern field as follows.

			Iterator<FormControl> iterator = PatternField.getSimpleIterator(form);
while (iterator.hasNext()) {
patternField = (PatternField) iterator.next();
}

Currency Field
Below code will create a currency field, set the spin button visible, set the decimal accurcy to 4 and use the 'CNY' as the currency symbol.

			FrameRectangle fieldRtg = new FrameRectangle(0.5, 2.0, 2.9433, 0.5567, SupportedLinearMeasure.IN);
CurrencyField currencyField = (CurrencyField) form.createCurrencyField(para, fieldRtg, "CurrencyField", "135.467");
currencyField.setCurrencySymbol("CNY");
currencyField.setDecimalAccuracy(4);
currencyField.setSpinButonVisible(true);

You can get an iterator of the currency field as follows.

			Iterator<FormControl> iterator = CurrencyField.getSimpleIterator(form);
while (iterator.hasNext()) {
currencyfield = (CurrencyField) iterator.next();
}


Size and Style
If you want to handle more style settings of a form control like horizontal alignment, you can try ControlStyleHandler and GraphicProperties.

			ControlStyleHandler handler = button.getDrawControl().getStyleHandler();
GraphicProperties properties = handler.getGraphicPropertiesForWrite();
properties.setHorizontalPosition(StyleTypeDefinitions.FrameHorizontalPosition.FROMLEFT);
You can use FrameRectangle to change the position and size of a form control.

			FrameRectangle rtg = new FrameRectangle(0.01, 2.0, 5, 2,SupportedLinearMeasure.IN);
button.setRectangle(rtg);


Impressum (Legal Info) | Privacy Policy (Datenschutzerklärung) | Statutes (non-binding English translation) - Satzung (binding German version) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Apache License, v2.0. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License v2.0. “LibreOffice” and “The Document Foundation” are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy. LibreOffice was based on OpenOffice.org.