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

#1 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2068
  • View blog
  • Posts: 4,301
  • Joined: 11-December 07

Who teaches setLayout(null)?

Post icon  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.

Who teaches this stuff?

And why?
Is This A Good Question/Topic? 0
  • +

Replies To: Who teaches setLayout(null)?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Who teaches setLayout(null)?

Posted 26 July 2013 - 04:50 AM

I think you're asking, "Where do these bad habits (with a focus on null layout managers) come from?"

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.
Was This Post Helpful? 2
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: Who teaches setLayout(null)?

Posted 30 July 2013 - 11:39 PM

View Postcfoley, 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.
Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2068
  • View blog
  • Posts: 4,301
  • Joined: 11-December 07

Re: Who teaches setLayout(null)?

Posted 31 July 2013 - 01:08 AM

But you still don't get absolute control, not unless you bundle the desired look and feel with your application. I suspect most beginners don't do that.
Was This Post Helpful? 0
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: Who teaches setLayout(null)?

Posted 31 July 2013 - 06:04 AM

Sure you do. It's only when you move from the cross platform look and feel that things start going awry.
Was This Post Helpful? 1
  • +
  • -

#6 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2068
  • View blog
  • Posts: 4,301
  • Joined: 11-December 07

Re: Who teaches setLayout(null)?

Posted 31 July 2013 - 06:28 AM

I might have to concede that one. :)

Do fonts throw things out or are standard ones bundled with the JRE? I can't remember.
Was This Post Helpful? 1
  • +
  • -

#7 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: Who teaches setLayout(null)?

Posted 31 July 2013 - 06:37 AM

View Postcfoley, on 31 July 2013 - 01:28 PM, said:

I might have to concede that one. :)/>

Do fonts throw things out or are standard ones bundled with the JRE? I can't remember.


Ah, crap. You're right about the font. Some fonts might over or undersize components.
Was This Post Helpful? 1
  • +
  • -

#8 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2068
  • View blog
  • Posts: 4,301
  • Joined: 11-December 07

Re: Who teaches setLayout(null)?

Posted 31 July 2013 - 06:57 AM

Still, it's easier to bundle a standard font than to bundle a whole L&F.

My problem isn't really with the edge cases where null layout is useful. It's with the hordes of beginners who choose it as the default option.
Was This Post Helpful? 2
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

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.
Was This Post Helpful? 2
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,153
  • Joined: 27-December 08

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.
Was This Post Helpful? 1
  • +
  • -

#11 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2068
  • View blog
  • Posts: 4,301
  • Joined: 11-December 07

Re: Who teaches setLayout(null)?

Posted 31 July 2013 - 10:25 AM

I think you might be onto something Ryano121
Was This Post Helpful? 0
  • +
  • -

#12 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 168
  • Joined: 27-May 13

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.
Was This Post Helpful? 0
  • +
  • -

#13 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2068
  • View blog
  • Posts: 4,301
  • Joined: 11-December 07

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.
Was This Post Helpful? 2
  • +
  • -

#14 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: Who teaches setLayout(null)?

Posted 31 July 2013 - 03:00 PM

View Postaxel1994, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1