java - How To solve BoxLayout can't be shared error while setting it for JPanel? -


i'm trying setup boxlayout of jpanel inside jpanel. can't figure out how solve error

    exception in thread "main" java.awt.awterror: boxlayout can't shared     @ javax.swing.boxlayout.checkcontainer(boxlayout.java:464) @ javax.swing.boxlayout.invalidatelayout(boxlayout.java:248) @ javax.swing.boxlayout.addlayoutcomponent(boxlayout.java:281) @ java.awt.container.addimpl(container.java:1120) @ java.awt.container.add(container.java:410) @ chatmsg1.profilepanel.<init>(profilepanel.java:63) @ chatmsg1.profilepanel.main(profilepanel.java:108)     java result: 1 

here code ! appreciated !!

import java.awt.borderlayout; import java.awt.gridlayout; import javax.management.immutabledescriptor; import javax.swing.boxlayout; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jlayeredpane; import javax.swing.jpanel;   class profilepanel extends jpanel{  jlabel jl1,jl2,jl3,jl4,jl5,jl6,jl7,jl8,jl9,jl10;  imagepanel ip= new imagepanel();  jpanel jp1= new jpanel(new borderlayout());  jpanel labelpanel;  static int textwidth=100;   static string uname, uage, usex, udept,   uheight="jjhkjhiuhiuhiuhiuhihihuihiuhiuhuhiuhiuhihihiuhhihhuhih",ubatch, ucontact, uemail; public profilepanel() {     this.labelpanel = new jpanel(new boxlayout(labelpanel, boxlayout.page_axis));       string html1 = "<html><body style='width: ";     string html2 = "px'>";      labelpanel.setsize(400, 400);     jl1= new jlabel(""+html1+""+textwidth+html2+"<h1>"+uname+"</h1>"); //jl1.setsize(jl1.getpreferredsize());      jl2= new jlabel(""+html1+""+textwidth+html2+uage); //jl2.setsize(jl2.getpreferredsize());      jl3= new jlabel(""+html1+""+textwidth+html2+usex); //jl3.setsize(jl3.getpreferredsize());      jl4= new jlabel(""+html1+""+textwidth+html2+udept);  // jl4.setsize(jl4.getpreferredsize());      jl5= new jlabel(""+html1+""+textwidth+html2+uheight); //jl5.setsize(jl5.getpreferredsize());      jl6= new jlabel(""+html1+""+textwidth+html2+ubatch); //jl6.setsize(jl6.getpreferredsize());      jl7= new jlabel(""+html1+""+textwidth+html2+ucontact); //jl7.setsize(jl7.getpreferredsize());    jl8= new jlabel(""+html1+""+textwidth+html2+uemail); //jl8.setsize(jl8.getpreferredsize());  /* labelpanel.add(jl1,borderlayout.center); labelpanel.add(jl2,borderlayout.south); labelpanel.add(jl3,borderlayout.south); labelpanel.add(jl4,borderlayout.south); labelpanel.add(jl5,borderlayout.south); labelpanel.add(jl6,borderlayout.south); labelpanel.add(jl7,borderlayout.south); labelpanel.add(jl8,borderlayout.south);  */ labelpanel.add(jl1); labelpanel.add(jl2); labelpanel.add(jl3); labelpanel.add(jl4); labelpanel.add(jl5); labelpanel.add(jl6); labelpanel.add(jl7); labelpanel.add(jl8);   jl1.setalignmentx(0); jl2.setalignmentx(0); jl3.setalignmentx(0); jl4.setalignmentx(0); jl5.setalignmentx(0); jl6.setalignmentx(0); jl7.setalignmentx(0);   ip.setimage("icons/1.jpg"); ip.setsize(200, 200); //jl1.setsize(jl1.getpreferredsize()); jp1.add(ip, borderlayout.center); jp1.setpreferredsize(jp1.getpreferredsize()); jp1.setbounds(20, 20, ip.getwidth(), ip.getheight());  //add(jp1,borderlayout.center ); //add(jl1,borderlayout.west); add(jp1); add(labelpanel); labelpanel.setlocation(jp1.getx()+jp1.getwidth()+20,0 ); labelpanel.setbounds(jp1.getx()+jp1.getwidth()+20,0 ,300, 400); setlayout(null); setpreferredsize(getpreferredsize()); setlocation(0, 0); //setbounds(10, 10, 600, 600); setvisible(true);      }          public static void main(string args[]){         profilepanel p1= new profilepanel();         p1.setsize(600, 600);         jframe jf= new jframe("propanel");          jf.setbounds(100, 100, 600, 600);         jf.setlayout(null);         jf.setvisible(true);         jf.setdefaultcloseoperation(jf.exit_on_close);          jf.getcontentpane().add(p1);    // jf.pack();      }     } 

imagepanel jpanel adds image panel. plus if there method set panel auto adjustable resizing main jframe it's components, please let me know. i'm kind of learning java , newbie don't know much. grateful help!!! thanks

boxlayout needs passed container, labelpanel null when create layout manager. need create panel before can create layout manager:

this.labelpanel = new jpanel(); labelpanel.setlayout(new boxlayout(labelpanel, boxlayout.page_axis)); 

by way, rid of null layout use in part of code - bad idea it's better in habit of using layout managers right start.


Comments