I would like to ask if SpringLayout can do anything like absolute position because I think absolute position have problem when I maximize the frame and what do I have to use if I need to set JMenu and JToolBar and JTextField and JTable all in one line in order?
I tried to use Borderlayout but it give me very big JTextField. I tried Gridlayout it give also big JTextField I need it big but not as big as it shows up.
can SpringLayout do all the job
No. It is neither designed, nor intended to do 'all the job'.
Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.
For absolute positioning, don't use a layout manager at all.
However, that's not really recommended. Usually you should use a layout that would scale.
See also this thread for more discussion.
i found note writing by java says
Note: This lesson covers writing layout code by hand, which can be
challenging. If you are not interested in learning all the details of
layout management, you might prefer to use the GroupLayout layout
manager combined with a builder tool to lay out your GUI. One such
builder tool is the NetBeans IDE. Otherwise, if you want to code by
hand and do not want to use GroupLayout, then GridBagLayout is
recommended as the next most flexible and powerful layout manager
Related
Hi :) I'm making my first GUI with Java swing, and I have a problem: when I change window dimension, "internal components" don't change their size.
I use absolute layout, it may be an important thing to know.
Absolute layout means that you are using absolutely defined constraints about position size e.t.c. To create resizable GUI. you need to use more "flexible" layouts.
One simple option to start is BorderLayout. Hovewer you have to leverage use of panels in this case.
Documentation
Another possibility may be the Grid bag layout but that often involves more work to do.
You can solve this problem by using the MigLayout in combination with the WindowsBuilder. This is a WYSIWIG editor for SWT and Swing Layouts. But BorderLayout and GridBag Layout can srink and grow as well.
Having read over the internet about JPanel for a while I feel I still need clarification after reading it. My questions:
What is JPanel mostly used for?
Which layout manager is most flexible to use?
How do you position components at certain areas using layout managers?
I find it really difficult to position components at certain areas within the container using a layout manager.
What layout manager do you recommend I use or do you have to use a mixture of different ones?
My first question is what is JPanel mostly used for.
You already know that. You add components to the panel.
My second question is which layout manager is most flexible to use
The more flexible that layout manager, the more complicated the layout manager is to use. So the trick is design your GUI logically and then use multiple panels with different layout manager to achieve your desire effect.
Each layout manager has its own strengths and weaknesses.
I find it really difficult to position components at certain areas within the container using a layout manager.
That is old thinking. You should not be trying to position components specifically. You position components generally and let the layout manager adjust as the size of the frame is changed by the user.
Maybe you want components centered, or in a grid. There are different layout managers to achieve your task. We can't give specific advice. If you have a specific problem then as a specific question and post your code that shows what you have tried.
But first, read the Swing tutorial on Layout Manager and download the example code play with the code to understand how each works. You learn by trying.
What layout manager do you recommend I use or do you have to use a mixture of different ones.
It depends on the GUI. I'd typically use a combination of layout managers for anything but the most trivial user interfaces. On the other hand, some like to try and use a more complex layout for those same GUIs, using layouts like:
GridBagLayout - older, but quite versatile.
GroupLayout - newer, more powerful, and provides better ability to align elements that are not next to each other, but considered so difficult to hand write that most use a GUI designer that will produce code that will not be portable to the next GUI designer or IDE.
A third party layout manager like MigLayout or FormLayout
JPanel can be used:
As a container for other components
Base for a custom widget
Drawing area
MigLayout is hands down the most flexible layout manager.
There are many layout managers in Swing, but in my opinion
only three are up to the job:
MigLayout
GroupLayout
FormLayout
Since MigLayout is an (greatly) improved FormLayout, I usually
recommend the first two managers.
Using a mixture of various managers is a poor practice. It is a way
of overcoming the simplicity of basic layout managers. One should choose a good layout manager and not to tangle with multiple layout managers. Most layouts can be easily done with MigLayout and GroupLayout.
I am trying to create a GUI for a program with an undefined number of rows but stay with two columns. Currently the only way I can get it to look how I want is using FlowLayout but the window of course needs to be smaller than desired. It needs to happen within one panel as well because it is being added to a tabbed pane.
What would be the best layout manager to solve the problem that is in the JDK? Or would just kind of brute forcing it with AbsoluteLayout be the best approach (as the user shouldn't really be resizing the window)?
I have attached the desired appearance.
Thanks for any help in advance.
Several layouts can do what you want. I'd suggest GridLayout (easy to use, but columns will be equal width) or GridBagLayout (harder to use, but you have lots of control). You could also use a BorderLayout and put all the fields in a sub-panel on the WEST and all the drop-downs in a sub-panel on the EAST. The difficulty with that is ensuring that the rows have the same height, since they won't be constrained by the layout itself.
The best thing to do would be to go through the Java tutorial on layouts and get up to speed on what the various layout managers can do.
Also, since you're using Swing, you could just use a JTable (as Gilbert Le Blanc suggests in his comment).
Okay, I spoiled myself by playing around with GTK layouts, and now I've started thinking about windows layouts in GTK's terms and can't find an appropriate LayoutManager to use.
I'm specifically looking for a LayoutManager written in Java, for use in a Swing-based application that acts almost exactly like GTK's Horizontal and Vertical boxes.
In particular, I want the child widgets fixed to the off-axis size of the container, and also the option to have certain components take up the extra space (the "expand" option in GTK) while the other widgets only take up their minimum on-axis size.
Is there a layout manager that will do this?
In Java, the most configurable layout manager is the GridBagLayout manager. It allows you to specify the position in a grid using gridx and gridy and how extra space is allocated along that axis using weightx and weighty.
GridBagLayout is quite fiddly, so I would suggest reading a tutorial before using, but I have never found a layout I couldnt build with GridBagLayout.
You should use nested layouts. If you understand the grid box flow border layouts in java which is easy then you can nest them to make anything you want. i would give you some java code to do it if i understood how you want to layout your components. By the way you may want to try mig layout http://www.miglayout.com/ . I don't use it my self because i all ready know how to use swing layouts but its a nice library.
You can use BoxLayout - Oracle tutorial - and javax.swing.box for a component with BoxLayout.
It can manage Vertical and Horizontal layouts.
While I've not used GTK myself the way you describe it sounds like the requirements to which I implemented MSBLayout:
http://msblayout.sourceforge.net/javadoc/de/winterdrache/layout/MSBLayout.html
It combines the properties of BoxLayout and GridBagLayout that other posters have already mentioned, but it is much easier to use.
I'm new to layout managers like Flow, borders, ...
I mostly use setBounds() to set the position of my components.
I read on an article that using setBounds is not an good practice and it's better to use some layout.
Which are the best and most used layouts?
How to position a button using layout managers; instead of doing setbounds(10,10,100,30)?
look here: http://download.oracle.com/javase/tutorial/uiswing/layout/using.html
Basically you should forget about coordinates. Look at your dialogs at a higher level of design. Questions you should ask yourself.
1) Is there a "main" area with smaller surrounding areas in your design. If so use a BorderLayout.
2) Is there equal grid like areas in your design, If so use a GridLayout.
3) If you need a top-down, or left-right layout, consider a BoxLayout
4) If you want to show a complex form, probably use a FormLayout from jgoodies.
But you have to look at things from a high level. There may be subsections in any one top level section of your dialog. If that is the case, then you need to put a JPanel in that section, and then use a sub layout in that JPanel. Use the above questions over again for that panel.
Besides "standard" Swing layouts (part of the JDK), there are many third-party (mostly open source) LayoutManagers that often are much better than Swing ones.
For a comparison of many LayoutManagers on a real example (with code), check out this link, although a bit old, it still shows the various features and ease of use of predominant LayoutManagers nowadays.
In general, I would advise DesignGridLayout which, although quite powerful, is very easy to use (you don't need a GUI designer to use and it's easy to maintain layout code of existing panels); it just takes one hour to understand it.
Also, MigLayout is viewed as the most flexible one (might be useful if you need very complex layouts), but it takes more time to get used to it, and sometimes you have to use "tricks"to make it work as you want.
..which is the best and most used layout (?)
Nested layouts. Use whatever layout works best for different groups of GUI components, then put them in panels inside other panels (with other layouts). See this Nested Layout Example for a demo. of combining layouts.
As to which best individual layouts to use, do the tutorial linked by MBFG to get a feel for what each can achieve, their strengths & weaknesses.
I will commonly use nested combinations of BorderLayout, GridLayout & FlowLayout, with an occasional GridBagLayout.