Alex W
Alex W

Reputation: 330

JScrollPane does not set its scrollbar's positional properties immediately

I have a JScrollPane displaying a JPanel, which has added to it (in a BoxLayout.Y_AXIS layout, i.e. vertically downwards) small JPanels that contain some text with titles and borders.

When this is created, if the larger JPanel in the JScrollPane uses up more space than viewable such that a vertical scrollbar appears, my JScrollPane does not necessarily set its scroll to the top.

Fixing this consists of setting the JScrollPanel's value attribute, which is the attribute storing how far down it has scrolled. However, upon adding the elements to the JScrollPane and then adding this JScrollPane to the frame in which it belongs, the attributes for its position (such as value, maximum, minimum) have not been set, and therefore I cannot set the value to zero as it is already zero, before then realising it is not zero.

Below is a small commented program I've created to demonstrate the problem.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class App
{
    public static void main ( String[] args )
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            @Override
            public void run ()
            {
                new App();
            }
        } );
    }

    public App()
    {
        // Making the main window.
        JFrame window = new JFrame();
        window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        window.setSize( 200, 200 );
        window.setLayout(
                new BoxLayout( window.getContentPane(), BoxLayout.Y_AXIS ) );

        // Making the JPanel that the scrollbox will be on.
        JPanel scrollBoxPanel = new JPanel();
        scrollBoxPanel
                .setLayout( new BoxLayout( scrollBoxPanel, BoxLayout.Y_AXIS ) );
        scrollBoxPanel.setMaximumSize( new Dimension( 300, 500 ) );

        // Making the JScrollPane.
        JScrollPane listScroller = new JScrollPane( scrollBoxPanel );

        // Making some panels of text.
        JPanel smallPanel;
        JTextArea textArea;

        for ( int i = 0; i < 3; i++ )
        {
            smallPanel = new JPanel();
            smallPanel.setLayout( new BorderLayout() );

            textArea = new JTextArea(
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fringilla, augue vitae venenatis venenatis, dui sapien suscipit libero, et malesuada quam odio vel sapien.\n" );
            textArea.setLineWrap( true );

            TitledBorder criticalPanelBorder = BorderFactory.createTitledBorder(
                    BorderFactory.createEtchedBorder( EtchedBorder.RAISED ),
                    "Box " + i, TitledBorder.LEFT, TitledBorder.TOP,
                    new Font( null, Font.BOLD, 14 ) );
            smallPanel.setBorder( new CompoundBorder( criticalPanelBorder,
                    new EmptyBorder( 5, 5, 5, 5 ) ) );

            smallPanel.add( textArea );
            scrollBoxPanel.add( smallPanel );
        }

        // Adding the JScrollPane to the window and displaying.
        listScroller.validate();
        window.add( listScroller );
        window.setVisible( true );

        // Printing the attributes of the JScrollBar. Note that the value prints
        // as 0, but is not zero in the program.
        JScrollBar scrollBar = listScroller.getVerticalScrollBar();
        System.out.println( "Value: " + scrollBar.getValue() + ", Max: "
                + scrollBar.getMaximum() + ", Min: " + scrollBar.getMinimum() );

    }
}

How do I set the scrollbar's value to zero?

Upvotes: 0

Views: 105

Answers (1)

camickr
camickr

Reputation: 324197

How do I set the scrollbar's value to zero?

Wrap the code in a second SwingUtilities.invokeLater(). This will make sure the code is executed AFTER the GUI has been built.

Upvotes: 2

Related Questions