Using LWUIT, I have a Form with two components: a read-only TextArea and a Button:

TextArea text = new TextArea("blah blah blah blah blah blah blah blah blah ...");
text.setEditable(false);
form.addComponent(text);

Button button = new Button("Press Me !");
button.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
          // DESIRED CODE IS HERE ...
     }
});
form.addComponent(button);

The TextArea has a Scrollbar because it contains a long String, when the user moves DOWN the TextArea's Scrollbar moves down until it reaches the end of the String, then the Button get focused leaving the TextArea's Scrollbar at the end of the TextArea.

I want that, when the Button is clicked, the scrollbar returns back to its original state in the Top of the TextArea instead of being in the Bottom of the TextArea. How could I do this ?

有帮助吗?

解决方案

Here's the solution for those who are interested.
by using a custom TextArea

public class CustomTextArea extends TextArea {
    public TextAreaScrollControlled(String text) {
        super(text);
    }

    public void resetScrollBackToTop() {
        setScrollY(0);
    }
}

Then the code is the following (instead of the one posted in the question):

CustomTextArea text = new CustomTextArea("blah blah blah blah blah blah ...");
text.setEditable(false);
addComponent(text);

Button button = new Button("Press Me !");
button.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
          text.resetScrollBackToTop(); // SOLUTION
     }
});
form.addComponent(button);

PS. "text" should be final or a member of the class ;)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top