Thanks for you reply, Henri. I have tried a variety of approaches such as those. I don’t think it’s necessarily a problem with my widget, because I observed the same problem using the sample custom fields included with the CustomField add-on. I will paste my code here to illustrate.
import java.util.Arrays;
import org.vaadin.addon.customfield.demo.data.City;
import org.vaadin.addon.customfield.demo.field.CityField;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
@SuppressWarnings("serial")
public class UnitsFieldTest extends VerticalLayout {
// the 'POJO' we're editing
AudioTrack song;
public UnitsFieldTest() {
song = new AudioTrack();
BeanItem<AudioTrack> songItem = new BeanItem<AudioTrack>(song);
setSpacing(true);
// Create the Form
final Form songForm = new Form();
songForm.setCaption("Audio track details");
songForm.setWriteThrough(false); // we want explicit 'apply'
songForm.setInvalidCommitted(false); // no invalid values in datamodel
// FieldFactory for customizing the fields and adding validators
songForm.setFormFieldFactory(new SongFieldFactory());
songForm.setItemDataSource(songItem); // bind to POJO via BeanItem
// Determines which properties are shown, and in which order:
songForm.setVisibleItemProperties(Arrays.asList(new String[] {
"name", "artist", "placeOfRecording" }));
// Add form to layout
addComponent(songForm);
}
}
class SongFieldFactory extends DefaultFieldFactory {
private static final long serialVersionUID = 1049955366709543836L;
private static final String COMMON_FIELD_WIDTH = "8em";
public SongFieldFactory() {
}
@Override
public Field createField(Item item, Object propertyId,
Component uiContext) {
Field f;
if ("placeOfRecording".equals(propertyId)) {
f = createUnitsField(propertyId);
} else {
f = super.createField(item, propertyId, uiContext);
}
if ("name".equals(propertyId)) {
TextField tf = (TextField) f;
tf.setRequired(true);
tf.setRequiredError("Please enter the track name");
tf.setWidth(COMMON_FIELD_WIDTH);
} else if ("artist".equals(propertyId)) {
TextField tf = (TextField) f;
tf.setRequired(true);
tf.setRequiredError("Please enter the artist name");
tf.setWidth(COMMON_FIELD_WIDTH);
}
return f;
}
private Field createUnitsField(Object propertyId) {
CityField cf = new CityField();
cf.setCaption("City");
cf.setWidth(COMMON_FIELD_WIDTH);
return cf;
}
}
class AudioTrack {
private static final long serialVersionUID = 1049955366709543835L;
public String name;
public String artist;
public int bitRate;
public boolean hasDrm;
public City placeOfRecording;
public boolean isHasDrm() {
return hasDrm;
}
public void setHasDrm(boolean hasDrm) {
this.hasDrm = hasDrm;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public int getBitRate() {
return bitRate;
}
public void setBitRate(int bitRate) {
this.bitRate = bitRate;
}
public City getPlaceOfRecording() {
return placeOfRecording;
}
public void setPlaceOfRecording(City placeOfRecording) {
this.placeOfRecording = placeOfRecording;
}
}
With each of the widgets being set to a width of 8em, the two TextFields responded appropriately, but the custom field CityField did not resize its contained combo box accordingly. Instead, the right side is cut off. Here’s a screen shot:
I also got the same results when I used the example BooleanField to represent the hasDrm field, and I used a setWidth argument small enough to correctly resize the text fields, but truncate the button.
I ran “analyze layouts” on the above example, and got these results:
Layouts analyzed on server, total top level problems: 0
Note that the component error shown in the screenshot is unrelated to the field being cut off; it’s probably just something with how I am setting up the model in the example. I am observing the same field truncation behavior in my application with my custom field, without that component error.
[color=#ff0000]
Caused by: java.lang.IllegalAccessException: Class com.vaadin.data.util.MethodProperty can not access a member of class com.example.vaadintest2.AudioTrack with modifiers "public"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.reflect.Method.invoke(Method.java:588)
at com.vaadin.data.util.MethodProperty.getValue(MethodProperty.java:610)
[/color]