Skip to main content

Auto scrolling issue

4 replies [Last post]
pakmee
Offline
Joined: 2008-09-01

Hello I know this has been addressed before but the previous thread is still unanswered and no proper solutution was provided for it.

Basically, I am trying to have a form that has a container set to center with borderlayout and a textfield also added to that form with borderlayout south.

Now, I want to be able to add components on the go to that container. I.e when I fill in the textfield with some text and press a firebutton, i want the component to be added to the container.

I tried doing that but for some reason, when I say added the 11th textarea to the container, the container wont scroll to the last one I added. i assume this is normal since it is the normal behaviour. I read the other posts and SHAI suggested to extend the container and call the scrollcomponenttovisible method when i add a new component and to make sure that the parent form scrolling is set to false and that the container's scrolling is set to true.

I tried that but it won't work. Anyone had any issue with this. I also tried using a list but lists have the same row heights which is kind of restrictive for me. Any help would be greatly appreciated.

Thanks.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pakmee
Offline
Joined: 2008-09-01

You are a star! I was about to start writing my own container. Looks like a little digging does help sometimes. At least I now know why it was not working.

vprise
Offline
Joined: 2003-11-07

That suggestion was for something far more complex. Just use component.requestFocus() should do the trick.

pakmee
Offline
Joined: 2008-09-01

Hi there, thanks for the reply but that doesn't seem to do anything aprt from the fact that the new element added would get focus buts that about it. The container won't scroll unfortunately....any other suggestions?

ionutianasi
Offline
Joined: 2008-11-10

Did some digging, here's what I came up with (hoping this is what you are looking for):

- on the container you must call setSmoothScrolling(false) ;
- the scrollComponentToVisible does indeed the trick, the issue is to call it at the right time; calling it after container's paint works for me; calling it sooner won't work since the TextArea's getBounds() returns a (0,0,0,0) rectangle which will always be in the view ( see Conainer.scrollComponentToVisible and Component.scrollRectToVisible source)

There might be a simpler way to fix this, I just could not find it...
Anyway, here's my test app:

final Form f = new Form("chat-like form");
f.setLayout(new BorderLayout());
f.setScrollable(false);
final TextField tf=new TextField();

final Container c=new Container(){
Component bottomComponent=null;

public void addComponent(Component c){
super.addComponent(c);
bottomComponent=c;
}

public void paint(Graphics g){
super.paint(g);

if(bottomComponent==null) return;
scrollComponentToVisible(bottomComponent);
bottomComponent=null;
}
};
c.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
c.setSmoothScrolling(false);
c.setScrollableY(true);

f.addComponent(BorderLayout.CENTER, c);
f.addComponent(BorderLayout.SOUTH, tf);

final Command fCMD = new Command("AddMsg") {
public void actionPerformed(ActionEvent evt) {
final TextArea ta = new TextArea(2,30);
ta.setText(tf.getText());
tf.setText("");
c.addComponent(ta);
c.repaint();
}
};

f.addCommand(fCMD);
f.show();