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