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
Developer
mlisaoverdrive
Posts: 179
Registered: ‎09-11-2009
My Device: Not Specified
Accepted Solution

Credentials Screen: Extend PopupScreen or Dialog?

I'm a very new Java/BB developer writing a browser ApplicationMenuItem invoked app that does the following

  1. pushes a screen that collects creds from the user
  2. checks creds against a remote URI
  3. if they are valid, perform one action
  4. if not, perform another

Any POSTs to the remote URI require credentials.

 

I've written implementations of both PopupScreen and  Dialog, and I'm having problems with both.  The Dialog screen I tried is easy to implement and inherits a lot of very useful methods, but I can't capture button press values from the style I'm using (D_OK_CANCEL) even when using FieldChangeListener.  The PopupScreen provides the exact layout I want, but, since I'm still very new at this, I'm having difficulty creating custom buttons and methods that return the values I need the way Dialog does.

 

My questions are:

  1. Is it possible to use FieldChangeListener and get the button pressed values from the D_OK_CANCEL dialog style?
  2. How do I create ButtonFields and the methods that will return a value when clicked...similar to the various ask() methods in Dialog?

I'm working on my second PopupScreen attempt, so I have code if it is needed.  thanks in advance...

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Credentials Screen: Extend PopupScreen or Dialog?

[ Edited ]

I would extend PopupScreen

 

Create a vertical field manager, add your fields and buttons to it, then add it to the screen.

 

Here is an example of a button and listener (completely untested):

 

Create the listener:

 

    class ButtonListener implements FieldChangeListener
    {
        ButtonListener()
        {
        }
       
        public void fieldChanged(Field field,int context)       
        {
            if (field == m_submit){
                onSubmit();
            }
             if (field == m_cancel){
                onCancel();
            }
            
        }
    }

 

Create the button:

 

ButtonField m_submit = new ButtonField("Ok",CONSUME_CLICK)

 

Add the listener:

 

       m_submit.setChangeListener(new ButtonListener());

 

Please use plain text.
Developer
mlisaoverdrive
Posts: 179
Registered: ‎09-11-2009
My Device: Not Specified

Re: Credentials Screen: Extend PopupScreen or Dialog?

 


RexDoug wrote:

I would extend PopupScreen

 

Create a vertical field manager, add your fields and buttons to it, then add it to the screen.

 

Here is an example of a button and listener (completely untested):

 

Create the listener:

 

    class ButtonListener implements FieldChangeListener
    {
        ButtonListener()
        {
        }
       
        public void fieldChanged(Field field,int context)       
        {
            if (field == m_submit){
                onSubmit();
            }
             if (field == m_cancel){
                onCancel();
            }
            
        }
    }

 

Create the button:

 

ButtonField m_submit = new ButtonField("Ok",CONSUME_CLICK)

 

Add the listener:

 

       m_submit.setChangeListener(new ButtonListener());

 


 

 

Actually, I have the code for the Buttons and the Listener...though I didn't think of doing separate methods depending on the ButtonField consumed, very neat...but I would have to attach values to those buttons so I can implement an public int ask() or public int prompt() method to do something based on the returned int and I'm really new at this.

 

I'll work with the PopupScreen, but, in case I want something faster, is there a way to capture the return values from a default D_OK_CANCEL Dialog?

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Credentials Screen: Extend PopupScreen or Dialog?

[ Edited ]

Something like this?

 

int choice = Dialog.ask(Dialog.D_YES_NO, "Do you want to abort the download?", Dialog.NO);
if (choice == Dialog.YES) {
    m_Text.setText("Aborting, please wait...");
    // do something here...
}
        

 

Please use plain text.
Developer
mlisaoverdrive
Posts: 179
Registered: ‎09-11-2009
My Device: Not Specified

Re: Credentials Screen: Extend PopupScreen or Dialog?

 


RexDoug wrote:

Something like this?

 

int choice = Dialog.ask(Dialog.D_YES_NO, "Do you want to abort the download?", Dialog.NO);
if (choice == Dialog.YES) {
    m_Text.setText("Aborting, please wait...");
    // do something here...
}
        

 


 

 

Yes.  But I'd be extending dialog as I need to add a username and password field to it.  Which leads to my problem...how to implement an ask() and return the default buttons?

 

Would it be something like this?

 

 

public class credsScreen extends Dialog
{
private PasswordEditField password;
private int pfsize = 30;
private EditField username;
private int unsize = 30;

public credsScreen()
{
super(Dialog.D_OK_CANCEL, "Authenticate", 0, Bitmap.getPredefinedStyle(Bitmap.getDefaultType());
username = new EditField("username: ", "", unsize, EditField.EDITABLE);
password = new PasswordEditField("password: ","", pfsize, PasswordEditField.EDITABLE);
VerticalFieldManager vfm = new VerticalFieldManager();
}

public static int ask()
{
// what code if any would go here to return OK/CANCEL values?
}

 

 


Do I need code for that ask() method?  Or just the UiApplication.getUiApplication().pushScreen(this);?

Please use plain text.
Developer
mlisaoverdrive
Posts: 179
Registered: ‎09-11-2009
My Device: Not Specified

Re: Credentials Screen: Extend PopupScreen or Dialog?

Or, to put it differently, if I'm creating my own no argument ask() method in my custom Dialog, do I need to also implement a FieldChange listener  for that ask() method to handle the actions after the OK or CANCEL buttons are clicked?

 

Or is it something as simple as your code below.  Something like,

 

 

public static int ask()
{
int choice = 0;
if (choice == Dialog.OK)
{
// do OK code here
} else
{
// do CANCEL code here
}
}

 

 

Please use plain text.
Developer
mlisaoverdrive
Posts: 179
Registered: ‎09-11-2009
My Device: Not Specified

Re: Credentials Screen: Extend PopupScreen or Dialog?

Or what I tried yesterday:

 

 

public static int ask()

{

UiApplication.getUiApplication().pushScreen(this);

return getSelectedValue();

}

 

I realized just now that I forgot to put in a System.out.println("return value: " int); to see if it worked, so I'm going to try that now.  Feel free to chime in...

 

Please use plain text.
Developer
mlisaoverdrive
Posts: 179
Registered: ‎09-11-2009
My Device: Not Specified

Re: Credentials Screen: Extend PopupScreen or Dialog?

 


mlisaoverdrive wrote:

Or what I tried yesterday:

 

 

public static int ask()

{

UiApplication.getUiApplication().pushScreen(this);

return getSelectedValue();

}

 

I realized just now that I forgot to put in a System.out.println("return value: " int); to see if it worked, so I'm going to try that now.  Feel free to chime in...

 


 

Okay...the System.out.println(); did not println, so I do need to implement a listener for the default D_OK_CANCEL buttons.  Is that even possible?  Or am I way off track in my conclusion?

 

Please use plain text.
Developer
mlisaoverdrive
Posts: 179
Registered: ‎09-11-2009
My Device: Not Specified

Re: Credentials Screen: Extend PopupScreen or Dialog?

Update:  (Sorry for so many posts in a row).  I believe I figured out how to capture button clicks, but found a different problem.

 

Here's the code snippet for the class constructor that extends Dialog

 

 

public credentialsScreen()
{
super(Dialog.D_OK_CANCEL, "Authenticate", 0, Bitmap.getPredefinedBitmap(Bitmap.getDefaultType()), Field.FOCUSABLE);
username = new EditField("username: ", "", unsize, EditField.EDITABLE);
password = new PasswordEditField("password: ","", pfsize, PasswordEditField.EDITABLE);
VerticalFieldManager vfm = new VerticalFieldManager();
setChangeListener(this);
vfm.add(username);
vfm.add(password);
add(vfm);
}

 

Here's the fieldChanged listener for that constructor

 

 

public void fieldChanged(Field field, int context)
   {
       context = getSelectedValue();
       switch(context)
       {
           case Dialog.OK: System.out.println("Context: " + context);
           case Dialog.CANCEL: System.out.println("Context: " + context);
       }
   }

 

The problem I'm having now is that context is the same value (-1) regardless of whether OK or CANCEL is pressed.

 

 

 

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Credentials Screen: Extend PopupScreen or Dialog?

[ Edited ]

I think you are making this about 3x more complicated than necessary.

 

The best advice I've got was about 5 posts ago :smileyhappy:

 

1. Create your screen class (for a first cut, how about just extending MainScreen?  It's easier)

2. Add your input fields

3. Add your buttons (Submit and Cancel) with listener

4. Add your "handler" methods (onSubmit and onCancel)

5. Look for your butttons in the listener, and dispatch to the correct handler

 

...and you are done (at least, with the UI portion).

 

 

 

 

 

 

Please use plain text.