adding labels to a scrollpane within a splitpane

Posted on 2006-07-12
Medium Priority
Last Modified: 2011-10-03

I'm adding multiple labels with icons from a database to a panle, which is within a scrollpane, which is itself set within a splitpane. The splitpane is necessary to control the layout of three panels within the overall GUI.

When I try to add more labels, so that the scroll bars would normally activate, they are not actually doing anything. The labels are added to the panel as required using a button, and the labels continue to fill up the space within the panel until all space is occupied. However, the pane is not increasing in size and using the scrollbars.

Current code for the panels etc is as follows,

        panel2 = new JPanel();
        panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
        TitledBorder aboveTopBorder2 = BorderFactory.createTitledBorder
                ("Sign Output");
        panel2.setPreferredSize(new Dimension(800, 175));
        JScrollPane scroll2 = new JScrollPane(panel2,

        JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scroll2, panel3);
        contentPane1.add(sp, BorderLayout.SOUTH);

The icon is loaded onto the label as follows,

        if (source == convertButton)
            try {
                while (sqlLoader.rs.next()) {
                    if (sqlLoader.rs.getString("sign_name").equals(
                if (!sqlLoader.rs.isAfterLast()) {
                    Blob blob = sqlLoader.rs.getBlob("sign_full");
                    int b;
                    InputStream bis = sqlLoader.rs.getBinaryStream("sign_full");
                    icon = new ImageIcon(blob.getBytes(1L, (int) blob
                    JLabel label = new JLabel();   // create a new label every time
                    label.setIcon(iconThumbnail);   //set the icon to the new label

Does anybody know why the panel and the scrollbars are not working properly? They should just allow as many labels as required to be added to the panel, with the scrollbars simply scrolling down through the panel.

Any help or advice appreciated.

Question by:nhay59
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 17095182
The following code gives scrollbars as you desire.  I wrote it with eclipse's Visual Editor (VE):

package com.your.package;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.BoxLayout;
import java.awt.GridLayout;

public class LabelsInScrollPane extends JPanel {

      private static final long serialVersionUID = 1L;
      private JPanel jPanel = null;
      private JSplitPane jSplitPane = null;
      private JPanel jPanel1 = null;
      private JPanel jPanel2 = null;
      private JScrollPane jScrollPane = null;
      private JPanel jPanel3 = null;
      private JLabel jLabel = null;
      private JLabel jLabel1 = null;
       * This is the default constructor
      public LabelsInScrollPane() {

       * This method initializes this
       * @return void
      private void initialize() {
            this.setSize(412, 497);
            this.setLayout(new BorderLayout());
            this.add(getJPanel(), BorderLayout.CENTER);

       * This method initializes jPanel      
       * @return javax.swing.JPanel      
      private JPanel getJPanel() {
            if(jPanel == null) {
                  jPanel = new JPanel();
                  jPanel.setLayout(new BorderLayout());
                  jPanel.add(getJSplitPane(), BorderLayout.CENTER);
            return jPanel;

       * This method initializes jSplitPane      
       * @return javax.swing.JSplitPane      
      private JSplitPane getJSplitPane() {
            if(jSplitPane == null) {
                  jSplitPane = new JSplitPane();
            return jSplitPane;

       * This method initializes jPanel1      
       * @return javax.swing.JPanel      
      private JPanel getJPanel1() {
            if(jPanel1 == null) {
                  jPanel1 = new JPanel();
                  jPanel1.setLayout(new BorderLayout());
                  jPanel1.add(getJScrollPane(), BorderLayout.CENTER);
            return jPanel1;

       * This method initializes jPanel2      
       * @return javax.swing.JPanel      
      private JPanel getJPanel2() {
            if(jPanel2 == null) {
                  jLabel1 = new JLabel();
                  jLabel1.setText("This gives scroll bars on the left panel");
                  jPanel2 = new JPanel();
                  jPanel2.setLayout(new BorderLayout());
                  jPanel2.add(jLabel1, BorderLayout.CENTER);
            return jPanel2;

       * This method initializes jScrollPane      
       * @return javax.swing.JScrollPane      
      private JScrollPane getJScrollPane() {
            if(jScrollPane == null) {
                  jScrollPane = new JScrollPane();
            return jScrollPane;

       * This method initializes jPanel3      
       * @return javax.swing.JPanel      
      private JPanel getJPanel3() {
            if(jPanel3 == null) {
                  GridLayout gridLayout = new GridLayout();
                  jPanel3 = new JPanel();
                  for(int i = 0; i < gridLayout.getRows(); i++) {
                        jPanel3.add(getJlabelWithIcon(i), null);
            return jPanel3;

      private JLabel getJlabelWithIcon(int i) {
            JLabel jLabel = new JLabel();
            jLabel.setText("Label " + i);
            jLabel.setIcon(new ImageIcon("C:/Documents and Settings/xcxc/eclipse3x/BlahBlah/web/check.gif"));
            return jLabel;

}  //  @jve:decl-index=0:visual-constraint="10,10"

Author Comment

ID: 17104710

Adding the actual scrollbars etc is not the problem. The problem occurs with the addition of the labels. The labels fill up the available space in the panel, but the scrollpane does not scroll through any more labels. In effect, the labels are filling up the available space in the panel, but the panel is not expanding to accomodate any more labels.

Therefore, in effect, the scrollbars appear to only scroll through the default available size of the panel, which is currently set to 800x175, and no more. The scrollbars therefore appear to work correctly, but the panel is not growing in size to accomodate more and more labels.

Anyone know how to change this problem to allow the panel to expand to fit an ever increasing number of labels? In effect, to allow as many labels as the user requires on the panel, currently set to panel2.

Any help or advice really appreciated.


Accepted Solution

ForceRs earned 1500 total points
ID: 17110640
You may want to try removing all manual settings of PreferredSize() (e.g., panel2.setPreferredSize(new Dimension(800, 175));).

Let the layout maanger handle this for you.  This may allow the panel to behaive as you desire.

Also, as in my example ocde, perhaps try to use the GridLayout instead of FlowLayout.  FlowLayout, I believe, will not wrap the JLabels you're adding to it.  I don't think you need to specify the exact number of rows in the GridLayoout in order for it to work correctly.  You would likely want 0 columns (i.e., 1 coloumn physically).

Author Comment

ID: 17112257

Thanks for the reply. I've tried GridBagLayout, and the scrollbars do work correctly now. The only problem is that each label is effectively being added to a new line, one on top of the other. I need to figure out how to allow each new label to be set to the right of the previous one, until it gets to the end of the row width for the panel .Then the new label should be placed at the start of the next row, on the left hand side of course. This is why I initially used FlowLayout, to achieve this exact layout, but of course that doesn't allow the scrollbars to work correctly.

Effectively, each label should be set one after the other in a horizontal row as required. I'm getting there slowly, I just need to figure out now how to get horizontal alignment with vertical scrolling.

Anyway, thanks for the help so far.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question