JavaFX layout equivalent to GridLayout - java

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

Related

Working with GUI's in java

I really need some help i've tried many scenarios of code and none seem to deliver the outcome.
I would like to produce a GUI where the Question is on one line and centered and the possible answers are on the next line centered too. I've produced this code - my thinking was make a Box Layout and add two flow layouts inside the box layout one for the question and one for the possible answers
This behavior is correct as you are using FlowLayout.
The FlowLayout class puts components in a row, sized at their
preferred size. If the horizontal space in the container is too small
to put all the components in one row, the FlowLayout class uses
multiple rows.If the container is wider than necessary for a row of
components, the row is, by default, centered horizontally within the
container.
Use any other layout such as SpringLayout or GridBagLayout.

What layout should I use In this case?

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.

Put 2 FlowLayouts one under another with a margin

I'm trying to dynamically create 2 FlowLayout instances (one of them has about 50 dynamically created buttons, other has about 10) so that there is a slight gap between them. I'm stuck with this, I tried to use BoxLayout and put flow layouts in it, and then create gap with Box class and its methods, but it didn't work. I tried with BorderLayout, and GridBagLayout, but that didn't work either.
You can see what I'm trying to do on the image below. I would appreciate if anyone has an idea how to do this. The actual question is: How can I create a gap between the first 50 buttons and other 10 buttons, where both groups of buttons have to be set in flow layout.
This effect can be achieved by adding an EmptyBorder to each of the containers with FlowLayout.
Many layouts allow us to set a gap between components, depending on which layout the 2 containers with flow layout are being added to, that might be a possibility here.

Java Layout for specific task

I need suggestion for layout type for following task. I have a panel on which user will be able to add or remove some components (label or another panel), which are all same size. There will be specific number of components at same column (like 4 components per column) but the number of components in rows will depend on user. The distance between components will be fixed, right, left up top. I will link you the image of what i need... Thanks.
link
I think you want a GridLayout, however I suggest using a third-party layout manager like MigLayout. For the case where you have empty cells, you can nest JComponents within each other with different layout managers ( see this SO question). MigLayout would be easier because it can simulate a GridLayout while respecting the preferred size you set on your JComponents, which allows you to have empty cells without the components stretching.

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.

Categories

Resources