I have written a custom edit field with border using JDE4.2.1 This field is then added to 开发者_如何学运维a VerticalLayoutManager and instanciated as such:
BorderEditField bef = new BorderEditField("Enter a value: ", null, 6,
BorderEditField.FIELD_RIGHT | BorderEditField.FILTER_NUMERIC);
However no matter which style I specify (FIELD_HCENTER) the Field is always left aligned. Is there something obvious I might be missing here? Tried it on different versions of JDE with the same result...
public class BorderEditField extends BasicEditField {
public BorderEditField(String label, String initialValue, int maxNumChars, long style)
{
super(label, initialValue, maxNumChars, style);
}
private int iRectX = getFont().getAdvance(getLabel());
private int iRectWidth = (getMaxSize() * getFont().getAdvance("X")) + 16;
public int getPreferredWidth() {
return Display.getWidth();
}
public void layout(int width, int height) {
super.layout(width, getPreferredHeight());
setExtent(width, getPreferredHeight());
}
public void paint(Graphics g) {
super.paint(g);
if (isFocus()) {
g.setColor(Color.RED);
g.setGlobalAlpha(220);
g.drawRect(iRectX, 0, iRectWidth, getPreferredHeight());
} else {
g.setColor(Color.BLACK);
g.setBackgroundColor(Color.DARKBLUE);
g.setGlobalAlpha(150);
g.drawRect(iRectX, 0, iRectWidth, getPreferredHeight());
}
}
}
Thanks.
When you create the VerticalFieldManager are you telling it to USE_ALL_WIDTH?? If you don't set that flag then it will only be as wide as the widest component, so no matter what style you set for its children they will always look the same.
==UPDATE==
Ok, another thing to look at are your layout and getPreferredWidth methods. In getPreferredWidth you are setting it to the width of the screen. This means that it will take up the entire width of the manager and therefore it will not be able to position it. Try calculating the actual width based on the contents. Same with the layout method, you are calling setExtent with the full available width. This tells the manager that the component should take up the full width. Try using the preferred width, once you have calculated it correctly.
As a quick test, before you start trying to work out how to calculate the width, you can just hard code a value in there. If this makes a difference then you can start working out how to calculate the width properly.
Good answer from Dave the code now looks like this and the field gets aligned properly:
public BorderEditField(String label, String initialValue, int maxNumChars, long style) {
super(label, initialValue, maxNumChars, style);
}
private int iRectX = getFont().getAdvance(getLabel()); public int getPreferredWidth() { return super.getMaxSize() * super.getFont().getAdvance("X"); } public int getPreferredHeight() { return super.getPreferredHeight(); } public void layout(int width, int height) { width = Math.min( width, getPreferredWidth() ); height = Math.min( height, getPreferredHeight() ); super.layout(width, height); setExtent(width, height); } public void paint(Graphics g) { super.paint(g); if (isFocus()) { g.setColor(Color.RED); g.drawRect(iRectX, 0, getPreferredWidth() , getPreferredHeight()); } else { g.setColor(Color.BLACK); g.drawRect(iRectX, 0, getPreferredWidth() , getPreferredHeight()); } }
精彩评论