When adding a component to a form, I want the form to scroll down to make that newly added component visible.
I assumed that .scrollComponentToVisible()
was used for this, but I does not work for me.
If you run the sample code I provided bellow, you will notice that the component is added correctly and gets focus. However, it is still outside of the visible
area of the screen.
Pay attention to the row:
form.scrollComponentToVisible(cont4);
I guess this line is wrong? What should I use instead?
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();
}
}