I'm trying to setup Boxlayout of Jpanel inside another JPanel. But i can't figure out how to solve this error
Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
at javax.swing.BoxLayout.checkContainer(BoxLayout.java:464)
at javax.swing.BoxLayout.invalidateLayout(BoxLayout.java:248)
at javax.swing.BoxLayout.addLayoutComponent(BoxLayout.java:281)
at java.awt.Container.addImpl(Container.java:1120)
at java.awt.Container.add(Container.java:410)
at chatmsg1.ProfilePanel.<init>(ProfilePanel.java:63)
at chatmsg1.ProfilePanel.main(ProfilePanel.java:108)
Java Result: 1
Here is the Code ! Any help will be 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 is another JPanel which adds image to this panel. Plus if there is any method to set this panel auto adjustable with resizing main Jframe with all it's components, please let me know. I'm kind of learning Java and as a newbie i don't know about it very much. I will be very grateful for any help!!! Thanks
BoxLayout
needs to be passed the container, but labelpanel is null
when you create the layout manager. So you need to create the panel before you can create the layout manager:
this.labelpanel = new JPanel();
labelpanel.setLayout(new BoxLayout(labelpanel, BoxLayout.PAGE_AXIS));
By the way, get rid of the null
layout you use in another part of the code - those are a bad idea so it's better to get in the habit of always using layout managers right from the start.