Link to home
Start Free TrialLog in
Avatar of nhay59
nhay59

asked on

problems using a scrollpane with multiple labels and scrolling correctly

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
Avatar of sciuriware
sciuriware

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!
Avatar of nhay59

ASKER

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
Another try, at least how I do it today:
replace the JPanel by a JEditorPane and use HTML to place those icons.

;JOOP!
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!
Avatar of nhay59

ASKER

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


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.

>>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.
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
use gridbaglayout and add the label in each row
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 ) );  
Avatar of Mick Barry
>         panel2.setPreferredSize(new Dimension(800, 175));

delete that

and revalidate your scroll pane after adding label
Avatar of nhay59

ASKER

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.
try setting HORIZONTAL_SCROLLBAR_NEVER
>>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.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nhay59

ASKER

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.
Avatar of nhay59

ASKER

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
thats what I suggested above :)
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
Avatar of nhay59

ASKER

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.
Avatar of nhay59

ASKER

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
Avatar of nhay59

ASKER

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