Update JscrollBar without resetting position

I have a class which extends JScrollPane, its viewport is another class which extends JComponent and implements Scrollable. When the size of the component changes the JscrollBars do not update unless I call revalidate() however this resets the position of the scroll bars to the top left. Is there a way of updating the size of the scroll bars while maintaining their current position?

Thanks, Rob

Answers


Is the problem your custom scroll pane or your custom component? We can't begin to guess what kind of changes you may have made. Post your SSCCE that demonstrates the problem.

It works fine for me.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScrollSSCCE extends JPanel
{
    public ScrollSSCCE()
    {
        setLayout( new BorderLayout() );

        final JPanel panel = new JPanel();
        panel.setPreferredSize( new Dimension(200, 200) );
        JScrollPane scrollPane = new JScrollPane( panel );
        add( scrollPane );

        JButton button = new JButton("Adjust");
        add(button, BorderLayout.SOUTH);
        button.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                Dimension d = panel.getPreferredSize();
                d.width +=50;
                d.height +=50;
                panel.setPreferredSize(d);
                panel.revalidate();
            }
        });
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("ScrollSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new ScrollSSCCE() );
        frame.setSize(150, 150);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

Need Your Help

Placing multiple submit buttons in the same line

jsp struts2

I am trying to place three submit buttons in the same line but they keep appearing one below another. The buttons function perfectly fine, I just cannot get them to be on the same line. My code in ...