How to make a JScrollPane scrollable after a component has been added at runtime

383
January 30, 2017, at 9:30 PM

Unfortunately, I have seen this question multiple times but unfortunately I do not know what I am doing well enough to be able to interpret what I have to implement in my own code.

I have used the GUI editor in netbeans to create a JScrollPane and a JPanel on top of this. I am aware I can create these components at runtime along with everything else but this proved problematic and simply adding the scroll pane and panel in the editor and adding components to them during runtime has worked for me thus far.

So far, creating components and adding them to the panel is no problem. The problem I face is that the scroll pane will not update itself to enable the user to scroll further down to view the created components at the bottom. I have been generating ‘entries’ each time a button is pressed, that currently creates a JTextField and adds this component to the panel, more components are going to be added later but for now this is just experimentation.

Once the ‘list’ of ‘entries’ exceeds the limit of the window, the window has to be resized in order to view components at the bottom but after a certain number of entries (around 25-30) the components are no longer viewable as the scroll bar does not scroll down the panel.

This is how I am adding components at runtime...

JTextField txtName1 = new JTextField();
txtName1.setLocation(10, 90);
txtName1.setSize(135, 25);
pnlContainer.add(txtName1);

The 'pnlContainer' is attached on top of the 'jspContainer' which is my JScrollPane and I am using the 'repaint()' method to get the components visible on the panel.

So far, adding components at runtime has worked, all of the components are visible and interact-able.

I am fairly new to programming in general with only a few years experience, any documentation that may help, tutorials or anything else is greatly appreciated. Documentation is always welcome as I still have a lot to learn.

Answer 1
JTextField txtName1 = new JTextField();
txtName1.setLocation(10, 90);
txtName1.setSize(135, 25);
pnlContainer.add(txtName1);

So far, adding components at runtime has worked, all of the components are visible and interact-able.

In you above code example you are setting the size/location of each component which implies you are using a null layout. Don't use a null layout!!!

Swing was designed to be used with layout managers and scrolling will work properly when you use panels with a layout manager.

The layout manager is responsible for determining the "preferred size" of the panel. The panel will then display scrollbars automatically when the preferred size of the panel is greater than the size of the scroll panel.

When you use a null layout the preferred size of the panel is 0, so the scrollbars will never appear.

Rent Charter Buses Company
READ ALSO
Restrict Access to Web Server for Specific Devices

Restrict Access to Web Server for Specific Devices

I have a web application running on Apache Tomcat and this Web Application runs on a server to which my Frontend connectsThe Frontend's could be a Browser or and App that runs on Android or iOS

358
How to pass JSP parameters to a SQL query?

How to pass JSP parameters to a SQL query?

I want to write a SQL query which contains parameters from a JSPI tried it like this

415
Code Generating Error at BST

Code Generating Error at BST

Code generating an Error and m trying to make this essential but failed doing that soplease help me to fix this

464
Jenkins Artifactory plugin not respecting custom repository layout

Jenkins Artifactory plugin not respecting custom repository layout

I am trying to deploy artifacts produced by the Jenkins Artifactory plugin from a Jenkinsfile to a repository with a custom layout

597