如何滚动表单以使指定的组件在LWUIT中可见?

时间:2014-04-11 13:48:06

标签: java java-me lwuit lwuit-form lwuit-textarea

向表单添加组件时,我希望表单向下滚动以使新添加的组件可见。

我认为.scrollComponentToVisible()被用于此,但我不适合我。

如果您运行我提供的示例代码,您会注意到组件已正确添加并获得焦点。但是,它仍然位于屏幕的visible区域之外。

注意行:

form.scrollComponentToVisible(cont4);

我猜这行错了?我该怎么用?

import javax.microedition.midlet.*;

import com.sun.lwuit.*;
import com.sun.lwuit.events.*;

public class ScrollTest extends MIDlet {

public void startApp() {

    Display.init(this);

    final Form form = new Form();
    form.getStyle().setBgColor(0xff0000);

    String text = "aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa" +
                "aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa";

    Container cont = new Container();
    cont.setFocusable(true);
    cont.getUnselectedStyle().setBgTransparency(0);
    cont.getSelectedStyle().setBgTransparency(100);
    TextArea area = new TextArea(text);
    area.setEditable(false);
    area.setFocusable(false);
    area.getUnselectedStyle().setBgTransparency(0);
    cont.addComponent(area);
    form.addComponent(cont);

    Container cont2 = new Container();
    cont2.setFocusable(true);
    cont2.getUnselectedStyle().setBgTransparency(0);
    cont2.getSelectedStyle().setBgTransparency(100);
    TextArea area2 = new TextArea(text);
    area2.setEditable(false);
    area2.setFocusable(false);
    area2.getUnselectedStyle().setBgTransparency(0);
    cont2.addComponent(area2);
    form.addComponent(cont2);

    Container cont3 = new Container();
    cont3.setFocusable(true);
    cont3.getUnselectedStyle().setBgTransparency(0);
    cont3.getSelectedStyle().setBgTransparency(100);
    TextArea area3 = new TextArea(text);
    area3.setEditable(false);
    area3.setFocusable(false);
    area3.getUnselectedStyle().setBgTransparency(0);
    cont3.addComponent(area3);
    form.addComponent(cont3);

    Command add = new Command("Add");
    form.addCommand(add);

    form.addCommandListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Container cont4 = new Container();
            cont4.setFocusable(true);
            cont4.getSelectedStyle().setBgColor(0xff0000);
            TextArea area4 = new TextArea("This should get focus and be visible when added");
            area4.setEditable(false);
            area4.setFocusable(false);
            cont4.getUnselectedStyle().setBgTransparency(0);
            cont4.getSelectedStyle().setBgTransparency(100);
            cont4.addComponent(area4);
            form.addComponent(cont4);
            form.repaint();

            cont4.requestFocus();
            form.scrollComponentToVisible(cont4);

        }
    });

    form.show();
}

public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void actionPerformed(ActionEvent ae) {
    notifyDestroyed();
}
}

1 个答案:

答案 0 :(得分:0)

如果要在每次单击时添加的容器相同,则可以使用List和自定义列表单元格渲染器,如下面的示例代码所示。

列表 setScrollToSelected 可用于焦点和滚动到上次添加的项目。

public class ScrollTest extends MIDlet {

private List myList;
private ListModel mListModel;
private int i = 0;

public void startApp() {
    Display.init(this);

    Form form = new Form();
    form.setLayout(new BorderLayout());
    form.setScrollableY(false);
    form.getStyle().setBgColor(0xff0000);

    myList = new List();
    CustomListRenderer mRenderer = new CustomListRenderer();
    myList.setListCellRenderer(mRenderer);
    mListModel = new DefaultListModel();
    myList.setModel(mListModel);
    myList.setFixedSelection(List.FIXED_NONE_ONE_ELEMENT_MARGIN_FROM_EDGE);
    myList.setSmoothScrolling(true);
    myList.setScrollToSelected(true);
    form.addComponent(BorderLayout.CENTER, myList);

    Command add = new Command("Add");
    form.addCommand(add);

    form.addCommandListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            mListModel.addItem(" Item #" + (i++));
            myList.setSelectedIndex(mListModel.getSize() - 1);
            myList.setScrollToSelected(true);
        }
    });

    form.show();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

private class CustomListRenderer extends Container implements
        ListCellRenderer {
    TextArea area;
    Label focus = new Label();

    public CustomListRenderer() {
        this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        area = new TextArea();
        area.setEditable(false);
        area.setFocusable(false);
        this.addComponent(area);
        this.getStyle().setBgColor(0xff0000);
        this.getSelectedStyle().setBgColor(0xff6600);
    }

    public Component getListCellRendererComponent(List list, Object value,
            int index, boolean isSelected) {
        area.setText(value.toString());
        return this;
    }

    public Component getListFocusComponent(List arg0) {
        focus.getStyle().setBgColor(0xff6600);
        return focus;
    }
}
}