16

Recently, I started creating a program for the company I work for. Just as background info, I'm still a student and a beginner programmer, so my solution is probably not recommended and I didn't know how to do it otherwise, but it works and I'm not going to be judged for it because it's a student job totally unrelated to programming.

The thing about the program is, it's going to be run on multiple different computer with different screen sizes and resolutions (800x600 and up). To make sure it takes as much of the screen as possible without losing any part of the program, I set the layout to null and hard-coded everything using relative values.

The program is kiosk-style and I first get the screen size values and go from there (for example, off the top of my head, the left-side menu takes an eighth of the screen, the top bar 2%, etc.). I also use font metrics to make sure the components are sized correctly and that everything gets displayed nicely.

My question is: why is it so frowned upon to make the layout null instead of using the layout managers? (I was told on some forums that this is a horrible way of doing things) I know how the layout manager works and know how to use the different layouts, but for the requirements of this program (multiple different resolutions, custom button shapes and placements, text changing on the components when you change language, etc.), I couldn't see myself using the layout managers to do it all.

How do you more experienced programmers use the layout managers in a situation like this? And what do you do when you want a button to be somewhere specific and other components somewhere else specific that don't really match any of the predefined layouts?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jumbala
  • 4,764
  • 9
  • 45
  • 65

4 Answers4

16

If you layer the layout managers correctly the screen will re-flow to different sizes for you, the idea is to use a single set of layout managers on ALL screen sizes.

If you use null you will have to do each screen size yourself. Not only that but if the app can be windowed you have to support every possible size they might scroll to.

That's kind of difficult to do, but the layout mangers are designed to do just that.

There are some common tricks. BorderLayout is a great layout to start with. Sometimes you might use it at multiple levels--often with just 2 or 3 components in it. That's because it's really good at giving all but one area the minimum required area and giving everything else to the CENTER.

FlowLayout can be useful but it's tricky if your components are different sizes.

I wouldn't try GridBagLayout unless you are planning to write code to feed your layout manager (an excellent solution at that!).

I also wouldn't use GUI builders, they don't know the overall way you want to reflow your layout.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Bill K
  • 62,186
  • 18
  • 105
  • 157
  • I figured that, but this particular application cannot be resized, so that is not a problem. What I had the most trouble with was that 1. I didn't find a way to have my buttons positionned exactly where I wanted them to be and 2. (I'm probably missing something here) I couldn't have my buttons the right size to fully display the whole text on every resolution ("longTextOnButton" would become "longTex..." on smaller resolutions) – Jumbala Jul 06 '11 at 06:45
  • 1
    You should be able to set minimum sizes for your components, and positioning exactly can be tricky and might require a couple different layouts in different ways. Also, one thing you'll learn pretty soon--when something is absolutely positively a given requirement (such as always full-screen) it'll probably change. Still, build it how you feel most comfortable and keep evaluating the different approaches learning from your mistakes and successes because that's how you build useful knowledge across your entire career, school is just the start. – Bill K Jul 06 '11 at 06:53
  • Yeah I understand that. For this particular project, I'm pretty sure it's not going to change since I'm the one that suggested doing it since we're not really busy at work come summer time, so I'm making my own requirements by thinking at what would be the most useful way of using it, but I see where you're coming from, I just didn't think about that beforehand. As for learning for my entire career, this is the main reason why I'm on this site, so I can improve by learning from what people have to say and to help others that are trying to learn as well. Thanks! – Jumbala Jul 06 '11 at 07:03
  • 1
    Made some minor edits to this great answer. Please review them. – Andrew Thompson Aug 13 '12 at 23:49
8

In a nutshell: because all the work that you explain above is done (or at least: should be done) by the layout manager.

More often than not, when a null layout is used, it also implies that all positions and sizes are hardcoded to a single value, so no flexibility at all is given. This means that changes in window size, language, font size, display density or any other related parameter have no effect on the layout and you get the usual ugly effects: empty parts of the window; tiny, unresizable lists; buttons with their labels cut off; ...

It sounds like the work you do should really be done by the Layout Manager. Either find one that does that (my personal suggestion would be MiGLayout, which does a lot and is easy to use) or write your own.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • I actually didn't know about other Layout Managers (this is kind of stupid, but I never thought about it). I only knew about the ones in JDeveloper like CardLayout, BoxLayout, GridLayout, GridBagLayout, FlowLayout, etc. I'll take a look at other ones like in the link you suggested. Thanks! That should make things way easier for me in the other windows I haven't created yet / for future applications. – Jumbala Jul 06 '11 at 06:54
  • @Adam: yes, unfortunately the built-in layout managers are limited. They are either too simple to be useful for complex layouts (most of them) or too complicated to be used by a sane developer (GridBagLayout). I don't often do UI work, but I **hated** doing any layout until I found `MiGLayout`, which brought the task from "terrible" down to an acceptable "mildly annoying" ;-) – Joachim Sauer Jul 06 '11 at 06:56
  • I'll take your advice and test MiGLayout out! I hate doing UI work as well since I'm not too artistic and I find it very tedious. Thanks again! – Jumbala Jul 06 '11 at 06:59
4

You are practically using a layout - your own, with all your sophisticated calculations of positions.

You can move these logic to a custom layout manager class to pacify the critics.

irreputable
  • 44,725
  • 9
  • 65
  • 93
  • I didn't think of it that way. I might do that if I don't end up switching to a layout I like better. That way I might be able to use it later in future projects. – Jumbala Jul 06 '11 at 07:00
  • The problem might be that you are thinking of it as "a layout", You never use just one. A reasonable screen will have a bunch of them interacting with different panels and components. One size does not fit all. – Bill K Jul 06 '11 at 11:28
4

hmmm trick should be by mixing LayoutMangers and by usage of numbers of nested JPanels that each could have diferrent Layout or not, really depends of number of JComponents, that allows you to create GUI that looks like as layed by using AbsoluteLayout but with same look/output to the GUI for every screen resolutions and ratio (4:3, 16:9, 16:10)

mKorbel
  • 109,525
  • 20
  • 134
  • 319