当列表元素是命令时如何对其进行标记?

发布于 2024-11-15 04:14:57 字数 131 浏览 2 评论 0原文

如果我基于命令数组创建一个列表,并且某些命令的文本并未完全显示在列表中,尽管列表 preferredWidth 设置为表单 preferredWidth ,如何标记它们?

非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡の花香 2024-11-22 04:14:57

在您的 midlet 类中添加以下类或为此创建一个新的类文件:

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);
    }
}
}

像这样使用它:

List cmdList = new List(cmds);
cmdList.setListCellRenderer(new TickerRenderer());

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());
看海 2024-11-22 04:14:57

尝试此代码,单击“显示列表”命令时它将在对话框中显示列表,并且最初还会启用股票代码。下面的代码显示了当列表包含在对话框中时如何使用上述类来查看列表中的股票代码。

不要忘记将您的列表设为最终列表,以便可以在内部类中使用它。

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
    } 
 });

此代码在对话框中显示我的列表并最初启动股票。如果没有帮助,请发布您的代码,我会尝试查看它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文