Search code examples
javascrollbarrectscrollrect

Java scrollRectToVisible weird scrolling


I have made a custom Table Component in Java and I am curious why scrollRectToVisible always scrolls to bottom right point of the input rectangle.

Is there a way how to tell parent component to attempt to view whole rectangle? Because its not funny when you want to automatically scroll to a table row and it always just view bottom of the wanted table row.

*EDIT

private JPanel box = new JPanel();
private JScrollPane scroll = new JScrollPane(box, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
public void scrollTo(int index){
    if(datas.size() != 0){
        JPanel panel;
        if(index < 0){
            index = 0;
        }else if(index >= datas.size()){
            index = datas.size() - 1;
        }
        panel = datas.get(index);
        Rectangle rectangle = panel.getBounds();
        if(index != 0){
            rectangle.setLocation(0, rectangle.y + 1);
        }
        box.scrollRectToVisible(rectangle);
    }
}

Solution

  • *SOLUTION
    Oh well, never mind I found out the problem. It was scrolling to bottom right corner because for some reason during constructor was getVisibleRect telling all data are zero, but after a whole Frame was constructed then it started to scrolling to normal position :(