• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

null Layout Users Anonymous

 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I've created this thread as a safe place for users (or abusers) of setLayout(null) to come and talk about using null Layout. Your goal might be to quit, but don't feel pressured - you should feel comfortable here.
What is it about the null Layout that lures a user? Why do you use it? Is it the sense of control (power) over the display? Is it the freedom (the subversive anarchist anti-establishment rush) of not following the rules (of an orderly Layout Manager)?
If you're concerned about being persecuted, try posting using an alias and connect through a proxy server that hides your IP address. Again, you should feel safe to express yourself here.
Only good will come from our discussions (confessions) and perhaps we'll find god along the way.
Good Luck,
-Dirk Schreckmann
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Why have I used? It's the quick control (the rush). When I just had to quickly get a project completed, and nobody cared if it looked the same on multiple platforms, and the Layout Manager I wanted to use wouldn't cooperate, wouldn't do things my way, that's when I cheated. Hey, I gave that Layout Manager a try - it didn't support me. If it had been there for me, I would have never been open to the guile of null Layout. It wasn't my fault.
Oh, but it was. I'm to blame. It was me. That Layout Manager was there for me - I turned it away. We just weren't communicating well and I didn't have the patience, respect, understanding that the Layout Manager needed from me. I'm bad.
(I need a break from this emotional burst.)
-Dirk Schreckmann
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used to be a chronic null LayoutManger user. But I managed to kick the habit. You can too if only you want it enough!
 
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The layout managers I've had relationships with always do things that I don't want. It seems I have to put a lot of time into them to get things to even start to look the way I want. And then a manager wants something different, and the amount of time to work in the new change is a lot.
With null, I have more control and get things done faster. For the few times I really needed to concern myself with resizing issues, I wrote my own resizing routines and I think things turned out better than if I used a layout manager.
I'm a null layout user. And I see no reason to stop.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Wheaton:
I'm a null layout user. And I see no reason to stop.

Which is why you are having problems such as those you whined about in the Swing forum. Those who live by the sword, die by the sword. The fact is that if you learn how to use layout managers (especially the complex ones) you will achieve WORA. Without layout managers you can write beautiful Windows applications that suck everywhere else (and probably suck on at least some people's computers even if they are using Windows).
 
sharp shooter, and author
Posts: 1913
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the worst thing is when you have a team or people new to Java AWT/Swing and an IDE that uses null layout by default!
A simple solution is to wrap away all that nice functionality so that people don't know that they are using it - with something like this ... GridBagPanel
Be strong ... kick the habit!
 
paul wheaton
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far I'm not convinced.
The problems I'm currently having I am quite sure have nothing to do with using the null layout manager and everything to do with Java using system fonts instead of carrying its own fonts around.
I have invested a great deal of time in layout managers and feel quite comfortable using them. But I feel more productive and more in control with the null layout manager. Especially with portability issues.
But! I've been wrong about so many things throughout my short lifespan, perhaps this is something I should not so quickly poo-poo ....
What, exactly, are the evils of using the null layout manager?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jim Yingst,

I think this is where you come in...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ummmm... why me? I haven't done a lot of GUI for a while, so I don't have a particularly strong opinion on the matter. I've always used layout managers myself - often putting one inside another, ad infinitum. I should probably get a better handle on GridBagLayout the next time I need to do GUI - but until then, there are probably more important skills for me to develop. Will be interested to hear what others have to say on this subject though...
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no GUI that has to resort to null layout.
 
paul wheaton
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would agree that there is no case where you *have* to use null layout managers. I think one could also say that there is no case where you have to use a "proper" layout manager.
I would argue that there are many cases where using a null layout manager is wiser than using a "proper" layout manager.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only case you "might" be able to argue for it is small dialogs. Even then it can be done just as easily with layout managers.
 
paul wheaton
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dunno, I think I could make a good argument for damn near everything except for occassionally using a default layout manager.
I have yet to see an argument to not use null except "I don't use the null layout manager and I am good. Therefore using the null layout manager is bad."
I've used both and I think null is almost always better.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Wheaton:
I've used both and I think null is almost always better.

The purpose of the layout managers to guarantee WORA even across Java version upgrades. How can you guarantee that using null?
 
Simon Brown
sharp shooter, and author
Posts: 1913
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly ... you can't since you can't guarantee the size of fonts, decoration, etc.
Well said Paul (S)!
 
paul wheaton
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everything I work with that has a font uses a special lightweight component I created that works with the null layout manager. Either labels or else writing something on an image.
The problems I wrestle with directly are the same problems you will experience with "proper" layout managers. As you pop over to another o/s or the version of java changes, the fonts might change a bit and your stuff looks a little different. If you really squeezed stuff into a tight space and your fonts suddenly get too big, you have a mess. I have a mess too. If the fonts get a little smaller, we're both okay.
I think that with the null layout manager I use less code and I have more control.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With the null layout you have to prevent the user from resizing or write your own code. You also have to set the position of each and every component. Can't see how that saves time or code. Because I like it and it works for me isn't an argument for its use. Come to the light Paul. Come to the light.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Wheaton:
I think that with the null layout manager I use less code and I have more control.



Nope... you have to manually do a setBounds() on every Component you add to the GUI. And, what if the user wants to resize your GUI? You'll have to make a ComponentListener listen for resizes and resize everything accordingly.

Null layout is bad because it forces you to hard code values and connections into your GUI and is bad for reusability. If you're going to add or remove a component, you have to remember all the places you affected it in your code, and the more stuff you hard code onto the widget, the more places something can be forgotten.

You also mentioned that if you were tight on space a layout manager would have the same problems as null layout... wrong! Most layout managers work off the preferred size of their objects, so if the font changes, the size of the component changes. Plus, you should use pack() instead of setSize() on your frame so it will resize accordingly, too, and not let you get tight on space...

I too have been tempted by the dark side... at one time I thought that null layout led to more power over the GUI... as I learned of the evil that null layout possessed, I thought it might be permissable to use it in only limited circumstances, such as development only code or applets, since they don't have the need to resize... However, the taint of the evil null layout will rear it's ugly head and be reused from test into production code, or some web developer somewhere will use percentages in applet size instead of actual numerical values... Remember... DO NOT GIVE IN TO THE TEMPTATION!!!

-Nate
 
paul wheaton
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

With the null layout you have to prevent the user from resizing or write your own code. QB]


And you have to do the same with the layout managers. Only have to put things within things, within things, within things ... Or have a mountain of GridBagRestraints objects to define what to do on those resize events.
It seems that most of the stuff I have made has been with a pile of my own lightweight custom components. These do not allow resizing. Once in a long while do I need something that needs resizing, and then I work out the positioning through my own calculations - I still think it is easier than using a dozen simultaneous layout managers.

you have to manually do a setBounds() on every Component


Nope. With my custom stuff, most of it already knows its wide and high. My labels for instance: based on the font and the string, they determine their own width and height. I pass the x and y into their constructor.

Null layout is bad because it forces you to hard code values and connections into your GUI and is bad for reusability


You can hard code if you want. When I have a screen full of components, I usually have a x1, x2, x3 ... at the top as constants. I'll still toss some containers in once in a while to keep groups of things relative to each other.
I think the opposite is true of this statement since your choice of layout manager for a particular panel is "hard coded". If you need to add one more thing to a BorderLayout panel, you will probably need to introduce a new panel with another layout manager. I can just pop it in with an x and y.

If you're going to add or remove a component, you have to remember all the places you affected it in your code, and the more stuff you hard code onto the widget, the more places something can be forgotten.


Whatever wickedness you are thinking of here is just as true by "hard coding" your layout managers as it is with using the null layout manager. The innards of any one panel are kept in one place and it does not interfer with the innard of a component or container within it except to maybe alter its bounds.

You also mentioned that if you were tight on space a layout manager would have the same problems as null layout... wrong! Most layout managers work off the preferred size of their objects, so if the font changes, the size of the component changes


Not wrong.
You are suggesting that I can cure all of my recent woes by writing my own layout manager. Which would simply shuffle within the bounds that I use with a null layout manager. Or, that I use an existing layout manager which already does this shuffling. But the data they run with for font related stuff is the same data I would gather. If the font data is errant, their components are going to encounter cosmetic flaws just as mine would.
As is, now that I'm getting better data from the fonts, everything is working great in the tight spaces.

I too have been tempted by the dark side...


If this is the dark side, then the "bright side" must be lit by the flames of hell.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a good tutorial on LayoutManager's on the Java Developer's Connection. It has such interesting phrases as...


"To describe why layout managers are necessary, all you need to do is examine a few of the problems they solve. Look at the following screen shots. These describe several GUI sins that are all too common"



and


"A layout manager encapsulates an algorithm for positioning and sizing of GUI components. Rather than building the layout algorithms into your code and watching for window resizing and other layout-modifying events, the algorithm is kept separate. This allows a layout algorithm to be reused for several applications, while simplifying your application code."



Also, there has been quite a few problems using true type fonts documented on the BlackDown site... looks like Java likes true type fonts and Linux doesn't... maybe this problem is an artifact of that...

-Nate
[ March 06, 2002: Message edited by: Nathan Pruett ]
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
I have never had the perceived problems you say. GridBagLayout does not need a mountain of anything, if you know how to use it. Adding components are very easy if you know how to use Layout Managers. I have done both and will tell you Layout Managers are far superior to null layout.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Paul Stevens. Once you know how to properly use layouts they are a breeze to use. I do end up putting layouts inside of layouts but so what. The layout managers do all the work.
 
paul wheaton
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've had problems with "proper" layout managers: putting stuff where I don't want it requiring me to invest hours in fine tuning things so it will look okay. Then on other systems it comes out a different flavor of goofy, so I have to fine tune things to look good on all platforms. I never have these problems with null layout.
As for the sample from the JDC: it seems clear to me that made a panel that was a real screwed up mess. And making the window bigger didn't make things better. That's a straw man fallacy. Resorting to fallacy shows that your argument is desperately weak.
Suppose the developer used null to make a bigger pop up to begin with. Then all would be well.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To each his own I guess. But for all of you who aren't set in your ways, layout managers are the way to go.
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Y'all are minimalist gooey guys aren't you?
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic