3. Let’s Sayin’ Somethin’

In the previous lesson, you’ve already create your first working BlackBerry apps right? After you’ve finished your celebration, now it’s time for us to move on to the next step.

Basically, there’s three basic things all programs in the world must be able to do.

  1. Take input from user
  2. Process the input
  3. Show output to user

Of course there’s some other advanced things, but we shouldn’t worry about it for the time being. Let’s take a look first on where your code will go.

BB 3a Package Explorer

See that ‘Package Explorer’ tab over there? That’s where your files go. You can browse your files there, if you want to jump right into any files, that’s your place. Move your cursor over our ‘Hello World’ project, then you will see a small triangle will pop out on the left of it. Click on the small triangle, or just double click the ‘Hello World’, to open all the files and folder associated with it.

BB 3b Package Explorer explored

Here’s what you’ll see after you open most of it.

  • src folder is where your code goes. It’s no brainer that this will be the folder that will be the most often to be explored.
  • res folder is where your images, sounds, videos, and any supporting files for your application that is loaded on the run-time goes.
  • JRE system library is something sacred that we wouldn’t want to go somewhere near right now. 🙂
  • deliverables is where your executable / compiled code goes. We will touch this later on. But you need files in here if you want to publish your apps on the web (not necessarily must be BlackBerry App World) so that people can browse, download, and install it on their BlackBerry device.

Now, let’s take a look on the src folder.

  • MyApp.java is one of the files that BlackBerry plugins automatically created for you. This is like the ‘main entry point’ of your app.
  • MyScreen.java is another file that’s automatically created. This is where mostly of your code goes.

Double click on the MyScreen.java to open it.

BB 3c MyScreen java

Do you remember at the previous chapter where you see “My Title” on the top of your app screen? Now you can see where the “My Title” is set. And you will also be safe to assume that all of your codes will be goes there, for now. Before, or after the setTitle command (let’s call it ‘function’).

Now, what to do? Let’s start from the most easiest thing to do: output.

To show the output, we will also use the most basic component the BlackBerry environment (and almost all programming environment) has offered: Label.

Label

A label is.. a label. That’s where your ‘plain text’ going and rolling on the BlackBerry apps screen. Here’s how you add a label on the screen:

  1. You define an object of Label type.
  2. You put the text you want on that object.
  3. You add the object onto the screen.

That’s it. Pretty simple, huh? Then, how is the actual code?

To define an object, you type the object’s type (let’s call it ‘class’) first, and then followed by the object’s name. The label’s class is happened to have an intuitive name: LabelField. Yep, you have to remember it down to the where the capital letters are. So, for example, if I want to have a label object named ‘cooltext’, all I have to do is write something like this:

LabelField cooltext;

In here, you define the cooltext on the memory. Don’t forget the semi colon. You’ll need it to terminate almost every instance of command here in Java. Next thing to do is ‘acquire’ the memory for it, by using this:

cooltext = new LabelField();

Don’t forget the opening and closing bracket too. After that, let’s put something inside the cooltext.

cooltext.setText("Hello World!");

Pay attention to the lowercase of ‘s’ and uppercase of ‘T’ of the setText. Actually, maybe you don’t need to remember a thing. You can use automatic completion. But I will tell you about that later. The last thing to do is to add the cooltext to the screen by write something like this:

add(cooltext);

And you’re done…! Sort of. The problem is, you’ll need to ‘link’ to the appropriate command library so that Eclipse will know what does “LabelField” means. You can add this quite long code to the beginning of the file:

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

So, the resulting code will be something like this:

package mypackage;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.*;
/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
 /**
 * Creates a new MyScreen object
 */

 public MyScreen()
 { 
 // Set the displayed title of the screen 
 setTitle("MyTitle");

 LabelField cooltext;
 cooltext = new LabelField();
 cooltext.setText("Hello World!");
 add(cooltext);
 }
}

The text in blue is what you need to add to the code. After you’ve finished adding the code, compile and run the code by clicking on Run menu –> Run As –> 1 BlackBerry Simulator.

Tips: You don’t need to close the BlackBerry Simulator. It will return to the home screen everytime you click on the Run As BlackBerry Simulator. You just need to locate the program icon and run it again.

BB 2k BB HelloWorld icon

BB 3d Hello World with label

There’s your Hello World label goes! Compare it with the previous blank app screen on the previous chapter.

There’s another way to write the code. You can write niftier and more consistent code by making some changes like these:

  1. You can define the object outside the myScreen block program (we will call it ‘method’, which is similar to ‘function’ actually). If you define the object outside the myScreen method, you’ll be able to use it and reuse its contents on other method in the myScreen class (yes, you’re actually creating a class right now). But you need to add either ‘private’ or ‘public’ prefix on front of it. Just use ‘private’ if you’re unsure for now.
  2. Or you can define the object inside the myScreen block, and you can combine the object definition with memory request in one line of code.

So, if you’re choosing the first path, the resulting code will be like this:

package mypackage;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.*;
/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
 /**
 * Creates a new MyScreen object
 */

 private LabelField cooltext;

 public MyScreen()
 { 
 // Set the displayed title of the screen 
 setTitle("MyTitle");

 cooltext = new LabelField();
 cooltext.setText("Hello World!");
 add(cooltext);
 }
}

Or, if you choose to go for the second way, the resulting code would be like this:

package mypackage;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.*;
/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
 /**
 * Creates a new MyScreen object
 */

 public MyScreen()
 { 
 // Set the displayed title of the screen 
 setTitle("MyTitle");

 LabelField cooltext = new LabelField();
 cooltext.setText("Hello World!");
 add(cooltext);
 }
}

So you can see, there’s more than one way to write some code that works in similar way, or sometimes, exactly the same. It depends on your needs and what suits you. So, for the next chapter, we will talk about how to get input from the user.

Did you feel the tutorial clear enough for you? Is there anything wrong in the article? Do you know anything I can do to improve this article? Feel free to use the comments area below to share your thought.


Next: 4. Did You Say Something?
Previous: 2. Prepare New Application

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s