Question

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 ?

Was it helpful?

Solution

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 ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top