adding labels to a scrollpane within a splitpane

Posted on 2006-07-12
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 ( {
                    if ("sign_name").equals(
                if (! {
                    Blob blob ="sign_full");
                    int b;
                    InputStream bis ="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
  • 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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 20
split string containing \r\n in Java 46 44
couple of eclipse 5 37
Running JavaFX on JDeveloper 12C 1 32
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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

778 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