What layout should I use In this case? - java

I am trying to make something like the above, a frame that consists of many JButton components (gray rectangles). These buttons are of the exact shape but their positioning looks like the picture. I am new to AWT, Swing and such and don't know what layout should I use, I know a bit of GridLayout and BorderLayout which are obviously not suitable in this case.
Any ideas?

I think its better to use GridBagLayout. GridBagLayout is a sophisticated, flexible layout manager. It aligns components by placing them within a grid of cells, allowing components to span more than one cell. The rows in the grid can have different heights, and grid columns can have different widths. For further details, see How to Use GridBagLayout. For more details - https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html#gridbag

I strongly recommend you to use Grid Bag Layout, with this layout you can make good looking GUI or strange GUI in most cases. In your problem Check insets in Grid Bag layout, this layout is kind of tricky but if you will learn it, you will see that it is simple.

Related

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

A Flexible Layout manager that will change the layout according to resize

In my JPanel, I have 6 buttons laid out in a row (using FlowLayout as of now). The default size of the panel is enough to accommodate these buttons in one row. But, when the frame is resized it gets stuck at the size that is the sum of the minimum sizes of each button.
I need a layout manager that simply puts the buttons in a new row on re-sizing of the panel.
I'm pretty new to Java Swing so I apologize in advance if this is a trivial question.
MigLayout is by far the best layout manager I've ever used. Things that used to require nested containers and lots of hard to understand code can be done in a single container with simple to understand (and maintain) string constraints.
The flow layout is capable of your desired behavior (moving components into new row if they cannot fit). Check out the swing tutorial (run FlowLayoutDemo). You'll have to show us your source code to find out, whether there is some other constrain which prevents it.
FlowLayout does actually paint components on a new row, but the problem is that the preferred size of the panel doesn't change so in many cases you can't see the components (unless you happen to add the panel to the CENTER of a BorderLayout).
One solution is to use the Wrap Layout, which extends FlowLayout to recalculate the preferred size of the panel so that you see the buttons on a new row.

JavaFX layout equivalent to GridLayout

I'm used to working with Swing to create GUIs but for a recent project I've chosen to switch to JavaFX. I'm having some trouble with recreating a certain layout I used to make using a GridLayout.
I desire the following behavior:
2 columns that scale proportionally with the size of their parent that center their contents.
Using Swing, I would make JPanel with a GridLayout (1 row, 2 columns) and add 2 JPanels with a BorderLayout, adding the actual content to those panels with the centered constraint.
Then I could add the first panel to any container that has a layout that stretches with the frame and all would be well.
I seem to be unable to recreate this behavior in JavaFX in a simple way. I can think of ways to do it using bindings and combining several panes but I was hoping there is a layout that does this automatically. I've tried using TilePane, HBox, GridPane, AnchorPane, SplitPane and even BorderPane but none of them seem to do what I want them to.
Is there a recommended way to accomplish this? I would much prefer not to embed Swing into the application. Basically what I want is to be able to split the content into two columns that automatically stretch with the Stage/Scene (JFrame).
GridPane? Some references here
http://docs.oracle.com/javafx/2/layout/builtin_layouts.htm#CHDGHCDG
http://docs.oracle.com/javafx/2/api/javafx/scene/layout/GridPane.html

Using Layouts in Java

I have a JFrame with layout BorderLayout, I add JPanels using BorderLayout.CENTER
I have a JPanel(added using CENTER), that uses GridLayout to place 4 buttons.
These buttons are MASSIVE, taking up the whole screen. Or, I do it my way as follows:
I have a JFrame with layout null, I set JPanel bounds and add them.
I have a JPanel:
It sets it's own bounds so it takes up center 1/2 of screen, and is only 1/8 of the screen
tall.
It adds buttons using grid layout, and results in a nice line of buttons.
Obviously the second option looks better, but as I wish to conform to the norm, and always use layouts... How to I mix customization and layouts?(Or just solve my problem at all)
When you add a componeent to BorderLayout.CENTER, it will expand to fill the remaining space of the container not used by the other portions of the BorderLayout. Also, you can only add one component at a time to BorderLayout.CENTER. Subsequent adds will replace the previous component.
As for solving your exact problem, I suggest you start by taking a look at A Visual Guide to Layout Managers. This will give you a primer on what layouts are available in the Swing API. Oracle also has more in-depth tutorials for each layout. By nesting layouts, you can give your UI any look that you wish while leveraging their power, especially auto-calculations when a window is resized or otherwise changed.

Any way to control the scaling of Swing components?

I'm trying to have my Swing components not scale either vertically and scale horizontally, or the other way around (In other words, I only want them to have one axis of freedom. They can either scale horizontally in some cases, or vertically in others).
Is there any elegant way of doing this, without adding a multitude of JPanels?
LE: I'd rather not make any significant modifications to the already made layout that I've received.
I'm hoping for a magical, simple fix to this issue, like JTextField.don'tScaleVerticallyIdiot();
You need to use an appropriate layout manager.
The two most powerful are MigLayout and FormLayout. I personally prefer MigLayout because it's easier to use directly, whereas FormLayout works well only with a window designer like WindowBuilder.
Both have similar feature characteristics.
Using MigLayout, you can specify if a component is allowed to grow on a specific axis, and if so, what kind of weight it should be provided (as opposed to extra space going to other components).
I think you can achieve that with setting BorderLayout as a LayoutManager of the container, so:
Adding swing component on BorderLayout.EAST will make it scalable vertically only.
Adding swing component on BorderLayout.WEST will make it scalable vertically only.
Adding swing component on BorderLayout.NORTH will make it scalable horizontally only.
Adding swing component on BorderLayout.SOUTH will make it scalable horizontally only.
Adding swing component on BorderLayout.CENTER will make it scalable both horizontally and vertically.
You didn't mention which LayoutManager that you are currently using.
Personally, I would use GridBagLayout for what you are trying to achieve.
I want it to be a single row as height and scale horizontally as much as I drag the frame, but without adding multiple JPanels.
I think BoxLayout with a PAGE_AXIS can do that.

Categories

Resources