Wednesday, June 3, 2015

NetBeans Java scroll bar (JScrollBar) does not scroll

To view a large size of data by scrolling it using the JScrollBar, one usually would put the data in a data container such as a JTextArea or a JTable, create a JScrollPane object and call the setViewportView method to set the data container to be viewed in the JScrollPane, and then set the scroll policy to be either as needed or always.

However, when you keep adding data to your data container and the newly added data goes out of the view, you try to drag the scroll bar or press the arrow at the end of  the side where the scroll bar locates, sometimes the scroll bar only moves a little bit, then stopped moving. This is because the preferred size of the data container (the JTextArea or the JTable) is set too small. The preferred size is the actual area that can be viewed in the JScrollPane. When data goes beyond this area, it is not visible and not accessible to scrolling.

If you use the GUI design of the NetBeans to build your components, the preferred size is automatically assigned, you need to make sure that it is big enough to contain all the possible data so that all the data are visible by scrolling. Or you can right click on the data container and choose the customize code to comment out the size setting code.
       
------------------------------------------------------------------------------------------------------------------------
                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
--------------------------------------------------------------------------------------------------------------------------

Reference:

1. setSize vs setPreferredSize in JTextArea, JTextField, JButton, JLabel, JPanel and JFrame