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
New Developer
alejandroriedel
Posts: 9
Registered: ‎12-17-2011
My Device: Blackberry Torch
My Carrier: Personal
Accepted Solution

Refresh Field Label

Hi, I've been reading several posts across the forum but I haven't found anything that solves my issue.

 

I'm new to BB development and I'm having a problem I don't seem to be able to sort out.

 

I have a main class and a screen class. I have a button that launches a datepicker. When the datepicker is closed I need the label of the button to be refreshed with the selected value. How should I do that?

 

I tried with invalidate, creating a CustomButtonField that implements different onFocus, onUnFocus, and some other stuff but I guess I implemented them in a wrong way...

 

My two classes are these ones...

 



public class TestClass extends UiApplication
{

public static Calendar alarm1time = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));

 

public static void main(String[] args)
{

TestClass testClass = new TestClass ();
testClass.enterEventDispatcher();

}

 

public TestClass()
{

pushScreen(new TestClassScreen());

}

}

  


  

public final class TestClassScreen extends MainScreen
{


public TestClassScreen()
{

ButtonField alarm1 = new ButtonField("Alarm : " + TestClass.alarm1time.get(Calendar.HOUR_OF_DAY) + ":" + TestClass.alarm1time.get(Calendar.MINUTE) ,ButtonField.FOCUSABLE)

{

public boolean navigationClick (int status , int time)
{

datePicker(1);
return true;

}

};

setTitle("Test Alarm");
add(new RichTextField(" "));
add(alarm1 );

}

 

public void datePicker(final int alarmNumber)
{

UiApplication.getUiApplication().invokeLater(new Runnable()
{

public void run()
{

DateTimePicker datePicker = null;
datePicker = DateTimePicker.createInstance(TestClass.alarm1time, null, "HH:mm");
if(datePicker.doModal())
{

Calendar cal = datePicker.getDateTime();
TestClass.alarm1time = cal;

//Here I need the label to be refreshed, after the datePicker is Ok

}

}

});

}


}

  


  

Thanks,

Alex

 

Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Refresh Field Label

[ Edited ]

Welcome to the forums.

 

Here is one way:

 

public boolean navigationClick (int status , int time)
{

datePicker(1, this);
return true;

}

public void datePicker(final int alarmNumber, final ButtonField buttonToUpdate)
{

UiApplication.getUiApplication().invokeLater(new Runnable()
{

public void run()
{

DateTimePicker datePicker = null;
datePicker = DateTimePicker.createInstance(TestClass.alarm1time, null, "HH:mm");
if(datePicker.doModal())
{

Calendar cal = datePicker.getDateTime();
TestClass.alarm1time = cal;

buttonToUpdate.setLabel(....)}

}

});

}

 

A more usual way to be to have change listener listen for Button Clicks and do similar processing in there. 

 

Edit: Code corrected.

Please use plain text.
Developer
tklanilkumar
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800
My Carrier: idea

Re: Refresh Field Label

try with
alarm1.setLable("selected date value");
*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Please use plain text.
New Developer
alejandroriedel
Posts: 9
Registered: ‎12-17-2011
My Device: Blackberry Torch
My Carrier: Personal

Re: Refresh Field Label

[ Edited ]

Sorry, I replied instead of quick replying... I copied the answer where it belonged.

Please use plain text.
New Developer
alejandroriedel
Posts: 9
Registered: ‎12-17-2011
My Device: Blackberry Torch
My Carrier: Personal

Re: Refresh Field Label

[ Edited ]

peter_strange wrote:

Welcome to the forums.

 

Here is one way:

 

public boolean navigationClick (int status , int time)
{

datePicker(1, this);
return true;

}

public void datePicker(final int alarmNumber, final ButtonField buttonToUpdate)
{

UiApplication.getUiApplication().invokeLater(new Runnable()
{

public void run()
{

DateTimePicker datePicker = null;
datePicker = DateTimePicker.createInstance(TestClass.alarm1time, null, "HH:mm");
if(datePicker.doModal())
{

Calendar cal = datePicker.getDateTime();
TestClass.alarm1time = cal;

buttonToUpdate.setLabel(....)}

}

});

}

 

A more usual way to be to have change listener listen for Button Clicks and do similar processing in there. 

 

Edit: Code corrected.




Thanks for your answer! I'll give it a try right now...

But I have one more question... I've been reading about the change listeners but I really didn't understand how and where I should implement it.

Could you give me an example of implementation of a change listener with the code I posted?

Thanks a lot!
Alex

Please use plain text.
New Developer
alejandroriedel
Posts: 9
Registered: ‎12-17-2011
My Device: Blackberry Torch
My Carrier: Personal

Re: Refresh Field Label

[ Edited ]

tklanilkumar wrote:
try with
alarm1.setLable("selected date value");

I tried with the alarm1.setLable("selected date value") but it didn't work. Looks like the screen needs a way to know that it has to redraw the object

Please use plain text.
New Developer
alejandroriedel
Posts: 9
Registered: ‎12-17-2011
My Device: Blackberry Torch
My Carrier: Personal

Re: Refresh Field Label

The problem with the solution you are suggesting is that the Datepicker class is outside from the scope of the Screen class

Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Refresh Field Label

Is there a problem with my suggestion?

Please use plain text.
New Developer
alejandroriedel
Posts: 9
Registered: ‎12-17-2011
My Device: Blackberry Torch
My Carrier: Personal

Re: Refresh Field Label


peter_strange wrote:

Is there a problem with my suggestion?


Yes, the datePicker is outside from the Screen class so I can't access the ButtonField in order to refresh it.

 

I think I should go with the ChangeListener. How could I implement that?

 

Thanks

Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Refresh Field Label

"Yes, the datePicker is outside from the Screen class so I can't access the ButtonField in order to refresh it."

 

Not being rude, but this does not make sense.

 

In the code I gave, the Button is passed as a parameter to the datePicker method, so it has a reference to it.  Please review the code I suggested again. 

Please use plain text.