Re: Weird window close behavior



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
.



Relevant Pages

  • Re: Constructors And Exceptions
    ... > upon the execution of the destructor. ... > signaling constructor failure via the exception mechanism. ... > My initial reaction to the first book was that the practice is not dubious ...
    (comp.lang.cpp)
  • Re: Declaring many objects
    ... Yes, I would certainly provide such a constructor in practice, but it is ... reasonable text to refer to. ... Francis Glassborow ACCU ...
    (alt.comp.lang.learn.c-cpp)
  • Re: reasons to hate C#
    ... That's bunk. ... To clarify the constructor chaining problem; in C#, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: [PHP] Classes and access to outside variables
    ... Refer to the global on the constructor. ... using a global like that is not a good practice. ... In the following example I would like to be able to access $dbh from ... PHP General Mailing List ...
    (php.general)
  • Re: Declaring many objects
    ... > Yes, I would certainly provide such a constructor in practice, but it is ... > agree that hard coding it into the program is not good practice). ... I've no shortage of reference material but the problem can ... clear idea of what you want to accomplish but no clear idea ...
    (alt.comp.lang.learn.c-cpp)