We need to take some things into consideration:
- Originally the Java language was introduced to the world without a
IDE
- The Java virtual machine is cross-platform, programs must
run the same on Windows, Macos and Linux (initially Sun gave
support for Windows and Sunos, and the first JVM to exist for Linux
was open source, then Sun itself started to support in environment
Linux)
Without an IDE that helps to position the components (as happens in VB and Delphi that are the main tools to popularize this (we also have Visualfoxpro, Powerbuilder, and etc)) it is very difficult to use an absolute positioning.
In addition, the positioning of components depends on the native Toolkit of each operating system to position the graphical controls.
Thus, it is much easier (or less difficult) to create graphical interfaces by defining relative layouts using the behaviors of each available layout type. Let’s say where the controls should appear, but if the screen is resized for example, Toolkit repositions everyone.
But we have two problems with this, one of them is having a very large amount of objects being created to abstract this positioning (like using a Flowlayout inside a Panel that is in the center of a Borderlayout). The other is to learn what kind of layout object presents the positioning we want.
However, Java Ides started to support interface design in the same way as VB and Delphi tools, especially Forte for Java, which was based on Netbeans IDE 3.0 already allowed editing very good interfaces. At the same time there was an optional plugin for Eclipse that allowed creating forms. The Netbeans IDE has gradually gotten stronger in support of this type of activity, including supporting absolute positioning of graphical controls.
The (maybe) most important link is the Sun tutorial (which is now on the Oracle page) about using them which can be accessed here. This tutorial covers the AWT Layout classes (original Java Toolkit) and which are used in Swing (Toolkit which was released in Java 2), while for Javafx there are panels with built-in layouts, as can be seen here. The idea is similar in the idea, but in practice decreases the amount of code (but increases the size of the download because Javafx is much larger).
So far it has been context.
But after all, what are these layout managers?
These are classes whose objects can be assigned to panels and which describe how the components added to the panel will be displayed on the screen. Each Layout has a feature being that the most flexible and powerful (and that has more details to configure) is the Gridbaglayout.
A short list of Layouts:
- Flowlayout - If you do not specify any Layout, your Swing panel takes over that. It creates a "flow" of components, that is, putting them on each other’s side and skipping lines. Note that this is different from explicitly forcing
setLayout(null)
in a Component like Jpanel or Jframe, which is the way to indicate that we will not use layouts.
- Borderlayout - creates a view of areas center, south, north, east and west where you can place components.
- Miglayout - this layout is not original from the Java library, it is an open source and free project that allows describing the layout as the components are being added.
What is its importance in interface construction?
The importance of this is to facilitate the interface to reorganize according to the user’s actions. Almost as an ancestor of today’s responsiveness vision, but of course a Java GUI was not the same as across platforms, although a Javase application can connect to a Javaee application.
Anyway, as explained above, if you use a screen building tool, we don’t even see the layouts directly. There is even a recommendation in the tutorial itself about using a tool like Netbeans for this (see the note at the beginning of the tutorial).
Why should I use them instead of positioning everything on its own
using setBounds()
Now, about should use Layout Managers It’s a little more delicate than that. Because in practice the importance is on the screen readjust more or less alone to react to user interactions to increase or decrease the window. The method setBounds
acts directly on the component in which it was called to define its location and size, provided that the layout of the component is null, ie, the screen is fixed independent of the user’s actions, in practice if the window size increased the components would remain stationary in their defined positioning.
Good answer, but did not answer question 1, did not exemplify and did not cite references as noted in the yellow box.
– user28595
Your last statement is a little contradictory. The use of setbounds does just the opposite of what you say (which is to readjust yourself, to be responsive), so it’s not a less important issue. If we expect something responsive and adaptable with setbounds we will not get it, the IDE’s own build tool makes use of a manager.
– user28595
I think the penultimate paragraph is irrelevant to the issue here, which was about using screen managers, and not about whether web is better than desktop.
– user28595
@Articuno ok, I left only the part that talks about layouts vs setBounds
– prmottajr
GridBagLayout
!? Check this out: https://www.youtube.com/watch?v=UuLaxbFKAcc - CC @Articuno– Victor Stafusa
@Articuno yes Gridbaglayout, I said he is the most flexible and powerful, and is, not the easiest to use. Oracle’s own recommendation (It was already Sun’s too) is to use it to have total control. That doesn’t mean it’s easy. Utmost ease is achieved using an IDE.
– prmottajr
I believe that this reply was directed to @Victorstafusa and not to me.
– user28595
@Articuno yes, sorry :-) as I was correcting the text things by your comments I ended up going on automatic.
– prmottajr
Guys, I’m sorry to intrude, but this issue I think will be important for the site. @prmottajr after you finish your question, if you can, quote also at the end a little bit of Miglayout, will be a great reference.
– wryel
@wryel my initial goal with the question was just this, that it was shown, if in summary, a little about the managers. But nothing prevents another person from responding in addition to this answer :) I’m thinking of elaborating another answer complementing this but citing the managers.
– user28595
@Articuno {no confusion this time! : -) } I could include the layouts but besides not being clear this in the question, it includes the link to the visual guide and the official tutorial. I think that would duplicate information that’s available. In my view what we deal with in this question are the considerations of use or not, when and where to use. This does not have in the tutorial ;)
– prmottajr
What do you mean, it’s not clear in the question? Question 1 asks what they are, underlining that the most common ones could be exemplified, even in summary form. The guide is in English, not everyone who attends the site(including me) speak English. I learned to use the managers by practice and test, unfortunately not thanks to this guide. :/
– user28595
@Articuno yes :) I explained what they are, I did not imagine that a list of them was expected. I can include this without problem, but only later :( About English is true, I did not care about it. In this case it would not be duplicate information, can not translate the whole tutorial, but can put more information here too (what the person does not to fight for 50 points right?) Is there anything else you’d like to enter?
– prmottajr
A list would not, because Voce would be right, would become expensive and would leave the scope even. But Voce has already cited a layout (the gridbag), other common used by the ides are the borderlayout and the miglayuot. Other layouts I agree will each learn, but these two are used in IDE as well, including netbeans has borderlayout and gridbaglayout as options in that graphic interface creator. Quote them quickly (not even need to dedicate a big explanation not) as you did with the grid already think it would be enough :D
– user28595
@Updated article!
– prmottajr