13 Replies - 9509 Views - Last Post: 31 July 2013 - 03:00 PM

Who teaches setLayout(null)?

Posted 26 July 2013 - 04:20 AM

We get so many people asking for help with their Java GUIs and a lot of them are using null layouts. I did a Java 101 class at uni and I was taught layout managers. I haven't specifically gone through tutorials but any I have glanced at focus on layout managers. I know the tutorials Sun wrote did mention null layouts but they were clear to caution against them.

Re: Who teaches setLayout(null)?

There are many examples of bad habits being "taught" throughout the web in most of the 2nd party Java tutorials I've reviewed. I'm embarrassed to say that I considered myself an intermediate Swing user until someone here (CasiOo?) challenged my practice of not running the GUI on the EDT. I simply hadn't learned Swing that way, and I didn't know it was a good thing to do or why, because the web tutorials I'd learned from didn't do it that way.

(I believe the popular series of 5 Swing tutorials here on DIC do not mention the EDT, but it's been a while, and I'd have to verify that.)

Why? For expediency? Simplicity? Your guess is as good as mine. But I think the tutorial writers have done their readers a disservice by providing examples that fundamentally do things the wrong way.

I assume the common use of null layouts to demonstrate other facets of using Swing components is a similar shortcut. The understanding and addition of other layouts might cloud the author's intention and confuse the reader. Adding other layout managers to those examples is left to the reader to pursue, discover, and experiment with. Those tutorials should at least mention the use of other layout managers as desirable options to explore further, even suggesting which ones to try and why, but few do.

Here at DIC and wherever we have the opportunity, we should challenge ourselves to do better.

Re: Who teaches setLayout(null)?

Posted 30 July 2013 - 11:39 PM

cfoley, on 26 July 2013 - 11:20 AM, said:

We get so many people asking for help with their Java GUIs and a lot of them are using null layouts. I did a Java 101 class at uni and I was taught layout managers. I haven't specifically gone through tutorials but any I have glanced at focus on layout managers. I know the tutorials Sun wrote did mention null layouts but they were clear to caution against them.

Who teaches this stuff?

And why?

Anyone who wants absolute control over a layout that will not be resized.

Re: Who teaches setLayout(null)?

Posted 31 July 2013 - 07:32 AM

I think this all comes from the hoards of online tutorials from other beginners who don't know what they're doing. I'm yet to to see a proper Java book teach null layouts and I'm pretty confident they wouldn't teach it in college courses, so I guess if people are learning the language in a sensible manner they won't come across this. It's just a shame that its so easy to learn from the wrong sources.

Re: Who teaches setLayout(null)?

Posted 31 July 2013 - 07:56 AM

I think a lot of it comes from newbies that want to write GUIs but don't want to invest the time to learn how to properly use the LayoutManagers. At least, that's where I mostly see null layout used in the forums.

Re: Who teaches setLayout(null)?

Re: Who teaches setLayout(null)?

Posted 31 July 2013 - 10:43 AM

You could make your own calculations so you can resize the screen without problems when using the null layout.

And let's say you are making some program which requires objects to appear anywhere on the screen?
For example a ball that moves around the screen.
Wouldn't it be the most easy to just use a null layout and draw the ball on the location you want it to be?

I do think/believe that for most other things a LayoutManager is more appropriate.

Re: Who teaches setLayout(null)?

Posted 31 July 2013 - 10:54 AM

Quote

You could make your own calculations so you can resize the screen without problems when using the null layout.

Yes, you're absolutely right. But what you have done there is code up the logic for a layout manager, albeit a very specific one. You'd be better off going one step further and encapsulating the resize logic in a class that extends LayoutManager or LayoutManager2.

Quote

And let's say you are making some program which requires objects to appear anywhere on the screen?
For example a ball that moves around the screen.
Wouldn't it be the most easy to just use a null layout and draw the ball on the location you want it to be?

I can see what you are getting at here but this example sounds more suited to drawing graphics on a canvas than using gui widgets and LayoutManagers.

There must be good examples for what you are talking about. The best I can come up with is Office's paperclip from days gone by or help bubbles that follow you about on online forms. I think Swing has layout managers designed for layers. They are what I would use here.

Re: Who teaches setLayout(null)?

Posted 31 July 2013 - 03:00 PM

axel1994, on 31 July 2013 - 05:43 PM, said:

And let's say you are making some program which requires objects to appear anywhere on the screen?
For example a ball that moves around the screen.
Wouldn't it be the most easy to just use a null layout and draw the ball on the location you want it to be?

You're not thinking about it the right way. Never think of drawing in your app as just drawing to the screen. You draw to a component which resides somewhere on the screen.