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.
Related
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...
Not sure if what I need is possible.
I have a container (JPanel) that contains some internal elements.
I was wondering if it is possible to force internal elements to fit into the container's size.
I need them to be fully visible i.e., resize to fit inside the Panel's size and not cut some parts of the internal elements.
Scrolling is not an option.
Is this possible by using a Layout or something?
EDIT: Important clarification:
The thing is that I do not have access to the internal elements neither to their properties so I would say that a Layoutmanager capable of resizing child elements to fit to its size is needed. I tested BorderLayout and GridBagLayout but the result is always the same, the internal elements are cut out.
It's for exactly that reason that LayoutManagers exist. All the LayoutManagers work for simple containers directly, excluding GridBagLayout which is to able to handle most complete GUIs directly.
For most complete GUI's you have some choices as follows:
Look for a 3rd party layout such as MigLayout or here
Use GridBagLayout
Very easy way is use nested layout, where there is more than one JPanel and each has child JPanels with the same or different LayoutManager
Or custom layout, should be hard..., but same as using GridBagLayout
You could set the JPanel layout to border layout, then add the single child to the center. If there are multiple children, this approach becomes less useful since components added to the the NORTH, SOUTH, EAST, and WEST will remain statically sized while the centre resizes to fill the remainder.
In short, this isn't an ideal solution. All layouting in Swing is made all the more complex by the fact that different components behave in different ways, so you really need to provide further details of the child components you wish to add to your panel, and any behaviour that has been overridden on those components.
The best way is to try a couple of simple examples to see what mileage you get and whether subtle redesign of your child component nesting could help.
you can use a layout, like GridBagLayout, or BorderLayout depending on the situation. With proper weights it is possible.
this sounds to me like you should just peek an appropriate layout manager and use it. For example, look at BorderLayout - put your component in the CENTER and it will occupy all the area. Its up to each concrete layout manager to decide what will be the size of the components.
Mark
I was using a JInternalFrame inside JDesktopPane. I wanted the internal_frame to auto resize as desktop pane is resized, so I had to implement the AncestorResized event for the internal frame where I placed the following code:
this.setPreferredSize(this.getParent().getPreferredSize());
this.pack();
On my Java Swing application I have two components. On the left side is a navigation (JList) and on the right side is a JTable. I would like to leave the possibility to increase the size of the window, without increasing the size of both components.
The proportion of 50/50 is kept, through ought the whole sizing. I use GridLayout. Is this behavior rooted into the LayoutManager or is a property which has to be set?
GridbagLayout will allow you to achieve this. However, have you also considered using a JSplitPane where the left-hand side contains your navigation panel and the right hand side contains the table? You could configure it so that all additional space is allocated to the right hand side by calling setResizeWeight(0.0). However, you still retain the flexibility of allowing the user to manually resize the navigation area if required. You also have the option to hide your navigation panel completely by calling setOneTouchExpandable(true) on the split pane.
From what I know the GridLayout manager resizes all cells to the same size. Knowing this you might use it anyway just add the component you want to stay unchanged to a panel and then add this panel to a cell instead.
Or use a different layout manager mine favourite is TableLayout, where you can set which columns/rows should fill the empty space where the rest will stay in their preferred size.
Good luck, Boro
Suggestion: Don't us GridLayout. Instead use other layouts such Borderlayout or GridBagLayout or a combination of layouts. For instance if you used BorderLayout, you could but the JTable BorderLayout.CENTER and the JList in one of the other positions. Or if you use GridBagLayout, then by setting your GridBagConstraint weightx and weighty values correctly and the fill values (only you know what you currently desire), would allow selective enlargement of the components added to the container.
I'd like to make a login bar for an application and I can't figure out how to organize a series of JLabels and JTextFields such that they are organized in a horizontal grid without these same components being resized to fit each cell. I also want to make sure that the group of components isn't resized below a certain width. How can this be achieved?
Edit: Thanks for the answers everyone. I'll have a look at MigLayout and SpringLayout later. Due to time constraints I'm going to have to make do with Visual Editor and use a null layout. The component placement and dimensions have to be adjusted by hand but at least they stay put. Here's a picture showing what I wanted to do.
bar http://img145.imageshack.us/img145/7356/bargw.png
Use MigLayout as your layout manager, it's extremely flexible, and supports what you're asking quite easily. You can set size constraints. If you need any further help, post some example code using Swing and MigLayout which shows what you're trying to do, and then I'll advise you on how to do what you want to achieve.
You probably want some additional cells which 'grow' to fill the remaining space. This can be achieved with column constraints, by inserting 'push' between the columns (specified by [..]) to expand the gap. You don't need any placeholder components in this case. (i.e., [pref!]10px[40px::]push[pref!]10px[40px::])
You have to use different layout. FlowLayout or BoxLayout will work in your case, but I would suggest MigLayout simply because it will cover all your needs and replace all others .
Check out the section from the Swing tutorial on Using Layout Managers.
The SpringLayout has an example that does exaclty this.
The GridBagLayout is more difficult to use but also support row/column type layout.
Finally, you can still use a GridLayout. Just add the text fields to a JPanel first, then the panel will grow but the text field won't.
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)