java - Add JScrollPane to JPanel -


i try make scrollbar jpanel when run, scrollbar not show up.

edited: latest code, updated contentpane layout.

this main panel:

contentpane = new jpanel(); contentpane.setbackground(color.white); contentpane.setborder(new emptyborder(5, 5, 5, 5)); contentpane.setpreferredsize(new dimension(1200, 400)); setcontentpane(contentpane); 

this jpanel inside main panel:

bigpanel = new jpanel(); bigpanel.setpreferredsize(new dimension(1137, 520));  bigpanel.setbackground(new color(224, 255, 255));  jscrollpane scrollpane = new jscrollpane(bigpanel); scrollpane.setpreferredsize(new dimension(600, 600));  scrollpane.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_always); scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_always);    contentpane.add(scrollpane,"1, 2, 6, 1, center, default");    bigpanel.setlayout(new formlayout(new columnspec[] {...} bigpanel.add(button1); bigpanel.add(button2);..... 

above code written under:

public ui2(){...} 

this launch application code:

public static void main(string[] args) {     eventqueue.invokelater(new runnable() {         public void run() {             try {                  ui2 frame = new ui2();                                       frame.pack();                 frame.setdefaultcloseoperation(ui2.exit_on_close);                 frame.setsize(1376,788);                                 frame.setvisible(true);              } catch (exception e) {                 e.printstacktrace();             }         }     }); } 

bigpanel has many buttons. need vertical , horizontal scrollbar within bigpanel.

this screenshot after run no scrollbar shown: bigpanel green background

edited: latest screenshot, bigpanel shows empty after wrote code above. enter image description here

i believe problem contentpane.setlayout(null);, means jscrollpane has not definable size , not been shown because of it.

avoid using null layouts, pixel perfect layouts illusion within modern ui design. there many factors affect individual size of components, none of can control. swing designed work layout managers @ core, discarding these lead no end of issues , problems spend more , more time trying rectify

also contentpane.add(bigpanel,"1, 2, 6, 1, center, default"); should contentpane.add(scrollpane,"1, 2, 6, 1, center, default");. once correct layout issue, scroll pane should become visible

see laying out components within container more ideas


Comments