Adding some JPanels (one below another) in JPanel - java

I'd like to add some panels (with different height) to another JPanel (which is in a JScrollPane) as in this image:
Which layout should I use?

Looks like a good fit for BoxLayout. See also A Visual Guide to Layout Managers for a quick 'birds eye view' of the layouts available in the J2SE.

Possibly the easiest is to use flowlayout and set the preferred height of the JPanels accordingly http://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html
If you plan to do more with your layout I'd recommend one of the more advanced third party layouts like JGoodies Form Layout http://www.jgoodies.com/freeware/forms/ You'll need to learn more, but than you have a good flexible layout which will solve most of the real world problems you'll see.

Related

JPanel and Java layout managers

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.

Can SpringLayout do all the job

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

Adding more than one JPanel in a single JFrame

I have created a JFrame - now I want to add the 4 JPanel in that frame at a particular location. How can set the location of panels in the frame?
Use (possibly nested1) layouts for the logic. See Laying Out Components Within a Container for details. They can:
Include default spacing in the constructor (often)
Calculate how big the GUI needs to be in order to display the components (in whatever PLAF, on whatever system the app. is deployed).
Extra spacing can be organized by adding an EmtpyBorder to child components.
See the nested layout example
Placing components in a container is quite a complicated subject in Swing. Instead of defining the exact places for your components, you would normally use a layout manager that arranges them in a certain way.
Here is the tutorial you should read to get a (visual) clue about the different layout managers: A Visual Guide to Layout Managers
However, the standard layout managers of Swing can be cumbersome for more complex layouts. Either, you could use nested layouts to get the desired result, or you could use a very powerful third-party library: JGoodies Forms. The downside is of course that you have to learn yet another library. Therefore, I would only recommend it for a bigger project.
For me it is good way to set GridbagLayout for the container of the frame. There are several visual swing GUI editors available to do this easily. You can use NetBeans GUI editor or GWT Designer (https://developers.google.com/web-toolkit/tools/gwtdesigner/) for complex GUI designing tasks
If its 4 locations, you can use BorderLayout,by default its the CENTRE, but it also have EAST, WEST , NORTH, SOUTH locations for the placement of the components. You can also use setLocation to put the panels in the appropriate locations, if a layout isn't used.
Its even better to use GroupLayout developed my NetBeans team in 2005, use Windows Builder Pro, now provided by google for free.
set the layout of the Frame to be null via setLayout(null)
create 4 JPanel and set their location using setLocation method
add these panels using JFrame's add method

Java LayoutManager: Looking for a LayoutManager similar to GTK's horizontal and vertical boxes

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.

how to use the layout managers in swing java

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.

Categories