Re: Weird window close behavior
- From: RedGrittyBrick <RedGrittyBrick@xxxxxxxxxxxxx>
- Date: Sat, 28 Jun 2008 22:09:57 +0100
Lew wrote:
Knute Johnson wrote:This is the part that I am curious about. What makes it better practice to 'build' the GUI someplace other than the constructor?
I must admit I find it very natural to "construct" the GUI elements in a GUI object's constructor. For example: to instantiate JTextFields in the constructor of a subclass of JPanel.
Honestly, I'm just curious and want to understand the thinking behind your method. Are these best practices written down somewhere?
The thinking is that I should see how to follow the advice of these pundits, which indeed is written down in many places, none of which I can quote off the top of my hand. The thinking is that I keep reading how dangerous it is to use partially constructed objects, how there can be uninitialized variables or thread dangers, how state of even final variables can appear to change, and how one should be in the habit of limiting constructors to construction. Apparently this is a new concept for you, but I must have seen it in a dozen places.
Doesn't the issue with partially constructed objects only arise if your constructor passes a reference to itself to other methods?
E.g. http://nat.truemesh.com/archives/000222.html
Or
class ExamplePanel extends JPanel {
ExamplePanel() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
// ^^^^ partially constructed!
add(new JLabel("One"));
add(new JLabel("Two"));
add(new JLabel("Three"));
}
}
Since the ancestral JComponent() constructor will have been completed before BoxLayout() gets to make use of it - isn't it a fully constructed JComponent so far as BoxLayout is concerned?
--
RGB
.
- Follow-Ups:
- Re: Weird window close behavior
- From: Lew
- Re: Weird window close behavior
- References:
- Weird window close behavior
- From: mrstephengross
- Re: Weird window close behavior
- From: Daniel Pitts
- Re: Weird window close behavior
- From: mrstephengross
- Re: Weird window close behavior
- From: Lew
- Re: Weird window close behavior
- From: Knute Johnson
- Re: Weird window close behavior
- From: Lew
- Re: Weird window close behavior
- From: Knute Johnson
- Re: Weird window close behavior
- From: Lew
- Re: Weird window close behavior
- From: Knute Johnson
- Re: Weird window close behavior
- From: Lew
- Weird window close behavior
- Prev by Date: java.awt.image.RescaleOp.filter() causes VM crash: EXCEPTION_ACCESS_VIOLATION
- Next by Date: Re: Weird window close behavior
- Previous by thread: Re: Weird window close behavior
- Next by thread: Re: Weird window close behavior
- Index(es):
Relevant Pages
|