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


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

Re: How to extend an array

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: How to extend an array
Date 2016-05-31 11:52 -0400
Organization A noiseless patient Spider
Message-ID <nikc0f$b0v$1@dont-email.me> (permalink)
References <ae727260-8c33-4648-8995-f387a6eb8b52@googlegroups.com>

Show all headers | View raw


On 5/31/2016 10:52 AM, Eric Douglas wrote:
> This looks funny but seems to work.  Does it make sense, is there better syntax, or anything I'm missing?
> I have a custom GUI object which needs to generate custom events, and a more complex version of that object which needs to generate those events plus additional events.  Here's a simplified sample.
> [... code snipped; see up-thread ...]

     I don't see why Class2 needs its own list of listeners, nor its
addExtendedListener() method.  Since Listener2 extends Listener1, a
Listener2 "is a" Listener1 and can be handled just fine by Class1's
addListener() method.  Unless there's some additional concern that
you've simplified away, I think you're working too hard.

     Side-issue: Since Class1 and Class2 extend JFrame, they are both
Swing components.  Swing components are not thread-safe (by and large),
so it's not safe to manipulate them from more than one thread.  That
thread should be the Event Dispatch Thread or EDT.  Your code, though,
does a *lot* of manipulation of these Swing components on a non-EDT
thread: It constructs them, resizes their windows, and makes them
visible, all from the startup thread that calls main().  Don't Do That.
(To be fair to you, Sun themselves didn't at first realize this was a
no-no, and some of the early Swing examples commit this same error.
Pay attention to more up-to-date examples, that's all I can suggest.)

     One simple way to ensure that construction happens on the EDT:

	public static void main(String[] args) {
	    SwingUtilities.invokeLater(new Runnable() {
	        Class2 myWindow = new Class2();
	        ... et cetera ...
	        myWindow.setVisible(true);
	    });

(The fashion nowadays is to use a "lambda expression" instead of an
explicit "new Runnable()", but I didn't want to risk spooking you.)

     For "belt and suspenders" safety, I also like to sprinkle

	assert SwingUtilities.isEventDispatchThread();

in the constructors of my Swingthings, and in some of their public
methods as well -- Better to get crashes during development and test
than to get baffling unreproducible failures at deployment ...

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

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


Thread

How to extend an array Eric Douglas <e.d.programmer@gmail.com> - 2016-05-31 07:52 -0700
  Re: How to extend an array Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-05-31 11:52 -0400
    Re: How to extend an array Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-05-31 11:56 -0400
      Re: How to extend an array Eric Douglas <e.d.programmer@gmail.com> - 2016-05-31 09:19 -0700
        Re: How to extend an array Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-05-31 14:01 -0400
    Re: How to extend an array Eric Douglas <e.d.programmer@gmail.com> - 2016-05-31 09:09 -0700
      Re: How to extend an array Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-05-31 14:11 -0400
        Re: How to extend an array Eric Douglas <e.d.programmer@gmail.com> - 2016-05-31 11:25 -0700
          Re: How to extend an array Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-05-31 17:01 -0400
            Re: How to extend an array Eric Douglas <e.d.programmer@gmail.com> - 2016-06-01 04:57 -0700
  Re: How to extend an array Roedy Green <inquiry@mindprod.com> - 2016-05-31 21:56 -0700
    Re: How to extend an array Eric Douglas <e.d.programmer@gmail.com> - 2016-06-01 04:49 -0700
  Re: How to extend an array Robert Klemme <shortcutter@googlemail.com> - 2016-06-01 23:07 +0200
    Re: How to extend an array Eric Douglas <e.d.programmer@gmail.com> - 2016-06-03 05:38 -0700
      Re: How to extend an array Robert Klemme <shortcutter@googlemail.com> - 2016-06-04 19:17 +0200
    Re: How to extend an array Eric Douglas <e.d.programmer@gmail.com> - 2016-06-03 08:52 -0700

csiph-web