2012-01-02 103 views
1

使用LWUIT,我有一个Form由两部分组成:一个只读TextAreaButton在LWUIT中,如何以编程方式将“TextArea的滚动”设置回顶部?

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

TextAreaScrollbar,因为它包含一个长String,当用户移动DOWNTextAreaScrollbar向下移动直至到达String的末尾,然后Button得到聚焦,在TextArea的末尾,离开TextAreaScrollbar

我想要的是,点击Button时,滚动条会在TextAreaTop中返回到其原始状态,而不是在TextAreaBottom中。 我该怎么做?

+2

这篇文章可能会帮助你http://stackoverflow.com/questions/7484720/lwuit-scrolling – frayab 2012-01-02 16:10:03

+0

这解决了这个问题,谢谢!我会为任何有相同问题的人发布答案,以便为以后的搜索发布 – 2012-01-02 16:39:40

回答

2

以下是对有兴趣的人的解决方案。
通过使用自定义的TextArea

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

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

然后代码是下面的(而不是一个张贴在问题):

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。 “文本”应该是最终的或类的成员;)

+1

太棒了!我会尽力做到这一点! – Mun0n 2012-01-02 19:37:38

+0

scrollRectToVisible(0,0,10,10)也应该起作用,并且在我记得的情况下它是公开的 – 2012-01-05 13:06:34

相关问题