Solved

adding labels to a scrollpane within a splitpane

Posted on 2006-07-12
4
643 Views
Last Modified: 2011-10-03
Hi,

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");
        aboveTopBorder2.setTitlePosition(TitledBorder.ABOVE_TOP);
        panel2.setBorder(aboveTopBorder2);
        panel2.setPreferredSize(new Dimension(800, 175));
       
        JScrollPane scroll2 = new JScrollPane(panel2,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        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 {
                sqlLoader.rs.beforeFirst();
                while (sqlLoader.rs.next()) {
                    if (sqlLoader.rs.getString("sign_name").equals(
                            translateText.getText()))
                        break;
                }
                if (!sqlLoader.rs.isAfterLast()) {
                   
                    Blob blob = sqlLoader.rs.getBlob("sign_full");
                   
                    int b;
                    InputStream bis = sqlLoader.rs.getBinaryStream("sign_full");
                   
                    bis.close();
                   
                    icon = new ImageIcon(blob.getBytes(1L, (int) blob
                            .length()));
                   
                    loadImage();
                   
                    JLabel label = new JLabel();   // create a new label every time
                    label.setIcon(iconThumbnail);   //set the icon to the new label
                    panel2.add(label);  
                    panel2.revalidate();

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.

Thanks
0
Comment
Question by:nhay59
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:ForceRs
Comment Utility
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() {
            super();
            initialize();
      }

      /**
       * 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();
                  jSplitPane.setLeftComponent(getJPanel1());
                  jSplitPane.setRightComponent(getJPanel2());
            }
            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();
                  jScrollPane.setViewportView(getJPanel3());
            }
            return jScrollPane;
      }

      /**
       * This method initializes jPanel3      
       *       
       * @return javax.swing.JPanel      
       */
      private JPanel getJPanel3() {
            if(jPanel3 == null) {
                  GridLayout gridLayout = new GridLayout();
                  gridLayout.setRows(50);
                  jPanel3 = new JPanel();
                  jPanel3.setLayout(gridLayout);
                  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"
0
 

Author Comment

by:nhay59
Comment Utility
Hi,

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.

Thanks
0
 
LVL 3

Accepted Solution

by:
ForceRs earned 500 total points
Comment Utility
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).
0
 

Author Comment

by:nhay59
Comment Utility
Hi,

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.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now