2014-04-11 13 views
0

폼에 구성 요소를 추가 할 때 양식을 아래로 스크롤하여 새로 추가 된 구성 요소가 표시되게하고 싶습니다.LWUIT에서 지정된 구성 요소를 볼 수 있도록 양식을 스크롤하는 방법은 무엇입니까?

나는 이것을 위해 .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(); 
} 
} 
+0

@Shai_Almog : 나는 당신이이 일에 대한 답을 알 것이라고 생각) –

답변

0

클릭 할 때마다 추가 할 컨테이너가 동일한 경우 아래 샘플 코드와 같이 목록 및 사용자 지정 목록 셀 렌더러를 사용할 수 있습니다.

목록의 초점에 사용 추가 된 항목을 마지막으로 스크롤 할 수 있습니다 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; 
    } 
} 
} 
+0

내가 변수 높이 요구 사항을 추가하고 구성 요소로 목록을 사용할 수 없습니다. 또한 ContainerList는 "setScrollToSelected"를 지원하지 않습니다. 그러나 "invoideAndWait"에 "repaint"와 "requestFocus"를 넣어서 해결했습니다. 그것으로 문제가 해결되었습니다. "requestFocus"를 적용하기 전에 구성 요소가 화면에 인쇄되어야한다고 생각합니다. –