0

In a basic GUI I'm creating I'm trying to extend JPanel and setting BoxLayout as a layout. Here's what I'm trying to do:

public class TestPanel extends JPanel {

    public TestPanel() {
        super();
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    }
}

I've recently discovered that this cannot be used as a parameter until the current instance is fully constructed; yet, I've seen this kind of code frequently in examples I've found around the net. Does it mean I need to do something like this to be sure everything is going as expected?

public class TestPanel extends JPanel {

    private TestPanel() {
        super();
    }

    public static TestPanel create() {
        TestPanel panel = new TestPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        return panel;
    }

}

EDIT: To be more clear, here's the issue I'm referring to. I'm not sure those consideration apply to my case, but I would've thought so.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
gcali
  • 1,178
  • 1
  • 16
  • 26
  • At that point (1) the super constructor is finished, and (2) all field initializations are done. Child instances of TestPanel not yet. But it is safe enough. – Joop Eggen Jul 06 '15 at 10:25
  • What is the error you are getting in the initial code? – Blip Jul 06 '15 at 10:28
  • @Blip I'm not getting any error right now, but not getting any error is different from having correct code, isn't it? @JoopEggen Does that mean that I can let `this` escape if it's in the last instruction of the constructor? – gcali Jul 06 '15 at 10:30
  • As far as my understanding goes, `this` can perfectly be used in constructors. So, I don't find anything wrong with the first code. Moreover the constructor does not actually construct the object but it is used to initialise the instance `this`. – Blip Jul 06 '15 at 10:34
  • I don't see anything wrong or bad in code. You can use this in constructor freely. – Aleksandar Jul 06 '15 at 10:34
  • @Blip Here's the issue I'm referring to; maybe I should read a bit more about it, seems I got it wrong! http://stackoverflow.com/questions/3705425/java-reference-escape – gcali Jul 06 '15 at 10:35
  • 2
    Swing is single threaded, so you should not have that particular problem with threads (as long as swing is used correctly, and if it's not then that one place is not your only problem). – kiheru Jul 06 '15 at 10:57
  • 1
    Also consider `javax.swing.Box`. – trashgod Jul 06 '15 at 10:59
  • You know that `JPanel` has an [overloaded constructor](http://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html#JPanel%28java.awt.LayoutManager%29) that accepts a `LayoutManager` as a parameter? You could call: `super(new BoxLayout(this, BoxLayout.PAGE_AXIS));`. – Mr. Polywhirl Jul 06 '15 at 11:43
  • @Mr.Polywhirl Wouldn't that be dangerous? I'd leak `this` before the `super` constructor is finished; if `BoxLayout` expects an initialized `Component`, it might not find one! – gcali Jul 06 '15 at 13:05

0 Answers0