-1

ive got a JPane within a JScrolledPane. When i add content to JPane , JScrollPane doesnt show scrollbar. I tried repaint() and revalidate() but it didnt help.

static void ladowaniePaneli()
    {
    int b;
    for(b=0;b<o;b++)
        {
        bgPanel[b] = new JBackgroundPanel();
        nowyPanel[b] = new JPanel();

        ((FlowLayout)bgPanel[b].getLayout()).setVgap(0);
        nowyPanel[b].setPreferredSize(new Dimension(790,518));
        nowyPanel[b].setOpaque(false);

        vertical[b] = new JScrollPane(nowyPanel[b]);
        vertical[b].setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        vertical[b].setPreferredSize(new Dimension(789,517));
        vertical[b].setOpaque(false);
        vertical[b].getViewport().setOpaque(false);
        bgPanel[b].add(vertical[b]);           
        }
    }
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366

2 Answers2

3

It makes sense that scrollbars are never seen since you restrict the size of the contained component so that it's always trivially larger than the scrollopane's viewport:

nowyPanel[b].setPreferredSize(new Dimension(790,518));

Solution: don't do that.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • (or use the Scrollable interface) – MadProgrammer May 28 '13 at 02:23
  • im using flowLayout. if i dont use setPreferredSize method components wont warp to another line. quess the answer for that is trying another layout but i dont know layouts that well. could anyone at least tell me which layout would be best for this? Im adding JButtons + JTextAreas there. – Robert Kubisiak May 28 '13 at 02:28
  • @RobertKubisiak The best layout will depend on what it is you're trying to achieve – MadProgrammer May 28 '13 at 02:46
  • @MadProgrammer im trying to add components in pairs of JButton+JTextArea.. no horizontal scrolling. just vertical. pairs should be warped to new line if they exceed JScrollPane width – Robert Kubisiak May 28 '13 at 03:00
  • 1
    +1 for getting the OP to listen to the suggestion. @RobertKubisiak, you were given this advice in you last question by MadProgrammer. Please pay attention to all answers given when you ask a question. – camickr May 28 '13 at 03:12
  • 1
    @RobertKubisiak `GridBagLayout` is probably your best shot. Here is an example that displays several pairs of label and textfield: http://stackoverflow.com/questions/16397609/positioning-gui-objects-on-a-frame/16397844#16397844 – Guillaume Polet May 28 '13 at 07:51
  • @Guillaume Polet i just used it. worked perfectly. thx everyone for help – Robert Kubisiak May 28 '13 at 13:45
3

if i dont use setPreferredSize method components wont warp to another line

You can try the Wrap Layout.

pairs should be warped to new line if they exceed JScrollPane width

Components are layed out individually. I you want a group of components to wrap then you would need to add the components to a separate panel first. Then add the panel to the panel using the WrapLayout.

camickr
  • 321,443
  • 19
  • 166
  • 288