Solved

problems using a scrollpane with multiple labels and scrolling correctly

Posted on 2006-07-14
22
457 Views
Last Modified: 2011-09-20
Hi,

I'm trying to load a new label each time a particular button is pressed. This works fine until I try to add the panel with the labels to a scrollpane, which is also then part of a splitpane. The problem is that the labels only fill up the set size of the panel, and the scroll bars only scroll through the set size of the panel as well.

The labels are loaded repeatedly within the actionPerformed method for the button, as follows:

                    SignPanel.icon = new ImageIcon(blob.getBytes(1L, (int) blob
                            .length()));
                   
                    SignPanel.loadImage();
                   
                    JLabel label = new JLabel();
                    label.setIcon(SignPanel.iconThumbnail);
                    panel2.add(label);  
                    panel2.revalidate();

panel2 and the scrollpane are loaded within the the buildFrame method, as follows, and then added as normal to the splitpane:

        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);
        scroll2.revalidate();

       JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scroll2, panel3);

(panel3 is just a standard panel with a textfield and a couple of buttons)

I really can't see why this won't work. I've tried every alternative I can think of and it still won't work properly. The labels simply fill the size of the panel and the scrollbars won't scroll beyond the set size of panel2.

Does anybody know how to correct this and allow the labels to be continuosly added to the panel, with the scrollbars scrolling vertically through the panel as you add more labels beyond the set size of the panel. In effect, the scrollbars only seem to be affected by resizing the panel, and not by adding the labels to the panel.

Any help or advice is appreciated.

Thanks
0
Comment
Question by:nhay59
  • 8
  • 6
  • 4
  • +2
22 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 17110124
The problem is that the JPanel does not resize but provides a viewport-like
windowlet on those labels.
I had some problems like that.

What happens if you replace the JPanel by a JWindow
and apply 'pack()' to it, that is "resize to contain them all"?

;JOOP!
0
 

Author Comment

by:nhay59
ID: 17110208
Hi,

The problem with using a JWindow, is that you can't add a window to a container. So unless I've misunderstood you, you won't be able to replace the JPanel, panel2, with a window because you can't add it to the scrollpane or splitpane.

You're right though about the resizing of the panel. The panel is not reacting dynamically to the addition of the labels.

There must be a way to dynamically resize the panel for the labels.

Thanks for the help
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17110336
Another try, at least how I do it today:
replace the JPanel by a JEditorPane and use HTML to place those icons.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17110388
JEditorPane display = null;

      display = new JEditorPane();
      display.setEditable(false);
      display.setEditorKit(new HTMLEditorKit());
      display.setText("Picture follows below:<BR>"
         + "<IMG SRC=\"" + yourIcon.toString() + "\">");


// Then put this 'display' in a JScrollPane; works OK.
// Of course the text should be extended every time as:
      String addition;
      display.setText(display.getText + "<BR>" + addition); // or such.
;JOOP!
0
 

Author Comment

by:nhay59
ID: 17110526
Hi,

Thanks for the reply. That seems a bit complicated to just render images on labels. There should be a way to use panels and a scrollpane with the labels.

I have managed to get the scrollpane to react to the addition of the labels, but it is only scrolling horizontally. It effectively scrolls horizontally indefinitely. I now need it to react to the FlowLayout and scroll vertically instead.

I managed to get it to scroll horizontally by only setting the preferred size for the scrollpane, and not the panel2, within the buildFrame method.

Anybody any idea how to achieve this?

Would it be possible to create a subclass of JPanel, which implements Scrollable and so defines how the Panel behaves with the ScrollPane? I have no idea how to do this though.

Any help or advice really appreciated.

Thanks


0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17110605
remove the "panel2.setPreferredSize(new Dimension(800, 175));" instead use the setMinimumSize method.

>>panel2.setLayout(new FlowLayout(FlowLayout.LEFT));

set borderlayout instead of the flowlayout.

Looks like you don't need a panel at all, simply add the label to the scroll pane and set the size to the label.

0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17110621
>>Looks like you don't need a panel at all, simply add the label to the scroll pane and set the size to the label.

sorry, misunderstood your quesion. thought that you are loading the image in single label always.
0
 
LVL 1

Expert Comment

by:Shoikana
ID: 17110630
Why not set panel2's layout to be a box layout? then you can make it vertical:

panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));

Amy
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17110632
use gridbaglayout and add the label in each row
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17110653
set layout as below,

panel2.setLayout( new GridBagLayout() );

add label as below,

panel2.add( label, new GridBagConstraints( 0, GridBagConstraints.RELATIVE, 1, 1, 0.0, 0.0,
    GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 0, 0 ) );  
0
 
LVL 92

Expert Comment

by:objects
ID: 17112301
>         panel2.setPreferredSize(new Dimension(800, 175));

delete that

and revalidate your scroll pane after adding label
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:nhay59
ID: 17112378
Hi,

Thanks for all the replies.

I think I'm moving in the correct direction. If I use BoxLayout, either Y_Axis or X_Axis, I can get continuous scrolling of the labels in either the X or Y axis direction, but not both. If I use the above GridBagLayout example, each new label is added to a new row, which means that the output is the same as the BoxLayout Y_Axis.

If I revalidate the scrollpane, with the use of the FlowLayout manager for panel2, I still only get horizontal scrolling.

The problem is that I need to be able to add new labels, one after the other, starting at the left side of the panel and progressing to the far right side, then move on to the next line and so on. This is why I initially used FlowLayout for the layout of panel2. This achieves the required layout result within the set confines of the panel's set preferred size, but does not allow correct scrolling of the labels in a vertical direction.

I think a bit more work is required on this design.

Thanks for all the help and advice so far.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17112425
try setting HORIZONTAL_SCROLLBAR_NEVER
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17112435
>>If I use the above GridBagLayout example, each new label is added to a new row, which means that the output is the same as the BoxLayout Y_Axis.

not the case! you can add the label in column too. you may have to check the size( width ) of the display area and the image and then decide addin in row/column.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 17112545
try:

panel2.setPreferredSize(new Dimension(800, Integer.MAX_VALUE));

and revalidate() the scrollpane
0
 

Author Comment

by:nhay59
ID: 17114621
Hi,

Thanks for the replies and help so far. If I add the following,

panel2.setPreferredSize(new Dimension(800, Integer.MAX_VALUE));

to the actionPerformed method, each time I create and add the labels, the scrollbars work correctly but the output is changed for the panel. The background of panel2, and therefore the scrollpane as well, is changed to all white when you first press the convert button. The design, wih borders etc, for panel2 is lost, and only replaced gradually as the convert button is pressed each time and the labels are added to panel2 and the scrollpane.

It is also necessary to set the preferred size for at least the scrollpane, in the buildFrame method, primraily because there are initially no components on panel2 when the application is started. Labels are only added to panel2 and the scrollpane when the convert button is pressed on panel3.

Current code for the acionPerformed method is as follows,

                    SignPanel.icon = new ImageIcon(blob.getBytes(1L, (int) blob
                            .length()));
                   
                    SignPanel.loadImage();
                   
                    JLabel label = new JLabel();   // create a new label every time
                    label.setIcon(SignPanel.iconThumbnail);
                    panel2.setPreferredSize(new Dimension(800, Integer.MAX_VALUE));
                    panel2.add(label);  
                   
                    panel2.revalidate();

The buildFrame method is currently as follows for panel2 and the scrollpane,

panel2 = new JPanel();
        panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
       
        TitledBorder aboveTopBorder2 = BorderFactory.createTitledBorder
                ("Sign Output");
        aboveTopBorder2.setTitlePosition(TitledBorder.ABOVE_TOP);
        panel2.setBorder(aboveTopBorder2);

        JScrollPane scroll2 = new JScrollPane(panel2,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll2.setPreferredSize(new Dimension(800, 150));

If I try to revalidate the scrollpane, I get an 'Exception : null' message, even though the label still loads within the pane.

I seem to have tried every permutation and combination of statements, and all currently to no avail. If anybody can see why this might be happening, please let me know.

Any help or advice is appreciated.

Thanks.
0
 

Author Comment

by:nhay59
ID: 17115832
Hi,

The only solution I could find to work successfully, was to simply set the preferredSize for panel2 to a very large number for the height and then allow the user to simply scroll through. It's an awful hack, but at least it allows me to get on with the rest of the GUi and the controls.

Thanks to al for their help and advice
0
 
LVL 92

Expert Comment

by:objects
ID: 17116080
thats what I suggested above :)
0
 
LVL 92

Expert Comment

by:objects
ID: 17116086
sorry, duidn't see your earlier comment

> The background of panel2, and therefore the scrollpane as well, is changed to all white when you first press the convert button.

you need to set the background colur that you want to use

> The design, wih borders etc, for panel2 is lost, and only replaced gradually as the convert button is pressed each time and the labels are added to panel2 and the scrollpane.

can u explain further what is happeneing?

> It is also necessary to set the preferred size for at least the scrollpane

absolutely, you should always set the preferred size for your scroll pane if you are using it in a layout that requires it
0
 

Author Comment

by:nhay59
ID: 17117181
Hi,

Thanks for the reply. The GUI design, colours etc are set to system default for Mac OS X, so I'm not certain how I would actually get the background colour correct for this application when I add the labels.

If I use the above code I get the following process of events for the GUI:

1. The application loads as normal with a standard panel design, coluor etc for the panel within a scrollpane within a splitpane.
2. A user enters a search query in to the textfield in panel 3 and presses the 'convert' button. This results in  a new label, with the appropriate icon, being added to panel2.
3. Panel2 correctly adds the label to the panel, but the visible area of the panel, 800x150, is in a white background colour, and the label is not visible until the user starts to scroll. The scrollbars are present, and the user can scroll down the panel. As the user scrolls down the panel, the white colour is replaced with the original panel design, borders, title etc plus the new label.
4. The top line of the border, directly beneath the border title, is now shown in white, and not the required and normal black. The rest of the border is the correct design.

The design and layout of the panel does not change any further with the addition of extra labels.

I've tried every hack I can think of, and the only one that results in the correct layout design is to set the preferredSize for the panel to something like 800x8000 to allow the user to add alot of labels. It's an awful, and somewhat limiting hack though.

Thanks for the help and advice.
0
 

Author Comment

by:nhay59
ID: 17117185
Hi,

In addition to the previous post, panel2 will also continue to revert to all white within the preferredSize area whenever the window/frame is resized.

Thanks
0
 

Author Comment

by:nhay59
ID: 17126159
Hi,

Thanks to all for their help. I got there eventually, even if I did have to use a really bad hack. At least it works as required.

Thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

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…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler 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

22 Experts available now in Live!

Get 1:1 Help Now