Skip to main content

Ticker Functionality in Labels

12 replies [Last post]
ckurtm
Offline
Joined: 2008-03-12
Points: 0

Hi,
I noticed the setTickerEnabled() method for Labels, wondering how u use this, or is it that i'm implementing my renderer the wrong way, here is wat i'm trying to achieve. I have a list of 'Persons' in a List cell renderer that lists the name and the description, so i have a renderer as shown below:

class ResultsRenderer extends Container implements ListCellRenderer {

private Label name = new Label("");
private Label description = new Label("");
private Label focus = new Label();

public ResultsRenderer() {
addComponent(name);
addComponet(description);
}

public Component getListCellRendererComponent(List list, Object value, int index, boolean isselected) {
Contact person = (Contact) value;
name.setText(person.getName());
description.setText(person.getDesc());
return this;
}

public Component getListFocusComponent(List list) {
return focus;
}

my question is, why is it the name label or description labels in the list both to do not 'tick' if the name or description is wider than the width of the list?

any help in the right direction? thanks in advance

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
nic_r
Offline
Joined: 2008-07-11
Points: 0

Hi Chen,

Well spotted - that worked, thanks.

One last thing though - when the list loses focus the previously selected item is not redrawn with zero text shift, ie. the text can be in any position in the cell.

This would seem to imply that the getListCellRendererComponent method is not called with the item not selected before the focus is lost ?

Any way round this ?

Thanks,

Nic

Shai Almog

Hi Nic,
the selected boolean argument to the list will never be "reverted"
when the list loses focus. However, within the renderer code you can
supplement the list condition with:
if(isSelected && list.hasFocus()) {
....
}

This will allow you to reset the ticker for both cases and still keep
the list highlight code when the list doesn't have focus (a common
use case).

Thanks.

> Hi Chen,
>
> Well spotted - that worked, thanks.
>
> One last thing though - when the list loses focus the previously
> selected item is not redrawn with zero text shift, ie. the text can
> be in any position in the cell.
>
> This would seem to imply that the getListCellRendererComponent
> method is not called with the item not selected before the focus is
> lost ?
>
> Any way round this ?
>
> Thanks,
>
> Nic
> [Message sent by forum member 'nic_r' (nic_r)]
>
> http://forums.java.net/jive/thread.jspa?messageID=290309
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>

Shai Almog
http://lwuit.blogspot.com/

[att1.html]

chen

Hi,
Cell Renderer's are special case since the renderer doesn't hold any
state it is just a rubber stamp to be used by the List.
To enable this functionality in the List you can use the
Label.setShiftText method(which is exactly what the Ticker functionality
is doing).
Simply increase/decrease a index in the List animation and set this
index to the Renderer when selected.

Regards,
Chen

lwuit-users@mobileandembedded.org wrote:
> Hi,
> I noticed the [i]setTickerEnabled()[/i] method for Labels, wondering how u use this, or is it that i'm implementing my renderer the wrong way, here is wat i'm trying to achieve. I have a list of names, with a List cell renderer that describes lists the name and the description, so i have a renderer as shown below:
>
> [i]class ResultsRenderer extends Container implements ListCellRenderer {
>
> private Label name = new Label("");
> private Label description = new Label("");
> private Label focus = new Label();
>
> public ResultsRenderer() {
> addComponent(name);
> addComponet(description);
> }
>
> public Component getListCellRendererComponent(List list, Object value, int index, boolean isselected) {
> Contact person = (Contact) value;
> name.setText(person.getName());
> description.setText(person.getDesc());
> return this;
> }
>
> public Component getListFocusComponent(List list) {
> return focus;
> }[/i]
> [Message sent by forum member 'ckurtm' (ckurtm)]
>
> http://forums.java.net/jive/thread.jspa?messageID=288344
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

ckurtm
Offline
Joined: 2008-03-12
Points: 0

I'm sorry but i think i dont get what you mean, i tried implementing what Shai showed from the ticker blog post, could you please elaborate on this a bit more :-)

chen

Shai's post was before we added the ticker functionality.
If you will look at the code post
http://lwuit.blogspot.com/2008/06/implementing-selected-item-ticker-in.html
The Ticker renderer code should look like this now:

*class* TickerRenderer *extends* DefaultListCellRenderer *{*
*private* *int* position;
*public* *void* resetPosition*(**)* *{*
position = 0;
*}*
*public* *void* incrementPosition*(**)* *{*
position += 5;
*}*

*public* Component getListCellRendererComponent*(*List list, Object value, *int* index, *boolean* isSelected*)* *{*

**Label** l = *(Label)super*.getListCellRendererComponent*(*list, value, index, isSelected*)*;
*if**(*hasFocus*(**)**)* *{*
l.setShiftText(position);
}else{
l.setShiftText(0);
}
*return* l;
*}*
*}*

Regards,
Chen

lwuit-users@mobileandembedded.org wrote:
> I'm sorry but i think i dont get what you mean, i tried implementing what Shai showed from the ticker blog post, could you please elaborate on this a bit more :-)
> [Message sent by forum member 'ckurtm' (ckurtm)]
>
> http://forums.java.net/jive/thread.jspa?messageID=288641
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

[att1.html]

nic_r
Offline
Joined: 2008-07-11
Points: 0

Hi,

I'm trying to do moreless the same thing as the OP, but keep getting the error message at runtime 'This method cannot be called before the Label is fully initialized at com.sun.lwuit.Label.startTicker(Label.java:351)', but this is after executing this code :

Label invoicesTitle = new Label("Invoices (AP)");
invoicesTitle.startTicker(100, true);

ie. just an ordinary label at the moment, not labels in a list.

How much more initialised can the label be ?! :o)

Also, does the argument for the setShiftText have to be incremented, as indicated above, or does the ticker functionality handle that for you (I presume so). The documentation is not crystal clear here.

Thanks,

Nic

chen

Hi,
The Label needs to be belongs to a Form before you start the ticker,
since we need to register the tickering animation on the Form.
The Ticker calls to setShiftText, so no need to call to this method.

Regards,
Chen

lwuit-users@mobileandembedded.org wrote:
> Hi,
>
> I'm trying to do moreless the same thing as the OP, but keep getting the error message at runtime 'This method cannot be called before the Label is fully initialized at com.sun.lwuit.Label.startTicker(Label.java:351)', but this is after executing this code :
>
> Label invoicesTitle = new Label("Invoices (AP)");
> invoicesTitle.startTicker(100, true);
>
> ie. just an ordinary label at the moment, not labels in a list.
>
> How much more initialised can the label be ?! :o)
>
> Also, does the argument for the setShiftText have to be incremented, as indicated above, or does the ticker functionality handle that for you (I presume so). The documentation is not crystal clear here.
>
> Thanks,
>
> Nic
> [Message sent by forum member 'nic_r' (nic_r)]
>
> http://forums.java.net/jive/thread.jspa?messageID=289808
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

nic_r
Offline
Joined: 2008-07-11
Points: 0

Thanks for the answer - that now works !

However, I'm also trying to use a 'ticker' in a list, using the shiftText method you described earlier, but getting some very strange results. I'm trying to get text in a label to scroll if it is bigger than the label, if that line in the list is highlighted, but sometimes text in other lines scrolls as well ! Here is the code:

public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected)
{
TableEntry entry = (TableEntry) value;
for (int i = 0; i < entry.getNumValues(); i++)
{
String text = entry.getValue(i);
values[i].setText(text);
boolean tickerNeeded = false;
if (entry.getValueLength(i) > columnWidths[i])
tickerNeeded = true;

if (isSelected) // selected in the list

{
setFocus(true);
getStyle().setBgTransparency(0);
if (tickerNeeded) // Text is wider than the space allocated

values[i].setShiftText(position % (tableFont.stringWidth(text)));
}
else
{
setFocus(false);
getStyle().setBgTransparency(200);
if (tickerNeeded)
values[i].setShiftText(0);
}
}
return this;
}

public Component getListFocusComponent(List list)
{
return null;
}

The position variable is incremented in an overidden animate method. The TableEntry class is basically an array of text strings, the 'values' array is an array of labels which I have set to a preferred size.

Maybe I'm confusing some of the terms (I'm relatively new to GUI programming) ? I'm assuming that 'isSelected' means the particular line in the list that is being rendered is highlighted, and that a list has 'focus' if we are traversing up and down it.

Any thoughts ?

Thanks,

Nic

chen

Hi,
Where is values array is defined? my guess is that values[] is shared
between different items.

Regards,
Chen

lwuit-users@mobileandembedded.org wrote:
> Thanks for the answer - that now works !
>
> However, I'm also trying to use a 'ticker' in a list, using the shiftText method you described earlier, but getting some very strange results. I'm trying to get text in a label to scroll if it is bigger than the label, if that line in the list is highlighted, but sometimes text in other lines scrolls as well ! Here is the code:
>
> public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected)
> {
> TableEntry entry = (TableEntry) value;
> for (int i = 0; i < entry.getNumValues(); i++)
> {
> String text = entry.getValue(i);
> values[i].setText(text);
> boolean tickerNeeded = false;
> if (entry.getValueLength(i) > columnWidths[i])
> tickerNeeded = true;
>
> if (isSelected) // selected in the list
>
> {
> setFocus(true);
> getStyle().setBgTransparency(0);
> if (tickerNeeded) // Text is wider than the space allocated
>
> values[i].setShiftText(position % (tableFont.stringWidth(text)));
> }
> else
> {
> setFocus(false);
> getStyle().setBgTransparency(200);
> if (tickerNeeded)
> values[i].setShiftText(0);
> }
> }
> return this;
> }
>
> public Component getListFocusComponent(List list)
> {
> return null;
> }
>
> The position variable is incremented in an overidden animate method. The TableEntry class is basically an array of text strings, the 'values' array is an array of labels which I have set to a preferred size.
>
> Maybe I'm confusing some of the terms (I'm relatively new to GUI programming) ? I'm assuming that 'isSelected' means the particular line in the list that is being rendered is highlighted, and that a list has 'focus' if we are traversing up and down it.
>
> Any thoughts ?
>
> Thanks,
>
> Nic
> [Message sent by forum member 'nic_r' (nic_r)]
>
> http://forums.java.net/jive/thread.jspa?messageID=290020
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

nic_r
Offline
Joined: 2008-07-11
Points: 0

Hi Chen,

'values' is defined just above - the rest of the class looks like this:

class TickerTableRenderer extends Container implements ListCellRenderer
{

private int position = 0;
private Label[] values = new Label[columnWidths.length];

public TickerTableRenderer()
{
setLayout(new BoxLayout(BoxLayout.X_AXIS));

for (int i = 0; i < columnWidths.length; i++)
{
values[i] = addTableLabel(this, null, columnWidths[i], Label.RIGHT); // Put some labels in the container
}

}

public void resetPosition()
{
position = 0;
}

public void incrementPosition()
{
position -= 5;
}

So basically the constructor defines enough labels in the array 'values' to display one table row, one label for each column.

It could well be my lack of understanding as to how list cell renderers are supposed to work, so please let me know if I'm doing something stupid !

Thanks,

Nic

nic_r
Offline
Joined: 2008-07-11
Points: 0

I'll try to post the code again:

class TickerTableRenderer extends Container implements ListCellRenderer
{

private int position = 0;
private Label[] values = new Label[columnWidths.length];

public TickerTableRenderer()
{
setLayout(new BoxLayout(BoxLayout.X_AXIS));

for (int i = 0; i < columnWidths.length; i++)
{
values (i)
=
addTableLabel(this, null, columnWidths[i], Label.RIGHT); // Put some labels in the container
}

}

public void resetPosition()
{
position = 0;
}

public void incrementPosition()
{
position -= 5;
}

chen

Hi,
Try to remove the condition

if (tickerNeeded) //remove this line
values[i].setShiftText(0);//leave this line as is

always set the shift text to zero if not selected.

I assume what you want is only the focus to start ticking, am I right?

Regards,
Chen

lwuit-users@mobileandembedded.org wrote:
> I'll try to post the code again:
>
> class TickerTableRenderer extends Container implements ListCellRenderer
> {
>
> private int position = 0;
> private Label[] values = new Label[columnWidths.length];
>
> public TickerTableRenderer()
> {
> setLayout(new BoxLayout(BoxLayout.X_AXIS));
>
> for (int i = 0; i < columnWidths.length; i++)
> {
> values[i] = addTableLabel(this, null, columnWidths[i], Label.RIGHT); // Put some labels in the container
> }
>
> }
>
> public void resetPosition()
> {
> position = 0;
> }
>
> public void incrementPosition()
> {
> position -= 5;
> }
> [Message sent by forum member 'nic_r' (nic_r)]
>
> http://forums.java.net/jive/thread.jspa?messageID=290049
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net