如果我根据命令数组创建一个List,并且列表中没有完全显示某些命令的文本,尽管List preferredWidth
设置为Form preferredWidth
,如何滚动他们?
非常感谢
答案 0 :(得分:1)
在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());
答案 1 :(得分:0)
尝试此代码,它会在单击“显示列表”命令的对话框中显示列表,并且最初也会启用自动收报机。下面的代码显示了当列表包含在对话框中时如何使用上面提到的类来查看列表中的自动收报机。
不要忘记将列表设为最终,以便可以在内部类中使用。
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
}
});
此代码在对话框中显示我的列表并最初启动代码。如果它没有帮助,请发布您的代码,我会尝试看到它。