• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 673
  • Last Modified:

adding labels to a scrollpane within a splitpane


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.

  • 2
  • 2
1 Solution
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"
nhay59Author Commented:

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.

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).
nhay59Author Commented:

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now