Java – horizontal scrolling and textfield errors
I created a customtextfield
I just need it to disable horizontalscrolling when I reach the last word. It should be able to scroll between the beginning and end of the last word
view code
import net.rim.device.api.ui.Color; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.Focuschangelistener; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.Manager; import net.rim.device.api.ui.XYEdges; import net.rim.device.api.ui.XYRect; import net.rim.device.api.ui.component.BasicEditField; import net.rim.device.api.ui.container.HorizontalFieldManager; import net.rim.device.api.ui.container.VerticalFieldManager; import net.rim.device.api.ui.decor.Border; import net.rim.device.api.ui.decor.BorderFactory; public class CustomTextField extends VerticalFieldManager { private int textWidth=0; private int textHeight=0; private BasicEditField basicEditField; private HorizontalFieldManager hfm; //Border border; public CustomTextField(int width,int height) { super(); textWidth=width; textHeight=height; //border=BorderFactory.createSimpleBorder(new XYEdges(1,1,1)); hfm=new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL){ protected void sublayout(int maxWidth,int maxHeight) { super.sublayout(maxWidth,maxHeight); setExtent(textWidth,textHeight); } }; basicEditField=new BasicEditField("","",200,BasicEditField.NO_NEWLINE); //basicEditField.setBorder(border); hfm.add(basicEditField); add(hfm); } protected void sublayout(int maxWidth,int maxHeight) { super.sublayout(textWidth,textHeight); setExtent(textWidth,textHeight); } protected void paint(Graphics graphics) { super.paint(graphics); graphics.setColor(Color.BLACK); graphics.drawRect(0,textWidth,textHeight); } }
I have initialized it to
CustomTextField textField=new CustomTextField(200,20); add(textField);
For horizontalfieldmanager, I think I need scroll (scrolling function)... But no solution has been proposed. Please help
Solution
Therefore, in the BlackBerry field, the range is the actual visual size of a field However, virtual ranges are logical sizes that can be used, some of which may not be visible For the manager to scroll, the virtual extension range is usually set to be greater than this range
I use this concept to dynamically change the virtual scope of your horizontal field manager, depending on how much space is currently required to accommodate the text in the basic edit field To do this, I have to ask the horizontalfieldmanager to monitor changes to the basiceditfield by implementing the fieldchangelistener Then, when each character is entered into the edit field, the horizontal field manager recalculates the width required for the amount of text now in the field Then reset the virtual width to that width
This allows the horizontal field manager to scroll only to the end of the input text, not to the right, which is how the code initially works
So, I don't think the BlackBerry has done anything wrong... There are no operating system bugs Previously, the virtual range was not set
I split your horizontalfieldmanager into a new private class because I don't like anonymous classes when the logic exceeds 5 lines of code Therefore, the following solutions are different
Other ideas:
1) There are drawing artifacts because you are trying to draw a border using a custom paint () implementation However, the bug was originally there, and I interpreted the problem as a rolling problem It looks like you are trying to use the border object, which may be a better way to scroll the field border
2) With my new solution, the actual customtextfield class doesn't have much content It's just a container for the custom horizontal field manager If you want, you may get rid of the outer layer However, I know that sometimes when you release code, you delete details that are not important to what you are in trouble Therefore, you may need a vertical field manager that contains a horizontal field manager that contains a basic edit field I'll leave it to you, but it will only be optional
3) I tested this on the 5.0 Storm2 simulator
import net.rim.device.api.ui.Color; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.Fieldchangelistener; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.Manager; import net.rim.device.api.ui.component.BasicEditField; import net.rim.device.api.ui.container.HorizontalFieldManager; import net.rim.device.api.ui.container.VerticalFieldManager; import net.rim.device.api.util.Arrays; public class CustomTextField extends VerticalFieldManager { private int textWidth = 0; private int textHeight = 0; private CustomHorizontalFieldManager hfm; public CustomTextField(int width,int height) { super(); textWidth = width; textHeight = height; hfm = new CustomHorizontalFieldManager(); add(hfm); } protected void sublayout(int maxWidth,int maxHeight) { super.sublayout(textWidth,textHeight); setExtent(textWidth,textHeight); } protected void paint(Graphics graphics) { // TODO: change me! super.paint(graphics); graphics.setColor(Color.BLACK); graphics.drawRect(0,textHeight); } private class CustomHorizontalFieldManager extends HorizontalFieldManager implements Fieldchangelistener { private BasicEditField basicEditField; /** the maximum virtual width of the edit field,based on the max num of chars */ private int maxVirtualWidth; public CustomHorizontalFieldManager() { super(Manager.HORIZONTAL_SCROLL); int maxNumChars = 200; basicEditField = new BasicEditField("",maxNumChars,BasicEditField.NO_NEWLINE); // determine how wide the field would need to be to hold 'maxNumChars',with the font // in use ... just pick a long string of all W's,since that's usually the widest char char[] buffer = new char[maxNumChars]; Arrays.fill(buffer,'W'); String spacer = new String(buffer); maxVirtualWidth = basicEditField.getFont().getAdvance(spacer); // we need to listen as the user types in this field,so we can dynamically alter its // virtual width basicEditField.setchangelistener(this); add(basicEditField); } protected void sublayout(int maxWidth,int maxHeight) { super.sublayout(maxWidth,maxHeight); // extent is the visible size,virtual extent can be wider if we want scrolling setExtent(textWidth,textHeight); setVirtualExtent(maxVirtualWidth,textHeight); } public void fieldChanged(Field f,int context) { if (f == basicEditField) { // recalculate how much virtual width the edit field needs,based on the // current text content int newWidth = basicEditField.getFont().getAdvance(basicEditField.getText()); setVirtualExtent(newWidth,textHeight); } } } }