Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: What do you think of my CropImage class Date: Thu, 21 Jul 2016 09:35:15 -0400 Organization: A noiseless patient Spider Lines: 48 Message-ID: References: <87twfk93vu.fsf@Equus.decebal.nl> <8737n36ucx.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 21 Jul 2016 13:35:28 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="2d081e2f7155c28005d4472f67c2f43d"; logging-data="12049"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+hqSkm23lsSWpBZNDyF3ej" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: <8737n36ucx.fsf@Equus.decebal.nl> Cancel-Lock: sha1:cd/bUk2IbqwGWN+fT6TvgymN98w= Xref: csiph.com comp.lang.java.programmer:35043 On 7/21/2016 5:58 AM, Cecil Westerhof wrote: > I was not completely clear :'-( so here some more info. > > I have no real experience with Swing and had an itch to scratch. > That is why I wrote this program: to get some experience with > Swing and get a little tool to make my live a little easier. > [...] I have not studied your code closely, but one thing jumps out: You're doing Swingthings on the wrong thread. Almost all of Swing is thread-unsafe, and almost all manipulation of Swing objects must occur on the Event Dispatching Thread, or EDT. Your code breaches this requirement: - It constructs a pair of JLabels on whatever thread loads and initializes the class. (And the JLabels are static, which is so very odd as to be downright weird. Have you thought this through?) - It constructs a bunch of Swing components on whatever thread executes main(). Furthermore, it manipulates those components (setting bounds, etc.), and even makes the JFrame visible -- all while running on the same wrong thread. Don't Do That. See http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html ... for a discussion of the issues, and how to deal with them. (I rather suspect that the "flicker" you report may have its origins in, er, wrong-threadedness.) A few other random remarks: - You mention that "for me pack did not work," which is sort of weird. I don't know what you tried before you settled on absolute positioning, but I wonder: Could your layout troubles have anything to do with the fact that CropImage extends Panel instead of JPanel? - Maybe not, but then: What is the purpose of the DispImage class? What does it accomplish that couldn't just as well be done in the containing CropImage class (if the latter were a JPanel)? - There's a handy MouseAdapter class that can eliminate some verbosity in your MouseListener implementation. -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM