Choosing the layout managers and number of panels for Java GUI - java

I would appreciate, If anyone here is kind enough to recommend me what are the layout managers to choose
and how to separate the attached GUI into Jpanels.
Thanks so much in advance.
What I have tried so far:
Jframe - BorderLayout
Map (the grid and clear button from another class extending jpanel) - so I have put it in jframe.CENTER
the buttons to the right: jframe.EAST
I put them in a jpanel in a gridlayout (but I cannot get the spacing between the components)
buttons at the bottom: jframe.SOUTH
I put them in a jpanel in a gridlayout (but I cannot get the spacing between the components)

When trying to determine what layout(s) you should use, you should start by trying to determine areas of responsibility...
For example...
Based on your needs, I might start with a GridBagLayout. This might seem complex, but if you break the UI down into seperate components, focusing on their individual needs, it should become simpler...
For the panel on the left...
I would be temptered to use a GridBagLayout, simply because it allows the components to use there preferred sizes, but still allows you to set up a grid like pattern...
For the arrow buttons...
This becomes a little more complicated, but I would use a GridLayout(2, 3) (2 rows, 3 columns). This will require to add a filler panel at the first and third position along the top row, but still maintain the buttons at a equal size...
For this panel...
I would be tempted to either use a GridBagLayout, because it will allow you to span the rows or even split it again into two separate panels, with the controls on the left in a GridLayout(2, 1) and the control on the right in something like a BorderLayout as required...
For "progress" panel...
I would be tempted to use...GridBagLayout. Mostly because it would allow you to provide more weight to the progress bars then the labels.
For the main panel...
I would probably be tempted to either use a BorderLayout, with the Clear Map on another panel of it's own, allowing it maintain it's preferred size, in the NORTH position and the map panel in the CENTER or even a GridBagLayout depending on what the invidual components are...

Related

Java GUI place layout under boxlayout

I have used borderlayout to specify where the content of my java GUI shall be placed, I have then chosen to place it on EAST and then made two boxlayouts to show two columns of buttons. I now have to place something underneath it and not beside it. How would you suggest or advice me to do so, using any layout but preferably boxlayout and not absolute layout(null). Thanks in advance.
Image:
The arrow points to the place I want another JPanel to be.
You could...
Wrap both of the button panels in a JPanel
Whatever component goes at the arrow, wrap in a JPanel with GrigbagLayout (just to center it).
Create another JPanel with BorderLayout that will hold the above panels. Use CENTER and SOUTH.
Give an EmptyBorder to the SOUTH panel, only specifying the top region and space it accordingly.
Really there are many ways to accomplish this. The key though is to nest JPanels and make use of the different layout managers with each, use EmptyBorders or stuts for empty spaces til you get your desired effect. The possibilities are endless. I don't think there's one right answer. Since we don't have a runnable example, I say just try the above, and mix and match will you get what you want.

Resizing two ScrollPanes equally in Swing

I am designing a JFrame that contains two ScrollPanes ordered sequentially, one below the other. I am using the inbuilt form editor of NetBeans (Matisse?) which uses GroupLayout.
What I observe: When resizing the frame, first the ScrollPane at the bottom is resized until it vanishes then the buttons above this ScrollPane and finally the other Scrollpane.
What I would like: When resizing the Frame, both ScrollPanes should be resized equally, so if the frame changes size by X, I want both Scrollpanes to change their size by X/2.
Is this possible and how? (Solutions for GroupLayout preferred)
P.S.: The total Layout includes Buttons and Labels placed in a table like manner. So GroupLayout is already quite useful for it.
P.S.: It seems the best option beyond simple Layouts (Grid, Box) is MigLayout.
Let me cite from the white paper
Growing and Shrinking
What should happen when a component isn't given the preferred size is extremely customizable. There is the option to divide components into grow and shrink priority groups where a higher priority will shrink/grow before components/rows/columns with lower priorities are resized. Grow weight within those groups can be set to specify how the free space or shrinkage should be divided between the components/rows/columns in that priority group. ...
P.S.2: I am now using MigLayout and it is so powerful that I want to recommend it again. Btw. I find their cheat sheet very useful (all options explained). Also the demos on their website contain many example and the source code. The problem here and many others can be easily solved with MigLayout.
Is this possible and how?
by using GridLayout, GridBagLayout, BoxLayout (have to override all Min, Max and PreferredSize) and for MigLayout
Is this possible and how? (Solutions for GroupLayout preferred)
I woudln't be going this way, even could be possible
A grid layout ensures that the elements are evenly sized. Use:
JPanel panel = new JPanel(new GridLayout(2,1));
Since you want them vertically aligned, that is after each other, 2 denotes the number of rows, and 1 denotes the number of columns.
Then simply add the ScrollPanes to the panel:
panel.add(scrollPane1);
panel.add(scrollPane2);
For documentation on Grid Layout, look at this

Which Panel to use in Swing

I would like to have a JTable component in one panel to take up as much space as the window allows. Underneath, however, I would like to add another, fixed size panel with a few buttons in it. WHich layout to use to allow the bottom panel be of fixed size, while allowing JTable panel to stretch according to size of the window?
It's hard to visualize what you want exactly, but here's an excerpt from Oracle that may help you:
Scenario: You need to display a component in as much space as it can get.
If it is the only component in its container, use GridLayout or BorderLayout. Otherwise, BorderLayout or GridBagLayout might be a good
match.
If you use BorderLayout, you will need to put the space-hungry
component in the center. With GridBagLayout, you will need to set the
constraints for the component so that fill=GridBagConstraints.BOTH.
Another possibility is to use BoxLayout, making the space-hungry
component specify very large preferred and maximum sizes.
Scenario: You need to display a few components in a compact row at their natural size.
Consider using a JPanel to group the components and using either the JPanel's default FlowLayout manager or the BoxLayout manager.
SpringLayout is also good for this.
Scenario: You need to display a few components of the same size in rows and columns.
GridLayout is perfect for this.
Scenario: You need to display a few components in a row or column, possibly with varying amounts of space between them, custom alignment, or custom component sizes.
BoxLayout is perfect for this.
Scenario: You need to display aligned columns, as in a form-like interface where a column of labels is used to describe text fields in an adjacent column.
SpringLayout is a natural choice for this. The SpringUtilities class used by several Tutorial examples defines a makeCompactGrid
method that lets you easily align multiple rows and columns of
components.
Scenario: You have a complex layout with many components.
Consider either using a very flexible layout manager such as
GridBagLayout or SpringLayout, or grouping the components into one or
more JPanels to simplify layout. If you take the latter approach, each
JPanel might use a different layout manager.
Source: Oracle: Using Layout Managers
The simplest way to achieve it is to use BorderLayout. Put your table in the center. Then create yet another panel with FlowLayout and put it to the south of your main panel.

How to enlarge java swing panel

I create some panel with a lot of components as images text ... . Now I have one problem. Customers tell me that panel is too small. Is there some option to enlarge this whole panel or I have to enlarged components one by one ?
UPDATE:
from this:
to this:
everything depends of used LayoutManager,
Is there some option to enlarge this whole panel or I have to enlarged
components one by one
-> no, basically this is reason why LayoutManager exists there, most of them there are created especially for this reasons, GridLayout, GridBagLayout, BoxLayout (maybe without glue), BorderLayout and todays MigLayout are best way how to do it
it depends, if you have a GridLayout or some Layout witch set all the components with some order, for example, all the components at the same distance or all the components like a Matrix, if you enlarge the panel, all the components will enlarge with it. If not, you have to enlarge by hand.

Can a layout manager spawn several JPanels?

I have to build a rather large form with many controls. The controls are divided in basic controls/settings and extended controls/settings. The user can decide if he wants to see only the basic or both basic and extended controls.
I've dropped all extended controls onto their own JPanel so that I can easily switch between the two views by showing or hiding this panel.
Currently I'm using GroupLayout and what happens is that the controls on different panels are not aligned:
Label aaa: Text field
Label a: Text field
Label aaaaaa: Text field
----------------------------
Label b: Text field
Label bbb: Text field
Label bb: Text field
Unfortunatly I found now way to "synchronize" the layouts of the two panels (except using AbsoluteLayout and fixed control coordinates)
Is there any way to achive this?
Is my whole design flawed?
EDIT: If it is possible I would like to keep the GroupLayout manager.
As far as I know, no Swing LayoutManager (from JRE or open source) can span several panels.
I am currently working on such a feature (which I called "layouts synchronization") for my DesignGridLayout project, but it is not something easy to implements (I have started about 2 weeks ago and I still don't see exactly if and when I will get to something interesting, but I still have high hope for it;-))
One option you could check would be to add all components to the same panel (with just one GroupLayout then) and hide/show them based on user's selection. Hopefully, GroupLayout will adapt the size to the situation (after calling pack()).
If GroupLayout behaves well, then it would just be a matter of calling pack() each time after user changes his selection to show/hide extended fields.
Else you would have to manually set the size of your panel every time the user changes his selection.
Probably the easiest (good) way to do it is to add all the components to the main panel. Set the subpanels to non-opaque, and add the also to the main panel. The main panel the needs optimised drawing to be switched off.
Another technique is to add a spacer component. To the bottom panel add a component in the same column as the labels which dynamically takes the width component of its various size methods from the top labels. Do the same in reverse to the top panel.
I think there is no way to do it with the standard layout managers. You'll probably have to write your own layout manager, but it shouldn't be too hard if you subclass GroupLayout.
You could use GridLayout instead of GroupLayout which will give you uniform spacing between the columns
If you want to keep them in separate panels with separate layouts:
Iterate over all of the labels that you add, and find the maximum preferred width of each.
Iterate a second time, and set the preferred size to that each label's preferred height, but the maximum width.
This is the explanation of th GridLayout. This will set every component to the size, you expect it. With the GridData object you can specify how the components are ordere.
Examples
(source: sun.com)

Categories