Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #37635

Re: How do you update the view space of a JTable?

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: How do you update the view space of a JTable?
Date 2017-11-01 10:37 -0400
Organization A noiseless patient Spider
Message-ID <otcm6d$q0u$1@dont-email.me> (permalink)
References (6 earlier) <66fc1800-d494-43a1-963d-a1a54a99f631@googlegroups.com> <ot9qht$3bf$1@dont-email.me> <28c95541-33f4-4502-af4e-31d82c672841@googlegroups.com> <otap2d$uss$1@dont-email.me> <fd194d27-8172-4ba2-b90e-44843e950cf8@googlegroups.com>

Show all headers | View raw


On 11/1/2017 9:29 AM, Eric Douglas wrote:
> On Tuesday, October 31, 2017 at 5:13:57 PM UTC-4, Eric Sosman wrote:
>> [...]
>>       - Venial: Various constructors call potentially overridable
>> methods, which could be a problem if any of your classes are someday
>> extended.  You could cure this by making the involved classes final,
>> or by making the methods final, or (since you probably don't need to
>> override some of them) by explicitly calling the superclass' method,
>> as in `super.getContentPane()' instead of just `getContentPane()'.
>> Or, you could just hope no one will ever extend the class and find
>> some reason to override setAutoResizeMode() or whatever.
>>
> I'm not sure exactly where or why this is bad form?  Obviously the getContentPant() is from Oracle's JFrame which is overridable, and I've found many Oracle classes and methods can be overridden including ones which probably should not be.  Of course this is just a test class so it doesn't hurt for this class and it's subclasses to be final.  Many if not most test classes on the internet seem to extend JFrame, though I've tried not to do that in my own tests, so rather than super.getContentPane() it should really be testFrame1.getContentPane().  I haven't used final on any classes or methods personally since I haven't seen confirmation of it improving performance and I'm not sure when you really need to ensure that no one overrides a method which must be visible.

     The "bad form" in calling an overridable method from a constructor
is, well, that the method might be overridden.  Specifically, it might
be overridden in a subclass, in which case you'd wind up calling an
instance method of an object that is not yet fully constructed.  This
could lead to unpleasant surprises, like observing a `final' field to
have different values at different times, or getting NPE when the
subclass' overriding method does something "safe" like

	class Super {
	    Super() {
	        // ...
	        theMethod();
	    }

	    void theMethod() {
	        // ...
	    }
	}

	class Sub extends Super {
	    private final List<String> names = new ArrayList<>();
	    @Override
	    void theMethod() {
	        names.add("Clark Kent");
	    }
	}

     So: The rule for avoiding such nastiness is to call no overridable
methods from a constructor.  Three ways to accomplish this (there may
be others) are

     - Make the class `final' and don't override the method.  Since no
       one can extend the `final' class, no one can override the method.

     - Override the method, but make your override `final'.  The class
       can now be extended, but no one can override the method.

     - Call `super.theMethod()' explicitly.  Even if a subclass overrides
       the method, the call will still go to super's implementation.

     I can't find "testFrame1" in your code, so I don't know what your
reference to it means.

>>       - Venial: Various constructors leak the `this' reference before
>> `this' is fully constructed.  The transgression would ordinarily be
>> worse than venial, but adding event listeners "should probably be"
>> safe since they're unlikely to be called until a good deal later.
>> IMHO this is an infelicity in the architecture of Swing/AWT: You
>> would *like* a constructor to deliver a fully-functional ready-to-
>> rumble instance of a JFrame or whatever, but if you're really not
>> supposed to register event listeners in a constructor ...  The
>> squeaky-clean approach, I guess, is to have a post-construction
>> init() method, which strikes me as Ugly.  Or ignore it and hope.
>>
> I'm not sure where exactly you're referring to this or how exactly it could be improved.  Am I just not understanding or do you not like how Oracle does GUI? ie JTable.setModel references this and is called from the constructor.

     It is dangerous to expose a reference to an object that is not yet
fully built.  An object is not fully built until its entire chain of
constructors has completed.  Something like `addActionListener(this)'
publishes an object reference to code in another class, and if done
in a constructor it is obviously done before the constructor chain
has finished.  The exposed object is incomplete, there are (at least
potentially) threading problems with memory visibility and so on, and
the program is in peril.

     As I said, you will "probably" be all right with this one (in this
case), because events won't begin to fire for a while yet and there's
"no good reason" for an event source to do anything with your object
until they do.  Daniele Futtorovic's dodge of registering listeners in
addNotify() is an approach that hadn't occurred to me; seems clever.

>>       - Style: Use `intValue' instead of `new Integer(intValue)' and
>> let auto-boxing take care of wrapping it.  (May produce a tiny gain
>> in efficiency, too.)
>>
> That actually may be required for an older version of Java?  I just took that out altogether and the compiler just accepted the method's int return value for the constructor's Object parameter.  It seems the only reason anymore to explicitly convert ie int to Integer is if you need to call a method, though most Integer methods can be done with static calls.

     You're right: Auto-boxing requires a Java 1.5 or later.  If you must
accommodate Java 1.4, you can't auto-box.  (Notes: 1.5 came out thirteen
years ago, 1.4 went off public support nine years ago, and 1.4 went off
pay-Oracle-for-special-treatment-support four and a half years ago.)

     I have no idea what you mean by "static calls," nor why they might
be relevant to auto-boxing.

-- 
esosman@comcast-dot-net.invalid
Eleven hundred seventy-six days to go.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-10-17 08:47 -0700
  Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-10-18 06:11 -0700
  Re: How do you update the view space of a JTable? Daniele Futtorovic <da.futt.news@laposte.net> - 2017-10-30 17:41 +0100
    Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-10-30 10:29 -0700
      Re: How do you update the view space of a JTable? Daniele Futtorovic <da.futt.news@laposte.net> - 2017-10-31 11:10 +0100
        Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-10-31 04:46 -0700
          Re: How do you update the view space of a JTable? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-10-31 07:59 -0400
            Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-10-31 05:21 -0700
              Re: How do you update the view space of a JTable? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-10-31 08:32 -0400
                Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-10-31 06:13 -0700
                Re: How do you update the view space of a JTable? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-10-31 17:13 -0400
                Re: How do you update the view space of a JTable? Daniele Futtorovic <da.futt.news@laposte.net> - 2017-10-31 23:33 +0100
                Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-11-01 06:29 -0700
                Re: How do you update the view space of a JTable? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-11-01 10:37 -0400
                Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-11-01 08:37 -0700
                Re: How do you update the view space of a JTable? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-11-01 13:20 -0400

csiph-web