开发者

How to set each List's Command component so that there is no space between the containing Dialog and the Command's texts?

开发者 https://www.devze.com 2023-04-06 00:13 出处:网络
There is a Dialog which contains a List of Commands. When shown then the Label renderer of the public Component getListFocusComponent(List list) method and the Component of the public Component getLis

There is a Dialog which contains a List of Commands. When shown then the Label renderer of the public Component getListFocusComponent(List list) method and the Component of the public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) method of the derived class DefaultListCellRenderer are not occupying all of the width of the Dialog.

So how to make the Label and the selection-component occupy all the width of the containing Dialog ?

Here is the code :

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 == lis开发者_StackOverflow社区t.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;
    }
}


Ok , here it is :

lCmds.getSelectedStyle().setPadding(0, 0, 0, 0);
lCmds.getUnselectedStyle().setPadding(0, 0, 0, 0);
this.getContentPane().getSelectedStyle().setPadding(0, 0, 0, 0);
this.getContentPane().getUnselectedStyle().setPadding(0, 0, 0, 0);
this.addComponent(lCmds);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号