0

I created a little program where i have a JFrame with a JScrollPane. In this JScrollPane there is a panel (where i set the Viewportview) with different JButtons. I set my setBounds, set null to the layout of the panel but i can't see the jscrollbar.

JFrame jf = new JFrame("My Frame");
//other things of the frame...

JScrollPane jsp = new JScrollPane();
jsp.setBounds(0,83,301,667);

JPanel jp = new JPanel();
jp.setLayout(null);

JButton jb = new JButton("Button 1");
jb.setBounds(0,0,40,1500)  //here there is only 1 button, a big button

jp.add(jb);
jp.revalidate();
jp.repaint();
jsp.setViewportView(jp);

And here is where i can't see the jscrollbar, it work only if I set the layout:

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

But with this layout i can't set my setBounds and everything is put below the previous component. Thanks and sorry for my bad english

Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
  • 1) For better help sooner, post an [MCVE](http://www.stackoverflow/mcve) (Minimal, Complete, Verifiable Example). 2) Swing GUIs might have to work on different platforms, using different PLAFs, on different screen sizes and resolutions with different default settings for font size. As such, they are not conducive to exact placement of components. Instead use layout managers, or [combinations of layout managers](http://stackoverflow.com/a/5630271/418556) as well as [layout padding and borders](http://stackoverflow.com/q/17874717/418556) for white space. .. – Andrew Thompson Jul 14 '14 at 08:13
  • .. 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Jul 14 '14 at 08:14
  • But can u just add screen shot of it? – Kishan Bheemajiyani Jul 14 '14 at 08:15
  • Just u can do one this autoscrols(true); and make sure once that your scroll panel size is less then panel size and then add jscroll.add(panel); try it once – Kishan Bheemajiyani Jul 14 '14 at 08:16
  • 1
    @Krishna I am 99% sure the problem here is using a `null` layout (and further code aspects related to doing that). – Andrew Thompson Jul 14 '14 at 08:18
  • @AndrewThompson yes bro u had point then we can change it to any layout na like even box layout :) – Kishan Bheemajiyani Jul 14 '14 at 08:20
  • as @AndrewThompson said try change layout instead and see. – Kishan Bheemajiyani Jul 14 '14 at 08:20

0 Answers0