Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 18
Registered: ‎05-17-2012
My Device: NA

Horizontal Scroll Effect and Text Field Development

I have created a CustomTextField which scrolls itself towards left when i type text which are extra than the width of the TextField for which a HorizonalFieldManager is used But now the problem is if i Right click with my mouse and scroll it it goes on to inadequate length but does not stop to the last word i type What is the problem here ?? Is it a bug

I just need that to disable HorizontalScrolling when it reaches the last word It should be able to scroll just between the start and end of last word in word

Check out the 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, 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,0, textWidth, textHeight);
    }

i have initialised it as

 

CustomTextField textField=new CustomTextField(200, 20);
            add(textField);

 

I feel that the reltion is with The Horizontal Scrolling effect function but have not come up to the solution please Help

 

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: Horizontal Scroll Effect and Text Field Development

Welcome to the forums!

 

In fact, the problem is in BasicEditField. All TextField descendants are "width-hungry", meaning that they take all available width during layout.

Now, HORIZONTAL_SCROLL flag on HorizontalFieldManager does two things:

1) It enables horizontal scrolling (duh!)

2) It instructs the manager to grant an almost infinite (Integer.MAX_VALUE >> 1 = 0x3FFFFFFF) width to the contained fields. In the end, the manager will set its virtual width to be only as big as needed.

 

Effect #2 along with "width-hungry" nature of TextField create the effect you're seeing. Solving it is not that easy.

 

First of all, though, let's take care of the pieces of your code which are obviously wrong. Your two sublayout overrides are incorrect. You don't need the one in HorizontalFieldManager at all, while the one in CustomTextField should look more like:

protected void sublayout(int maxWidth, int maxHeight) {
  super.sublayout(Math.min(maxWidth, textWidth), Math.min(maxHeight, textHeight));
}

In addition, in your constructor you need to call super() with parameters: super(USE_ALL_WIDTH | USE_ALL_HEIGHT). And never ever call setExtent after super.sublayout - you will only confuse the built-in manager and will produce some hard to explain behavior. Well - sometimes you can, but you need to be extremely careful about what you are doing. For a working example of such unusual call sequence take a look here:

Implementing a standard-style scrollbar

 

Now, back to your original problem: there is no ready-made solution, but you can achieve a good approximation. This will involve some creative use of layout within your BasicEditField. Since it tries to re-layout each time you hit the right border while typing (or maybe on typing anything as well as deleting anything with a backspace) you can check the current text, find out its advance, give it a little more width (so that new characters can be typed) and pass that width into super.layout instead of the maxWidth passed to you. So your BasicEditField might look like this:

basicEditField=new BasicEditField("","",200,BasicEditField.NO_NEWLINE) {
  protected void layout(int maxWidth, int maxHeight) {
    int textWidth = getFont().getAdvance(getText());
    int newWidth = textWidth + someAdditionalValueCalculatedElsewhere;
    super.layout(Math.min(maxWidth, newWidth), maxHeight);
  }
};

See if it helps.

 

P.S.: there is a knowledge base article dealing with similar fields, but it has the same "infinite width" problem. However, the comments to it give some ideas on the proper border implementation - worth reading anyway.

Scrollable one-line text input field

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!