tags:

views:

41

answers:

2
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class File
{
 private JFrame frame1;
 private JPanel panel1;
 private JPanel panel2;
 private JLabel labelWeight;
 private JLabel labelHeight;




    File()
    {
     frame1 = new JFrame();
     panel1 = new JPanel();
     panel2 = new JPanel();
     labelWeight = new JLabel("Weight :");
     labelHeight = new JLabel("Height :");


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     panel1.setLayout(new FlowLayout());
     panel1.add(labelWeight);

     panel2.setLayout(new FlowLayout());
     panel2.add(labelHeight);

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     frame1.setLayout(new BoxLayout(frame1,BoxLayout.X_AXIS));
     panel1.setAlignmentY(0);
     panel2.setAlignmentY(0);

     frame1.add(panel1);
     frame1.add(panel2);


     frame1.setSize(400, 200);
     frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);
     frame1.setVisible(true);
    }


    public static void main (String args[])
    {
     new File();
    }

}   

It gives BoxLayout Sharing error at runtime

+1  A: 

Swing components should be created in the Event Dispatch Thread. Try this in your main():

javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        new File();
    }
});

But your problem may be the same as this question.

Arnold Spence
Now it gives Exception in thread "AWT- Event Queue 0 " and java.awt.AWTError:BoxLayout sharing error
subanki
I updated my answer with a link to another similar question. You should keep the modification to main though.
Arnold Spence
+2  A: 

Generally, LayoutManagers are set on a JPanel. I guess JFrame implements this method to forward it to the content pane of the frame. I would suggest you try:

Container contentPane = frame1.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.X_AXIS)); 

If you still have problems take a look at the Swing tutorial on How to Use Box Layout for working examples.

camickr
can i add panel to contentpane ???
subanki
@subanki - yes, actually you must. `JFrame.add()` and `JFrame.setLayout()` are just delegates; they call the same named method of the result of `getContentPane()` (if rootPaneCheckingEnabled is true).
Carlos Heuberger