3

Could you help me understand what is going on here. I consulted Javadoc: JFrame has setLayout method. So, what sharing error springs out is a mystery to me.

public class View extends JFrame {
    public View(){

        // LayoutManager for the whole frame.
        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    }
}

Result

Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
    at javax.swing.BoxLayout.checkContainer(BoxLayout.java:465)
    at javax.swing.BoxLayout.invalidateLayout(BoxLayout.java:249)
    at java.awt.Container.invalidate(Container.java:1583)
    at java.awt.Component.invalidateIfValid(Component.java:2957)
    at java.awt.Container.setLayout(Container.java:1484)
    at javax.swing.JFrame.setLayout(JFrame.java:605)
    at View.<init>(View.java:16)
    at Init.main(Init.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Michael
  • 4,273
  • 3
  • 40
  • 69
  • duplicated: http://stackoverflow.com/questions/761341/boxlayout-cant-be-shared-error – alex Jul 19 '14 at 13:24

1 Answers1

4

Try this one on JFrame#getContentPane()

this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));

Read more How to Use BoxLayout


All the components are added in JFrame's content pane.

Read more Adding Components to the Content Pane

Here is the pictorial representation how JFrame looks like

enter image description here


EDIT

From comments:

Well, not clear anyway. I analyze it like this: BoxLayout class needs to know it target. JFrame has setLayoutt method and needs to know its layout.

this.setLayout(manager) internally calls getContentPane().setLayout(manager);

The below line

this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

is converted to below line that is not correct.

this.getContentPane().setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

For more detail have a look at Source code

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Working. But I can't understand anything. Could you clarify what exactly did I share? – Michael Jul 19 '14 at 13:29
  • Well, not clear anyway. I analyze it like this: BoxLayout class needs to know it target. JFrame has setLayoutt method and needs to know its layout. In all examples I've seen objects invoking setLayout and the target to BoxLayout are the same. Like this: listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS)); this is from http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html Well, in this light I can't still see the reason why I should getContentPane. – Michael Jul 19 '14 at 13:52
  • `this.setLayout()` internally calls `this.getContentPane().setLayout()` that is applying layout on content pane on which JFrame adds the component. – Braj Jul 19 '14 at 13:54
  • Here is [Source code](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/JFrame.java#JFrame.setLayout%28java.awt.LayoutManager%29) and I hope now its clear to you. – Braj Jul 19 '14 at 13:55