0

I'am using panel with absolute layout (don't ask why) and I need to add elements on it programmatically. I done that part, but now I want to surround panel with JScrollPane so that when user add more items, scroll bar does its job. But surrounding panel with scroll bar doesn't work. What can I do here.

JFrame frame = new JFrame();
    frame.setSize(582, 451);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(10, 11, 546, 391);
    frame.getContentPane().add(scrollPane);

    JPanel panel = new JPanel();
    scrollPane.setViewportView(panel);
    panel.setLayout(null);




for(int i=0;i<labelList.size();i++){
        if(a==4){
            a=0;
            b++;
        }
        labelList.get(i).setBounds(10+120*a+10*a, 10+130*b+10*b, 120, 130);
        labelList.get(i).setOpaque(true);
        labelList.get(i).setBackground(Color.WHITE);
        labelList.get(i).setText(Integer.toString(i));
        panel.add(labelList.get(i));
        a++;
    }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    `"I'am using panel with absolute layout (don't ask why)"` -- Please remember that you're asking **volunteers** to help you, and so should not put any stipulations on that help. The obvious question for most folks experienced with Swing is why you want to use absolute layout. Since your question stipulates that we can't ask this, then I will bow out. Hopefully someone else will help you, but to increase your chances of getting help, consider explaining just a little bit about your requirement as it is germane to your problem, and will help us understand it better. – Hovercraft Full Of Eels Oct 19 '14 at 17:07
  • 1
    Note, that when your JPanel does use a valid layout manager, its **preferredSize** will automatically increase as more components are added and it is revalidated (the layouts are told to re-layout their components). Your null JPanel will not do this, and so the JScrollPane will not work. – Hovercraft Full Of Eels Oct 19 '14 at 17:10
  • @Hover I'm not forcing anyone to help me. I'am just asking if anyone have solution of problem I'am trying to solve the way I like. The "don't ask why" is not mentioned to be unfriendly, it is because everyone will answer like "don't use absolute layout". It is like someone watch TV from 2 meter distance and ask if someone can help him switch programs without walking. Than someone else says "why wouldn't you get up and change programs manualy". To prevent this, person A adds anex "don't tell me that I can switch programs by getting up, i know that already". Thanks for replay. – Ђорђе Ђашић Oct 19 '14 at 17:27
  • Well, despite my reservations, I've already given you an answer that should solve your problem, but still I'd appreciate hearing why you don't want to use layout managers. You could very well be dealing with an [XY Problem](http://mywiki.wooledge.org/XyProblem) where you ask how to solve a specific code problem when the best solution is to use a completely different approach. – Hovercraft Full Of Eels Oct 19 '14 at 17:43
  • It is because I'm not skilled in Swing programming, to be more specific I am beginner in Swing, then I found this absolute layout being most logical (to me, at this point). You can see up there how I calculate pixels for each component, and I know the other layouts would do that in more elegant way. In the end you were right all the time... :) – Ђорђе Ђашић Oct 19 '14 at 17:53
  • *"..someone else says "why wouldn't you get up and change programs manualy". To prevent this.."* ..explain yourself, rather than issuing people with commands. – Andrew Thompson Oct 19 '14 at 21:27
  • 1
    `I am beginner in Swing, then I found this absolute layout being most logical (to me, at this point).` it is not the most logical because Swing was designed to be used with layout managers. A layout manager does more than just position components on a panel. As a beginner you don't know all the benefits and as a result you are having problems trying to use a scrollpane which you would not have if you used layout managers. It is better that you spend the time NOW to learn and understand layout managers instead on learning the incorrect way to code. – camickr Oct 19 '14 at 21:27

1 Answers1

2

You're not going to like my answer, but I feel that we should be compelled to give you the best answer, which is not always what you want to hear. And that is this: use layout managers to do your component layouts. Yes while it might seem to newbies that null layouts are the easiest to use, as you gain more experience you will find that exactly the opposite is true. Null layout use makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.

As for ease of use, I invite you to create a complex GUI with your null layout, and then later try to add a component in the middle of this GUI. This will require you to manually re-calculate all the positions and sizes of components added to the left or below the new component, which is prone to many errors. The layout managers will all do this automatically for you.

Specifically use of valid layout managers will update your JPanel's preferredSize automatically increase as more components are added and as the JPanel is revalidated (the layouts are told to re-layout their components). Your null JPanel will not do this, and so the JScrollPane will not work. Yes, a work around is for you to manually calculate and set your JPanel's preferredSize, but this is dangerous and not recommended for the same reasons noted above.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • No, I like your answer and it is completely right. I have another question that just made up from my mind. In program, when someone use Layout Manager, and during the execution of the code, does all Managers rely exactly on pixels, for example if I add label on panel with Layout Manager, does Java actually does it on pixel level, and the use of Manager is only for the programmer to make his work easy? – Ђорђе Ђашић Oct 19 '14 at 18:01
  • @ЂорђеЂашић: I'm not sure that I fully understand your question. The manager usually places components relative to each other and the stipulations that you give the manager. Each layout manager type, such as FlowLayout, BorderLayout, GridLayout, BoxLayout, GridBagLayout,.... works differently from the others. – Hovercraft Full Of Eels Oct 19 '14 at 18:03
  • I will try to explain with example. There are instances panel and label. With BorderLayout to add this label on panel we use panel.add(label). Does behind this, in the definition of method add(Component c) stays something like panel.add(label, x, y) where x and y are pixel location of panel. I mean, does BorderLayout uses AbsoluteLayout (under the hood)? – Ђорђе Ђашић Oct 19 '14 at 18:09
  • @ЂорђеЂашић: I think that the layout manager's code mainly deals with relative positioning of components. You can view their source code and all Java source code [here](http://grepcode.com/). – Hovercraft Full Of Eels Oct 19 '14 at 18:46
  • 1
    @ЂорђеЂашић This conversation goes back some 10 years, but there was an underlying assumption that the Graphics team made that the output was at 72dpi, I think this might have changed over the years as the graphics pipeline has changed and matured. At a VERY basic level, as, the layout manager thinks in terms of pixels and not picas or some other unit of measurement, but this isn't guaranteed as will depend on the underlying graphics pipeline – MadProgrammer Oct 19 '14 at 23:16
  • @HovercraftFullOfEels I just want to thank you for wiping out the Absolute Layout from my head. Now I use MigLayout and it is brilliant. Thanks. – Ђорђе Ђашић Oct 23 '14 at 14:48