1

If I create a List based on an array of Commands, and the text of some Commands are not entirely shown in the List, although the List preferredWidth is set to the Form preferredWidth, how to ticker them ?

Thank you very much

bharath
  • 14,283
  • 16
  • 57
  • 95

2 Answers2

1

Add the below class in your midlet class or create a new class file for that:

class TickerRenderer extends DefaultListCellRenderer {

private DefaultListCellRenderer selectedRenderer = new DefaultListCellRenderer(false);
private List parentList;

public TickerRenderer() {
    super(false);
}

public boolean animate() {
    if (parentList != null && parentList.getComponentForm() != null) {
        if (selectedRenderer.isTickerRunning()) {
            if (selectedRenderer.animate()) {
                parentList.repaint();
            }
        }
    }
    return super.animate();
}

public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
    if (isSelected) {
        selectedRenderer.getListCellRendererComponent(list, value, index, isSelected);

        // sometimes the list asks for a dummy selected value for size calculations and this might
        // break the tickering state
        if (index == list.getSelectedIndex()) {
            if (selectedRenderer.shouldTickerStart()) {
                if (!selectedRenderer.isTickerRunning()) {
                    parentList = list;
                    list.getComponentForm().registerAnimated(this);
                    selectedRenderer.startTicker(UIManager.getInstance().getLookAndFeel().getTickerSpeed(), true);
                }
            } else {
                if (selectedRenderer.isTickerRunning()) {
                    selectedRenderer.stopTicker();
                }
            }
        }
        return selectedRenderer;
    } else {
        return super.getListCellRendererComponent(list, value, index, isSelected);
    }
}
}

Use it like this:

List cmdList = new List(cmds);
cmdList.setListCellRenderer(new TickerRenderer());
Mital Pritmani
  • 4,880
  • 8
  • 38
  • 39
  • There is a problem : the first command in the List element should ticker immediately when the List is shown ; but when I debug the application then I see that the program goes to the else code of the "if (selectedRenderer.shouldTickerStart())". Although the text length of the first Command is large. The List is selected like this when shown the first time lCmds.requestFocus(); lCmds.setSelectedIndex(0); So how to enable the ticker immediately ? I tried to start the ticker if index == 0 but the ticker did not start. –  Jun 10 '11 at 08:14
  • Do one thing. Don't change the above code. If you are displaying the list lCmds on the click event of any button, then code it like this: `form.addComponent(lCmds); lCmds.setSelectedIndex(0); lCmds.requestFocus();` And if you are focusing on the list when the form gets displayed, then code it like this: `form.show(); lCmds.requestFocus(); lCmds.setSelectedIndex(0);` This is because the renderer is initialized when the form will get displayed and then we will let renderer know that the list is focused and which is the selected index. Try it. This works for me perfectly. – Mital Pritmani Jun 13 '11 at 13:17
  • I coded what you wrote but the ticker did not start. For explanation the List is contained in a Dialog , and the Dialog is shown when clicking a Menu. –  Jun 14 '11 at 06:14
0

Try this code, it will show list in dialog box on clicking "Show list" command and will also enable ticker initially. Below is the code which shows how to use the above mentioned class to see ticker in list when list is contained in dialog.

Don't forget to make your list final so that it can be used in inner classes.

form.addCommand(new Command("Show list") { // add command in form and override its actionPerformed method
     public void actionPerformed(ActionEvent evt) {
         Dialog d = new Dialog() {  // create an instance of dialog and make it an inner class so that you can override onShow() method and set focus on list when dialog gets initialized and also can set its index to ur preferred one (here it's 0)
             protected void onShow() { // overriding of onShow() method
                 list.requestFocus();  // set focus on list
                 list.setSelectedIndex(0);  // set selected index to 0
             }
         };
         d.addComponent(list);  // add list in dialog
         d.show();  // show dialog
    } 
 });

This code shows my list in dialog and starts ticker initially. If it doesn't help, post your code, i will try to see it.

Mital Pritmani
  • 4,880
  • 8
  • 38
  • 39
  • My code is a bit different from which you gave ( I use two java files ) , and it is difficult to re-arrange my code to follow what you wrote. So I give you below my code. –  Jun 16 '11 at 05:27
  • ok, i saw your code. Add this onShow() method after your afficheCmd(Command[] cmds) method in CMenu class.You are already extending Dialog class so you just need to override this method of Dialog class and add this two lines in that to start ticker initially. `protected void onShow() { lCmds.setSelectedIndex(0); lCmds.requestFocus(); }` – Mital Pritmani Jun 16 '11 at 07:58