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
Super Contributor
Posts: 308
Registered: ‎05-31-2011
My Device: Blackberry-SDK-9800
My Carrier: SimPakage 6.0.0.337_9800 asia.exe
Accepted Solution

how to alter the width of editfield in blackberry java

hi, blackberry exerts,

    i want to alter the width of editfield... can anybody tell me how can i do that

 

Highlighted
Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none

Re: how to alter the width of editfield in blackberry java

Hi,

  Try

    public int getPreferredWidth() {
        return yourWidth;
    }

   protected void layout(int width, int height){
        super.layout(getPreferredWidth(), height);

        setExtent(getPreferredWidth(), height);   

   }

 

Thanks & Regards

            pp

Super Contributor
Posts: 308
Registered: ‎05-31-2011
My Device: Blackberry-SDK-9800
My Carrier: SimPakage 6.0.0.337_9800 asia.exe

Re: how to alter the width of editfield in blackberry java

BasicEditField bef = new BasicEditField("ttt","")               

{                   

public int getPreferredHeight()                  

  {                       

return 30;                 

  }
                   

public int getPreferredWidth()                    

{                        return 50;                   

}
                    public void layout(int width, int height)                  

  {                     

  //setExtent(getPreferredWidth(), getPreferredHeight());                                                                                  super.layout(getPreferredWidth(), getPreferredHeight());                     

  setExtent(getPreferredWidth(), height);                   

}             

  };

 

 

next i am preparing grid view  in that,one cell field:

 

grid.add(bef);

 

is there anythig else to add...

 



Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none

Re: how to alter the width of editfield in blackberry java

Hi yogeh,

   use TableLayoutManager and it will adjust the width of field( based on no. of columns added)  to your screen.

 

Am not getting your requirement, but if u want to insert fields so that it will adjust space then use RoundedPanel and u can add tablelayoutmanager to this panel.

/*
 * RoundedPanel.java
 *
 * © <your company here>, 2003-2008
 * Confidential and proprietary.
 */


import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.system.Display;

/**
 * 
 */
public class RoundedPanel extends VerticalFieldManager {
    private int _heightBtmIncr = 0;
    private int color = Color.WHITE;
    private String str = null;
    
    public RoundedPanel() {
        super(Manager.VERTICAL_SCROLL);
    }
    
    public RoundedPanel(int heightBtmIncr) {
        super(Manager.VERTICAL_SCROLL);
        _heightBtmIncr = heightBtmIncr;
    }
    
    public RoundedPanel(int color , String str) {
         
        super(Manager.VERTICAL_SCROLL);
        this.str = str;
        this.color = color;
    }
    
    public void paint(Graphics g) {
        g.setBackgroundColor(Color.LIGHTGREY);
        int initialColor = g.getColor();
        g.setColor(Color.LIGHTGREY);
        g.fillRect(0, 0, this.getPreferredWidth(), this.getHeight());
        g.setColor(color);
        g.fillRoundRect(3, 3, this.getPreferredWidth() - 6, this.getHeight() - 6, 17, 17);
        g.setColor(Color.DARKGRAY);
        g.drawRoundRect(3, 3, this.getPreferredWidth() - 6, this.getHeight() - 6, 17, 17);
        g.setColor(initialColor);
        super.paint(g);
    }
        
    public void sublayout(int maxWidth, int maxHeight) {
        super.sublayout(maxWidth, Integer.MAX_VALUE);
        int fieldCount = getFieldCount();
        int x = 12;
        int y = 10;
        Field child = null;
    
        for (int i = 0; i < fieldCount; i++) {
            child = this.getField(i);
            setPositionChild(child, x, y);
            layoutChild(child, getFieldWidth(child), Integer.MAX_VALUE);
            int childHeight = getFieldHeight(child);
            y = y + childHeight;
        }
        
        this.setExtent(getPreferredWidth(), Math.max(y, getPreferredHeight()));
        setVirtualExtent(getPreferredWidth(), Math.max(y, getPreferredHeight()));
    }                  
    
    private int getFieldWidth(Field f) {
        return Math.min(Math.max(Math.max(f.getContentWidth(), f.getWidth()), f.getPreferredWidth()), this.getPreferredWidth() - 12);
    }
    
    private int getFieldHeight(Field f) {
        return Math.max(Math.max(f.getContentHeight(), f.getHeight()), f.getPreferredHeight());
    }
    
    public int getPreferredWidth() {
        return Display.getWidth();
    }
    
    public int getPreferredHeight() {
        int height = 6;
        int iNumFields = getFieldCount();
        for (int i = 0; i < iNumFields; i++) {
            height += getFieldHeight(getField(i));
        }
        
        return height + _heightBtmIncr + 6;
    }
    
    protected boolean keyDown( int keycode, int status ) {
        invalidate();
        
        return super.keyDown( keycode, status );
    }
    
    protected boolean navigationMovement( int dx, int dy, int status, int time ) {
        invalidate();
        
        return super.navigationMovement( dx, dy, status, time );
    }
    
    protected int moveFocus(int amount, int status, int time) {
        invalidate();
        
        return super.moveFocus(amount, status, time);
    }
}

 

 

Thanks & Regards

         pp

Super Contributor
Posts: 308
Registered: ‎05-31-2011
My Device: Blackberry-SDK-9800
My Carrier: SimPakage 6.0.0.337_9800 asia.exe

Re: how to alter the width of editfield in blackberry java

here i want to change the width of edit field here... PP. i am sorry if my reply is silly.

package

mypackage;

 

import

net.rim.device.api.i18n.Locale;

import

net.rim.device.api.system.Bitmap;

import

net.rim.device.api.system.Display;

import

net.rim.device.api.ui.*;

import

net.rim.device.api.ui.component.*;

import

net.rim.device.api.ui.container.*;

import

net.rim.device.api.ui.decor.BackgroundFactory;

import

net.rim.device.api.ui.decor.Border;

import

net.rim.device.api.ui.decor.BorderFactory;

 

public

class GridFieldManagerDemo extendsUiApplication

{

publicstaticvoidmain(String[] args)

{

GridFieldManagerDemo theApp =

newGridFieldManagerDemo();

theApp.enterEventDispatcher();

}

 

GridFieldManagerDemo()

{

pushScreen(

newGridScreen());

}

}

class

GridScreen extendsMainScreen

{

publicGridScreen()

{

//setTitle("GridFieldManager Demo");this.setTitle("My Screen");

 

Bitmap logo = Bitmap.getBitmapResource(

"acusis.png");

 

// Set the linear background.this.getMainManager().setBackground(

 

BackgroundFactory.createLinearGradientBackground(0x0099CCFF,

 

0x0099CCFF,0x00336699,0x00336699));

Bitmap borderBitmap = Bitmap.getBitmapResource(

"borderrect.png");

 

// Bitmap logo = Bitmap.getBitmapResource("borderrect.png");

EditField ef=

new EditField("","", 8, EditField.FILTER_EMAIL);

 

BasicEditField bef =

newBasicEditField();

 

/*{

public int getPreferredHeight()

{

return 30;

}

public int getPreferredWidth()

{

return 50;

}

public void layout(int width, int height)

{

//setExtent(getPreferredWidth(), getPreferredHeight());

super.layout(getPreferredWidth(), getPreferredHeight());

setExtent(getPreferredWidth(),getPreferredHeight());

}

};*/

 

ef.setBorder (BorderFactory.createBitmapBorder(

new XYEdges(10,10,10,10),newXYEdges(20,20,20,20), borderBitmap

 

)

 

);

PasswordEditField pwd=

new PasswordEditField ("","",8,EditField.FILTER_DEFAULT);

 

pwd.setBorder (BorderFactory.createBitmapBorder(

new XYEdges(10,10,10,10),newXYEdges(10,10,10,10), borderBitmap

 

)

 

);

// int borderHeight = Display.getHeight()/2;//int borderWidth = Display.getWidth()/2;

GridFieldManager grid =

new GridFieldManager(3,2,GridFieldManager.AUTO_SIZE | GridFieldManager.HORIZONTAL_SCROLLBAR ); //2 row, 3 coloumn

grid.add(

new LabelField(""));

grid.add(

new BitmapField(logo,BitmapField.FIELD_RIGHT));

grid.add(

new LabelField("UserName:",LabelField.FIELD_LEFT));

grid.add(ef);

grid.add(

new LabelField("Password:",LabelField.FIELD_RIGHT));

grid.add(pwd);

 

 

 

 

 

grid.setColumnPadding(10);

grid.setRowPadding(20);

//grid.setMargin(20,-50, 30, 0);/* grid.setBorder(

 

BorderFactory.createBitmapBorder(

 

new XYEdges(12,12,12,12), borderBitmap

 

)

 

);*/

 

add(grid);

 

}

}

Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none

Re: how to alter the width of editfield in blackberry java

Hi yogesh,

  use

TableLayoutManager Table = new TableLayoutManager(new int[] {TableLayoutManager.FIXED_WIDTH, TableLayoutManager.FIXED_WIDTH, TableLayoutManager.FIXED_WIDTH}, new int[] {100,(Display.getWidth()-25)/2,30}, 0,Manager.NO_VERTICAL_SCROLL |Manager.USE_ALL_WIDTH );
       

here my first column is 100 pixel width so i have given 100..  and last column is 30 pixel width... and this manager adjust accordingly.

 

for custom editfield am pasting code here...

/*
 * CustomEditField.java
 *
 * © <your company here>, 2003-2008
 * Confidential and proprietary.
 */

import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.Dialog;

/**
 * 
 */
public class CustomEditField extends EditField {
    private int _defaultFontSize = 24;
    
    public CustomEditField(long style) {
        super(style);
        SetFont(Font.PLAIN, _defaultFontSize);
    }
    
    public CustomEditField(String label, String initialValue) {
        super(label, initialValue);
        SetFont(Font.PLAIN, _defaultFontSize);
    }
    
    public CustomEditField(String label, String initialValue, int maxNumChars, long style) {
        super(label, initialValue, maxNumChars, style);
        SetFont(Font.PLAIN, _defaultFontSize);
    }
    
    public CustomEditField() {
        super();
        SetFont(Font.PLAIN, _defaultFontSize);
    }
    
    public CustomEditField(String label, String initialValue, int font_size) {
        super(label, initialValue);
        SetFont(Font.PLAIN, font_size);
    }
    
    public CustomEditField(String label, String initialValue, int font_style, int font_size) {
        super(label, initialValue);
        SetFont(font_style, font_size);
    }
    
    private void SetFont(int font_style, int font_size) {
        try {
            FontFamily fontFamily = FontFamily.forName(FontFamily.FAMILY_SYSTEM);
            Font font = fontFamily.getFont(font_style | FontFamily.SCALABLE_FONT, font_size).derive(font_style);
            this.setFont(font);
        } catch (Exception e) {
            //System.err.println("CustomEditField - SetFont : " + e.getMessage());
            e.printStackTrace();
            
            Dialog.alert("CustomEditField - SetFont : " + e.toString());
        }
    }
    
    protected void layout(int width, int height){
        super.layout(width, getPreferredHeight());
        setExtent(width, getPreferredHeight());
    }
    
    public int getPreferredHeight() {
        return 30;
    }
    
}

 here i have not given width and u can give if u want.

 

 

Thanks & Regards

            pp