Vaadin的comboBoxes旨在逐行显示字幕和项目(您可以在here 中看到它们)。如果我想在同一行看到它们,解决方案是什么?
现在我想尝试如下......
HorizontalLayout hlMain = new HorizontalLayout();
hlMain.addComponent(new Label("Gender:"));
final ComboBox gender = new ComboBox("" , genderList);
hlMain.addComponent(gender);
但我知道上面的代码太难看了,所以我试图用CSS。我想知道有没有简单的方法来获得Vaadin?任何建议对我都有很大的帮助。
答案 0 :(得分:3)
如果您愿意为此专门设置布局,请使用FormLayout
:
// run with: spring run vaadin.groovy
@Grapes([
@Grab('org.vaadin.spring:spring-boot-vaadin:0.0.5.RELEASE'),
@Grab('com.vaadin:vaadin-server:7.4.4'),
@Grab('com.vaadin:vaadin-client-compiled:7.4.4'),
@Grab('com.vaadin:vaadin-themes:7.4.4'),
])
import org.vaadin.spring.annotation.VaadinUI
import com.vaadin.server.VaadinRequest
import com.vaadin.ui.*
@VaadinUI
class MyUI extends UI {
protected void init(VaadinRequest request) {
setContent(new FormLayout( // XXX
new ComboBox("Gender:").with{ addItem("M"); addItem("F"); it }
))
}
}
如果您不将空标题放在ComboBox上,那么您当前的代码将会起作用(它会让Vaadin认为,有些东西要显示并呈现空行,中断,然后是组合框)。然而FormLayout是最好的解决方案。
// run with: spring run vaadin.groovy
@Grapes([
@Grab('org.vaadin.spring:spring-boot-vaadin:0.0.5.RELEASE'),
@Grab('com.vaadin:vaadin-server:7.4.4'),
@Grab('com.vaadin:vaadin-client-compiled:7.4.4'),
@Grab('com.vaadin:vaadin-themes:7.4.4'),
])
import org.vaadin.spring.annotation.VaadinUI
import com.vaadin.server.VaadinRequest
import com.vaadin.ui.*
@VaadinUI
class MyUI extends UI {
protected void init(VaadinRequest request) {
setContent(new HorizontalLayout(
new Label("Gender"),
new ComboBox().with{ addItem("M"); addItem("F"); it } // no label!
))
}
}
答案 1 :(得分:2)
您可以在输入字段之前的任何位置使用Vaadin FormLayout组件。
示例:
// A FormLayout used outside the context of a Form
FormLayout fl = new FormLayout();
// Make the FormLayout shrink to its contents
fl.setSizeUndefined();
TextField tf = new TextField("A Field");
fl.addComponent(tf);
// Mark the first field as required
tf.setRequired(true);
tf.setRequiredError("The Field may not be empty.");
TextField tf2 = new TextField("Another Field");
fl.addComponent(tf2);