GridBagLayout or BorderLayout for proposed project in Swing/AWT? - java

I'm attempting to create a GUI as pictured:
I'm having some trouble deciding which layout would be the best for this.
I've attempted to use a GridBagLayout, but can't figure out how to make items different sizes, and how to position them.
I've also considered using a BorderLayout and creating an eastpanel, westpanel and centerpanel these using the respective layout constraints to put them there, but this feels incorrect.
I've also read the how to use GridBagLayout Java tutorial, but still can't figure out how to achieve the goal.

You should not force yourself to use only a single panel with a single layout. Nest several panels inside each other with each panel possibly having different layouts.
You could have 1 root panel with a BorderLayout. For the top you have a panel with a FlowLayout (or maybe a gridbaglayout). And the center can be another panel with a GridBagLayout or perhaps a combination of more nested panels.
There is no one magic layout that can do everything. Composition is the key.

Related

Make children of JLayeredPane fill available space of container

I have two JPanel instances in a JLayeredPane, on different z-orders. I want both of my child JPanels to always fill the space of the LayeredPane.
The idea is for me to toggle the display of a 2nd panel over top of the first to display a modal-like dialog. Yes, I could just use a JDialog, but I thought it would be fun to try and create some transparancy overtop of the covered JPanel for a nice effect.
I find that using a layout manager on the JLayeredPane, like BorderLayout, and trying to set both children to CENTER conflicts since both panels can't be in the Center.
Is there a trick that I'm not seeing?
The idea is for me to toggle the display of a 2nd panel over top of the first
The easiest way to do this is to use a Glass Pane.
Check out the Disabled Glass Pane for an example of this approach.
There are two ways to create some "Glass Panel like" overlay for JPanels with JLayeredPane:
Add a ComponentListener to the JLayeredPane and update the sizes of all child components whenever the size of the JLayeredPane changes
Create a simple FillLayout, which expands the size of its child Components to the size of the Layout Container (In our case the JLayeredPane). You need to keep a list of children Components. During layoutContainer you copy the dimensions of the Container to these child Components. I wrote this and its really simple, but unfortunately I can't post it, since it's corporate. But if anyone is interested just ask in the comments. The implementation basically consists of one-liners.
For both solutions you need to make sure, that the panels on top are transparent, by setting setOpaque to false. This ensures that underlying panels render their content.

Auto scaling components with Java JFrame [duplicate]

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();

Java Swing Custom Layout Issue

I'm having trouble organizing the contents of a JPanel. I've tried different layout managers but haven't gotten anything to work.
The JPanel contains 3 other JPanels and should look like this:
Does anyone know how I can achieve this layout?
Thanks in advance.
What you want is similar to a BorderLayout, but using one natively will cause the second pane to fill the bottom space (rather than the 3rd pane.)
However, you could use two in tandem to get this effect - The first BorderLayout pane would contain the second BorderLayout pane (in its centre) and panel 3 (on its east.)
In the second BorderLayout pane, you could then just set pane 1 to be the centre, and pane 2 to be the south.
Simplest, but not best solution, is to use null Layout Manager with manually set components bounds. It is called Absolute Positioning
Other approach is to use horizontal SplitPane with nested Vertical SplitPane: left one will contain vertical splitPane with Panel1 and Panel2, and right will be just Panel3.

Dynamically resize JPanels to fit width of window

I've got an arbitrary number of JPanels being created based on user input (like 1-8 panels). Right now, I'm putting all the panels in a larger panel using FlowLayout, and then adding that panel to my main window which is using BoxLayout. I want the panels to fit the width of the main window but right now they are very small. Should I use a different layout or is there a way to do this?
You have to choose GridLayout or GridBagLayout.
Yes, you should use a different layout manager. Use BorderLayout (tutorial here), which streches all the contents to fit the container.
Also, BorderLayout doesn't allow more than one component in each area, thus you will need another sub-panel to hold your panels. Don't use FlowLayout for that sub-panel, as it will not have the streching behaviour your are looking for.

Resize a JPanel in line with a JDialog

I've got a JDialog which contains a series of JPanels in a CardLayout. Some of these panels contain JTables which I would like to be resized in line with any resizing of the JDialog. I am not sure how to achieve this and any help would be greatly appreciated.
At present the tables simply remain their current size and do not scale. I'm sure its something simple but I'm having trouble locating the exact approach needed.
Many thanks in advance. I will provide any more information if required.
edit: The JDialog is used as a wizard, so only one of the panels is being displayed at any one time, hence the use of CardLayout. I would ideally like to keep this is layout manager, although if it is the source of the problems then obviously I would rethink!
You can keep the CardLayout, but you need to do the following:
1. set the layout of your JDialog to BorderLayout, and add a new JPanel (contentPanel) to the JDialog
2. now set this contentPanel layout to be CardLayout
3. add your other panels to the cardlayout as required.
4. Also make sure off the layouts you use on each of the panels you're adding to the CardLayout. By default JPanel uses FlowLayout, I think, and this is not ideal for a JTable. So you might need to play around with the layout of the panel containing the table as well; try out BoxLayout or BorderLayout for that as well.

Categories

Resources