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.
Related
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.
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 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.
I need to implement ui for list of contacts like in skype. An contact represented by custom class(JContact) which derived from JPanel. I tried to use different layouts but not received expected result. Main frame has next structure.
JFrame -> JPanel(contactsPanel)-> JScrollPane(scrollContacts)->JPanel(contactPanel)
scrollContacts.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollContacts.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
If use FlowLayout for contactPanel (see FlowLayout in image)
Strange behavior i think, because expected what each contacts will one under one because indicate HORIZONTAL_SCROLLBAR_NEVER for scrollContacts.
If use Grid or Box layout. Layout automatically re-size my panels, it's look very ugly. see Grid&Box layouts image.
Expected result see "expected" image
----SEE IMAGE----
I'm not native speaker, so please sorry for my bad English.Thank you for attention!
Quoting the Swing tutorial:
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
So the result you get is expected.
A GridLayout object places components in a grid of cells. Each component takes all the available space within its cell, and each cell is exactly the same size. If the GridLayoutDemo window is resized, the GridLayout object changes the cell size so that the cells are as large as possible, given the space available to the container.
So the result you get is also expected.
When a BoxLayout lays out components from top to bottom, it tries to size each component at the component's preferred height. If the vertical space of the layout does not match the sum of the preferred heights, then BoxLayout tries to resize the components to fill the space.
So the result you get is also expected.
But, a box layout can contain glue components to avoid that.
I would thus use a vertical box layout, and add a vertical glue as the last component. Read the tutorial.
How would I go about making the length of the tabs automatically resize based on how much room is left in that row of tabs.
Picture:
As you can see the tab's width is based off the text in the tab.
If you need me to explain what I want better then just ask me because I don't know if I made it clear enough.
You can use a custom component and set it's preferred size. For example, in ButtonTabComponent of TabComponentsDemo:
label.setPreferredSize(new Dimension(...));
You have to choose an appropriate dimension based on other aspects of your layout, so it won't be automatic.
I want to define a size for the actual tabbed pan.
The size of the JTabbedPane is a function of the dimensions and LayoutManager of the Container to which it has been added. In the example cited, the default layout of the frame's content pane is BorderLayout, and add(pane) adds it to the center by default.
To accomplish your goal, I see two approaches:
Divide the current width of the enclosing Container among the existing tabs and repaint the tabbed pane, as shown in this example.
Develop your own implementation of TabbedPaneUI and interpret SCROLL_TAB_LAYOUT accordingly.