X-Received: by 2002:ac8:1997:: with SMTP id u23mr975823qtj.11.1551390728029; Thu, 28 Feb 2019 13:52:08 -0800 (PST) X-Received: by 2002:a25:9d91:: with SMTP id v17mr1582446ybp.144.1551390727827; Thu, 28 Feb 2019 13:52:07 -0800 (PST) Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!m21no204379qta.0!news-out.google.com!o7ni57qta.1!nntp.google.com!m21no204374qta.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Thu, 28 Feb 2019 13:52:07 -0800 (PST) In-Reply-To: <3167672b-6941-4802-acd7-3b7defe06348@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=84.74.101.34; posting-account=UjEXBwoAAAAOk5fiB8WdHvZddFg9nJ9r NNTP-Posting-Host: 84.74.101.34 References: <8617d379-1659-4582-94fe-6ffb95c975e4@googlegroups.com> <3167672b-6941-4802-acd7-3b7defe06348@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <91d4e209-bbe3-4fe1-9506-7984f8ed64cc@googlegroups.com> Subject: Re: it's Closeable, but I don't want to close() it yet. From: bursejan@gmail.com Injection-Date: Thu, 28 Feb 2019 21:52:08 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Lines: 59 Xref: csiph.com comp.lang.java.programmer:38766 If you need caching, you can cache on icon level. The same ImageIcon instance can be used in multiple=20 places, right? On Thursday, February 28, 2019 at 10:49:43 PM UTC+1, burs...@gmail.com wrot= e: > Hint: Don't use MyClass.class.getResourceAsStream. > Use getResource which gives you an URL, and provide > this to ImageIcon. >=20 > Swing will create a background worker, and load the > ImageIcon for you. getResource doesn't throw > any error, but it might return null: >=20 > URL url =3D clazz.getResource("relative path to icon"); > if (url!=3Dnull) { > ImageIcon icon=3Dnew ImageIcon(url); > /* do something with the icon, > it should already be loaded */ > } else { > /* your setup is wrong or programming error > wrong class or wrong relative path */ > } >=20 > On Thursday, February 28, 2019 at 9:11:41 PM UTC+1, Eric Douglas wrote: > > On Wednesday, February 27, 2019 at 10:52:01 AM UTC-5, Andreas Leitgeb w= rote: > > > In my application there exists an entity that is Closeable. > > > It is kept in some class, and other parts of my application > > > request a ref to the entity and do actions on it, then drop > > > their ref, leaving the entity intact. > > >=20 > > > Everything runs fine, except eclipse warns me about spots > > > where the entity is requested, used, and then dropped. > > > Eclipse thinks it might need to be close()d. > > >=20 > > > Apart from ignoring the warning per eclipse settings or > > > adding @SuppressWarnings, is there maybe a way to tell > > > the compiler that a certain ref is not meant to be close()d? > > > Letting it know that - despite the entity's ultimate fate of > > > being eventually closed - this is not yet the time&place for it? > >=20 > > Here's where it gets ugly. I've had the "Potential resource leak" erro= r just turned off, now I'm trying to turn it on and change code to make it = go away. I have a class with class level final public static ImageIcon var= iables. I initialize each ImageIcon in a static block which calls MyClass.= class.getResourceAsStream() to open an InputStream to an image file which i= s packaged into the jar (so if this ever gets an error you've got serious p= roblems). I then pass that InputStream into another method in a utility cl= ass which uses ImageIO to generate the ImageIcon. That method closes the s= tream. Eclipse complains the stream may be leaked in this class. I take t= he close out of that class and try to change this to use the try-with-resou= rces. I previously needed no try statement in this static block. When I w= rote a try-with-resources for a SQL Statement it didn't ask for any excepti= on clause, but here it's telling me I need to catch IOException. Now of co= urse my assignment of the ImageIcon must be inside this try block, and if I= add a catch clause it's complaining it may not be initialized, so I have t= o initialize it to null in the constructor and remove the final.