Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #3736
| From | "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> |
|---|---|
| Subject | Re: Weird window close be |
| Message-ID | <xaudnRiazPo3NfvVnZ2dneKdnZydnZ2d@bt.com> (permalink) |
| Newsgroups | comp.lang.java.gui |
| References | <JeidnUNXGPIV7PjVnZ2dnUVZ_hWdnZ2d@comcast.com> |
| Date | 2011-04-27 15:46 +0000 |
| Organization | TDS.net |
To: comp.lang.java.gui
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
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24
Back to comp.lang.java.gui | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Weird window close be "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
Re: Weird window close be "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
Re: Weird window close be "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
csiph-web