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
Highlighted
Developer
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Some GUI Questions - Background Colour - Images - setBounds

Hi all,

 

I currently have a simple GUI with a button. [Code at the end] I am using the vertical layout manager to place items in the GUI

 

There are 3 things I would like to do

1) Change the background colour to black

2) Change the layout manager (or whatever it takes, if much) so I can definewhere to put things like buttons using co-ordinates - Something like: fieldManager.add(button1, 10, 20, 10, 12) [setBounds() in Swing I think it is]

3) Insert an image and use the co-ordinates above to place it somewhere or anything like this if possible?

 

I'm quite new to GUI's especially the BlackBerry API, so some guidance would be great!

(If it helps I have done the above in Swing before and i'm okay at Java, but obviously its all different with the BlackBerry API!

 

I just need a kick in the right direction

 

Thanks, 

Graham

 

 

import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.*;
class main extends UiApplication{
public static void main(String[] args){
main myApp = new main();
myApp.enterEventDispatcher();
}
public main(){
MainScreen screen = new MainScreen();
screen.setTitle(new LabelField("Application Title"));
VerticalFieldManager fieldManager = new VerticalFieldManager();

screen.add(fieldManager);
ButtonField button1 = new ButtonField("A Button");

screen.add(new SeparatorField());
screen.add(new RichTextField("A text Field"));
fieldManager.add(button1);
pushScreen(screen);

}

}

 


 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Some GUI Questions - Background Colour - Images - setBounds

I would recommend the search tool if you think it is likely that some has asked a similar question before - they probably have!

 

Q1. How can set the Background of the screen
http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=2706

 

Q2 and Q3: setPosition of BitmapField
http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=13202

 

 

Developer
Posts: 696
Registered: ‎12-02-2008
My Device: Not Specified

Re: Some GUI Questions - Background Colour - Images - setBounds

 
Hi,

 

Do you want to change the background color of your screen or ButtonField in both the case you will have to override the paintBackground method of Field class.

 

  ButtonField objButton = new ButtonField("A Button"){
           protected void paintBackground(Graphics graphics){
                   graphics.setBackgroundColor(Color.DARKGREEN);
                   graphics.clear();
                   super.paint(graphics);
            }
   }; 

you have to also override the getPreferedWidth() and getPreferedHeight() methods also to set the size of your buttonField.

 

Regarding your Second question, I think setPositionChild((Field field,int x,int y) of Manager will solve you problem.

 

Not able to understand you third question, If you want to add image in bacground you can again use setPaintground(Graphics g) of Screen and draw image using g.drawBitmap() of graphics.

 

Please let me know does this solved your problem or not.

 

Regards,
Rajat Gupta.
--------------------------------------------------------------------------------
If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish
Developer
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Re: Some GUI Questions - Background Colour - Images - setBounds

Hi there,

Thanks for your reply.

 

I need to change the background colour of the screen and not the button. I've done a search but dont really understand the code all that well. If someone can have a play around with my simple code above that would be great. 

 

As for the bitmapImage I just want to insert an image. Is there a more simple way to do this? - I've seen the post mentioned above and it was really long winded for what I'd have thought to be a simple thing to do?

 

for setBounds, I know its possible in Swing by field.setBounds(10,5,20,12) - Is there a similar thing in the blackberry API or example code? - I couldn't see anything 

 

Cheers, 

Graham

Developer
Posts: 696
Registered: ‎12-02-2008
My Device: Not Specified

Re: Some GUI Questions - Background Colour - Images - setBounds

Hi,

 

you can add bitmap on your screen as follows.

 

Bitmap objHeaderImage = Bitmap.getBitmapResource("360Header.png");

BitmapField objHeader   = new BitmapField(objHeaderImage);

 

add this BitmapField on to you screen.

 

you can set background color  of your screen by overriding protected void paintBackground(Graphics graphics)  of Field class

Regards,
Rajat Gupta.
--------------------------------------------------------------------------------
If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish
Developer
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Re: Some GUI Questions - Background Colour - Images - setBounds

HI,

 

I did this, but i just get the same before, no image comes up on the screen (Only the button). Where do I store the image? - in the src, bin or project folder? - I've done all to make sure I wasn't doing anything stupid and I get no comile errors

Thanks for your time

 

Bitmap objHeaderImage = Bitmap.getBitmapResource("123.png"); BitmapField objHeader = new BitmapField(objHeaderImage); ButtonField button1 = new ButtonField("CLICKTOSTART!!"); fieldManager.add(button1); fieldManager.add(objHeader);

 

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Some GUI Questions - Background Color - Images - eastbound

Your image must be added to the project, and given your filename, should be in the same directory as the referencing file.  To add the file, using Project --> Add File to Project, and select the appropriate suffix.
Developer
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Re: Some GUI Questions - Background Color - Images - eastbound

[ Edited ]

Thanks for your reply,

I've put it in the same directory as the .java file, and also everyother directory too to make sure but it still doesn't come up in the emulator.

Here is the code, it has to be something stupidly easy to fix..

 

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.*;
class home extends UiApplication{

public home(){
MainScreen screen = new MainScreen();
screen.setTitle(new LabelField("Title String"));
VerticalFieldManager fieldManager = new VerticalFieldManager();

screen.add(fieldManager);
Bitmap objHeaderImage = Bitmap.getBitmapResource("123.png");
BitmapField objHeader = new BitmapField(objHeaderImage);

ButtonField button1 = new ButtonField("CLICK HERE");


fieldManager.add(button1);
fieldManager.add(objHeader);
pushScreen(screen);

Listener button1Listener = new Listener();
button1.setChangeListener(button1Listener);

}

}

Is this the correct import statement to use?:

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

 

Message Edited by grahamb314 on 03-25-2009 07:36 PM
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Some GUI Questions - Background Color - Images - eastbound

Have you added it to your project as I said?

 

Put a breakpoint on the line

BitmapField objHeader...

and check to see if objHeaderImage is null.  If it is, then the project can't find the 123.png file.

Developer
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Re: Some GUI Questions - Background Color - Images - eastbound

Hi there,

It comes up with a null value, 

I did try to add it into the project like you aid but I dont have an option in "Project"

Ive read the API and it saysthe .png image needs to be added into the project just like you say

I'm using Eclipse Platform Version: 3.4.2  - THere doesn't sem to be an option to add to a project..

 

Thanks, 

Graham