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.