-1

I am trying to create a JPanel that is resizable & scrollable and contains x smaller inner panels. Each inner panel can be as wide as it wants/needs. BUT the depth should be a preferred size.

Like:

enter image description here

So far my code is:

public class TestSize {

    public static void main(String[] args) {

        JFrame F = new JFrame();
        F.setVisible(true);
        JPanel P = new JPanel();
        P.setLayout(new BorderLayout());
        JScrollPane scrollPane = new JScrollPane(P);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setPreferredSize(new Dimension(900,900));//.setBounds(50, 30, 300, 50);

        JPanel S = new JPanel();
        S.setBackground(Color.GREEN);
        S.setPreferredSize(new Dimension(900,200));
            JPanel S2 = new JPanel();
        S2.setBackground(Color.GREEN);
        S2.setPreferredSize(new Dimension(900,200));
            P.add(S,BorderLayout.NORTH);
            P.add(S2,BorderLayout.NORTH);


        F.add(scrollPane);
        F.pack();
        F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

enter image description here

But when I have added a single inner panel it fills all the space vertically - which is not what I want:

ManInMoon
  • 6,795
  • 15
  • 70
  • 133

2 Answers2

0

In BoxLayout, there are different methods of using invisible components as filler. I don't think you will want to use a "rigid area", since I think you want to have a variable number of inner panels. You may want to try using vertical glue or custom Box.Filler.

Another solution might be to put a JPanel between your JFrame and your ScrollPane that uses a BorderLayout, and put the scrollpane in the BorderLayout.NORTH of that panel. Components in BorderLayout.NORTH get resized horizontally, but they do not get resized vertically. Essentially, they just get pushed to the top of the panel.

Edit:

I think you will want something like this:

    JFrame F = new JFrame();
    F.setVisible(true);
    F.setLayout(new BorderLayout());
    JPanel P = new JPanel(new BoxLayout(P, BoxLayout.PAGE_AXIS));
    JScrollPane scrollPane = new JScrollPane(P);

    JPanel S = new JPanel();
    S.setBackground(Color.GREEN);
    JPanel S2 = new JPanel();
    S2.setBackground(Color.BLUE);
    P.add(S);
    P.add(S2);

    F.add(scrollPane, BorderLayout.NORTH);
    F.pack();
    F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

You said you want it resizeable, so I don't see why you would set the preferred size of the frame. This way it will just fit to the panels inside.

gla3dr
  • 2,179
  • 16
  • 29
  • That seemed quite good for 1 panel. added with: P.add(S,BorderLayout.NORTH); but when I added a second I get with P.add(S2,BorderLayout.NORTH); Then the second panel REPLACES the first... – ManInMoon Nov 13 '13 at 16:28
  • @ManInMoon That's because you can only have 1 component in each section of a BorderLayout. Are each of your panels in their own scrollpane? – gla3dr Nov 13 '13 at 16:43
  • @ManInMoon, so how is this question different than your question yesterday (http://stackoverflow.com/q/19936025/131872) in which you were advised to add the fixed panel to the NORTH and the other panel to the CENTER? – camickr Nov 13 '13 at 16:43
  • @camickr This is about adding MULTIPLE wide but not deep panels to a scroll pane... – ManInMoon Nov 13 '13 at 16:45
  • So where in you SSCCE do you attempt to do this? Just create a panel for the NORTH and add other panels to it. Then create a JScrollPane using the north panel and add the scrollpane to the NORTH of the frame. – camickr Nov 13 '13 at 16:50
  • @ManInMoon Ok, I see what you are trying to do in your updated code. You want all of the panels in 1 scrollpane that takes up the entire frame? In that case, you should have another panel inside `P` to the NORTH that contains S, S1... – gla3dr Nov 13 '13 at 17:51
  • @gla3dr - err then I will get the sizing issue again! - that's why you suggested NORTH in the first place was it not? – ManInMoon Nov 13 '13 at 18:15
  • There is no need for another panel inside "P". All you need to do is use an appropriate layout manager for "P" as I have already suggested an hour ago. – camickr Nov 13 '13 at 18:50
0
//P.setLayout(new BorderLayout());

Why did you set the layout to a BorderLayout? You can only add 1 component to the NORTH. Is that what you want? Read the section from the Swing tutorial on Using Layout Managers and pick a more appropriate layout manager. Bookmark the tutorial link as it provides the basics for Swing programming.

scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

Why did you set these properties? These are the default values.

scrollPane.setPreferredSize(new Dimension(900,900));

Why would you set the height to be 900. You only want the scrollpane to contain components of height 200? In general you should NOT be setting the preferred size. Let the frame.pack() method do its job.

S.setBackground(Color.GREEN);

Why would you set the background color of both panels to be the same. How can you tell if the two panels get added? Make them different color for an easy visual.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks for all the comments but it would be nice if you read what other people suggested first. I want lots of short wide panels (I can have what I want can't I?) and they are GREEN because this is just a SCCE!!!!!! Some actual suggestions about to TO DO instead of what NOT TO DO would be appreciated. – ManInMoon Nov 13 '13 at 18:14
  • `would be nice if you read what other people suggested first` - I have, why do you think I commented in the other answer? `they are GREEN because this is just a SCCE` - a SSCCE should still make sense. You should be able to tell if both panels are displayed. It is a one line change to your code. This is a standard debugging technique.`suggestions about to TO DO instead of what NOT TO DO would be appreciated` - I did give you Do's. The Do's and Don'ts all work in combination to solve the problem. You have your answer. – camickr Nov 13 '13 at 18:30
  • You've lost me! The only suggestion I see is to add a "panel" to the NORTH border. Are you suggesting that that panel will behave differently because it is in the NORTH border? – ManInMoon Nov 13 '13 at 18:59
  • I suggested you read the `Swing tutorial` on layout managers so that you understand how layout managers work. Sstart with the `BorderLayout` because that is the default layout manager for a JFrame. `Are you suggesting that that panel will behave differently because it is in the NORTH border?` - YES. The height will be respected but the width will vary. So the scrollpane should be added to the NORTH of the frame. The panel added to the frame will contain your other panels that vary by width, so you need to choose a layout manager for that panel that will display the S, S1, panels horizontally. – camickr Nov 13 '13 at 19:38