Существует диалог, который содержит список команд. Когда отображается, средство визуализации Label метода public Component getListFocusComponent(List list)
и Component метода public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected)
производного класса DefaultListCellRenderer
не занимают всю ширину диалога.
Итак, как заставить Label и компонент selection занимать всю ширину содержащегося диалога?
Вот код:
public class CMenu extends Dialog {
private String disp;
private List lCmds;
public static Command retCommand;
public CMenu(Command[] cmds, String disposition)
{
super();
this.getStyle().setBorder(Border.createRoundBorder(10, 20));
afficheCmd(cmds);
this.setScrollableY(false);
disp = disposition;
}
private void afficheCmd(Command[] cmds)
{
lCmds = new List(cmds);
lCmds.setListCellRenderer(new TickerRenderer());
lCmds.setCommandList(true);
lCmds.setPreferredW(this.getPreferredW());
lCmds.setFixedSelection(List.FIXED_NONE_CYCLIC);
((DefaultListCellRenderer)lCmds.getRenderer()).setShowNumbers(false);
this.addComponent(lCmds);
lCmds.setSelectedIndex(0);
lCmds.requestFocus();
}
protected void onShow()
{
lCmds.setSelectedIndex(0);
lCmds.requestFocus();
}
public void affiche()
{
int top = Display.getInstance().getDisplayHeight() - lCmds.getHeight();
if (disp.equalsIgnoreCase("gauche"))
{
this.show(top, 0, 0, 100, false);
}
else
{
this.show(top, 0, 100, 0, false);
}
lCmds.requestFocus();
lCmds.setSelectedIndex(0);
}
protected void actionCommand(Command cmd)
{
retCommand = cmd;
this.dispose();
}
}
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)).getSelectedStyle().setBgTransparency(0);
(selectedRenderer.getListCellRendererComponent(list, value, index, isSelected)).getUnselectedStyle().setBgTransparency(0);
(selectedRenderer.getListCellRendererComponent(list, value, index, isSelected)).getSelectedStyle().setFgColor(16777215);
(selectedRenderer.getListCellRendererComponent(list, value, index, isSelected)).getUnselectedStyle().setFgColor(0);
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);
}
}
public Component getListFocusComponent(List list) {
Label label = new Label("");
label.getStyle().setBgTransparency(255);
label.getStyle().setBgColor(205388);
label.getStyle().setBorder(Border.createRoundBorder(5, 10, 16777215));
return label;
}
}