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: 34
Registered: ‎10-03-2012
My Device: 9300
My Carrier: cell c
Accepted Solution

change button bitmap

Hello everyone

I have customfield button called cbutton.
I want it to show different text each time I click on it.(Eg a-z) . But when I try
cbutton =new custombutton(alpha);
It doesn't seem to work.
Plz help
Developer
Posts: 16,750
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: change button bitmap

crystal ball is all cloudy, unfortunately i would need to see the code to have at least a chance at saying why "it does not work".
(which is not a very helpful error description, btw)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 34
Registered: ‎10-03-2012
My Device: 9300
My Carrier: cell c

Re: change button bitmap

This is the code. I have six bitmaps, lsq,lsqf,dsq,dsqf,nsq,nsqf.

when i click on either button i would like that button to change to nsq. but the following code doesnt do that.

thank u in advance.

 

 

//////////////my screen code

package mypackage;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen implements FieldChangeListener
{
 
 String pice[] = {"dsq","lsq"};
 CustomButton[] nButton = new CustomButton[2];
 
    /**
     * Creates a new MyScreen object
     */
    public MyScreen()
    {       
        // Set the displayed title of the screen      
        setTitle("MyTitle");
       
        GriFildManager gFild = new GriFildManager();
       
        nButton[0]= new CustomButton("lsq");
  nButton[0].setChangeListener(this);
        gFild.add(nButton[0]);
        nButton[1]= new CustomButton("dsq");
  nButton[1].setChangeListener(this);
        gFild.add(nButton[1]);
        add(gFild);
    }

 public void fieldChanged(Field arg0, int arg1) {
  // TODO Auto-generated method stub
  if(arg0==nButton[0])
  {
   nButton[0]= new CustomButton("nsq");
  }
  
  if(arg0==nButton[1])
  {
   nButton[1]= new CustomButton("nsq");
  }
  
 }
}
/////////////////////////////////////////////////end my screen

////////////////////////custom button start

package mypackage;

 

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;

 

public class CustomButton extends Field {

 BitmapField bitmapField;
 
 private String pisi;//label
 private String fon; //focus on
 private String fout;//focus off

 public CustomButton(String labelp) {
   super(0);
   this.pisi = labelp;
   }
 
 
 protected void drawFocus(Graphics graphics, boolean on) {
 }
 
   public int getPreferredHeight() {
   return 36;
   }
   public int getPreferredWidth() {
   return 36;
   }
   protected void layout(int width, int height) {
   setExtent
   (Math.min(width, getPreferredWidth()), Math.min(height,
   getPreferredHeight()));
   }

 protected void paint(Graphics graphics) {
 if (isFocus()) {
  graphics.setColor(0x4682b4);
  graphics.fillRoundRect(1, 1, getWidth()-2, getHeight()-2, 12, 12);

  fon=pisi+"f.png";
  
  Bitmap wdc = Bitmap.getBitmapResource(fon);
  bitmapField = new BitmapField(wdc);
  graphics.drawBitmap(0, 0, 36,36, wdc, 0, 0);
  
 }
 else {
 graphics.setColor(0x4682b4);
 graphics.fillRoundRect(1, 1, getWidth()-2, getHeight()-2, 12, 12);


  fout=pisi+".png";
 
 Bitmap wdc = Bitmap.getBitmapResource(fout);
 bitmapField = new BitmapField(wdc);
 graphics.drawBitmap(0, 0, 36,36, wdc, 0, 0);
 
 }
 }


 protected void onFocus(int direction) {
  super.onFocus(direction);
  invalidate();
  }
  protected void onUnfocus() {
  super.onUnfocus();
  invalidate();
  }
 
  
  
  protected boolean navigationClick(int status, int time) {
   fieldChangeNotify(0);
   return true;
   }
 
 
public boolean isFocusable() {
return true;
}


}


///////////////////////end custom button

///////////////////////////Fieldmanager start
package mypackage;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;


public class GriFildManager extends Manager{

 
 
 public GriFildManager() {
  super(0);
  
  }

 protected void sublayout(int width, int height)
 {
  int columnWidths = 36;
  int columnHeight = 36;
  
  int availableWidth = width;
  int availableHeight = height;
  int fieldXOffset=0;
 
  
  
  if(getFieldCount()>0)
  {
   
   for(int i=0; i<getFieldCount();i++)
   {
   Field centeredField = getField(i);
   layoutChild(centeredField,availableWidth,height);
   
   
   
   setPositionChild(centeredField,0+(fieldXOffset*36),0);
   
   fieldXOffset++;
   
   }
  
   setExtent(width,height);
   
   
  }
 
    
 }
 
 
}


////////////////////////////end fieldmanager

 

 

 

Developer
Posts: 16,750
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: change button bitmap

you have to replace the old field with the new, it is not automatically done when you assign a new value to the variable.
it would be more elegant to have a method in your custom button to change the state, or even better to have the button doing it by itself.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 34
Registered: ‎10-03-2012
My Device: 9300
My Carrier: cell c

Re: change button bitmap

Could you please help me with this. I have tried various methods. With no success
Developer
Posts: 16,750
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: change button bitmap

you could override navigationclick and navigationunclick, set a boolean status in there and call invalidate.
in the paint method you check for isClicked (your custom status) and paint something else, like the second bitmap, for example.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 34
Registered: ‎10-03-2012
My Device: 9300
My Carrier: cell c

Re: change button bitmap

Hello

Problem solved, by using function replace(). Thank u