Java Swing: Choosing the correct LayoutManager - java

I'm building a PropertyPanel. Currently I'm using a GridLayout to manage the JLabels and their corresponding fields where I can specify the value. But the problem is that the GridLayout automatically manages the size of the columns: it makes them the same width.
This means when I'm having a big value field, the colum, is getting bigger (which is good), but the other column (with all my JLabels) is getting bigger as well. Here is a screenshot:
< BAD
As you can see, the image property has a huge value, which makes both columns bigger, and I'm having a lot of space after the JLabels.
So, I'm searching for a LayoutManager which makes each column as big as necessary.
I want a layout like this (it's edited with Gimp):
< GOOD
Thanks

You can use SpringLayout for this. See How to Use SpringLayout.
Example layout:
Remember that you also can nest layouts.

SpringLayout is what I typically use for forms like this. Although I think GridBagLayout would also work nicely.

I tend to try to hack everything by mixing GridLayout and BorderLayout, so maybe it's not the best solution but...
Create two GridLayouts, both have a single column. One for the labels the other for the controls.
Now create a BorderLayout to be the parent.
Add the left grid to the BorderLayout.WEST and the right grid to the BorderLayout.CENTER.

While this was answered 11 hours ago, I just thought I'd pop in & make a suggestion. I suggest GroupLayout.
I was looking to break from nested layouts for a name/value dialog recently and looked at both GroupLayout & SpringLayout. It seemed the only advantage offered by SpringLayout was that it could achieve the right aligned text of the labels (there may be a way to do it using GL, but I couldn't figure out how). On the downside, the Java Tutorial examples for SpringLayout used a whopping 'helper class' to define layout constraints.
In the end (it was only a very short 'study') I chose to use GroupLayout.

Consider using MigLayout. If constrained within the current JDK, GridBagLayout.

Here's an overview of the standard LayoutManagers:
http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html
You could e.g. use GridBagLayout or the non-standard MigLayout, if you want to code the GUI by hand.
If you want to use a GUI builder (e.g. the one in NetBeans) you could use the GroupLayout.

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

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.

Where can I find tools for building groups for GroupLayout?

In many cases, a GroupLayout may include pieces with simple structures. For example, it seems sensible to write something like an AlignedStack object that would allow the user to put together a bunch of components/groups/gaps making up a left/center/right-aligned vertical or top/center/bottom aligned horizontal "stack" and then retrieve the resulting horizontal and vertical groups. I can surely write what I need myself (it seems like fairly trivial boilerplate), but I'm wondering if anyone's built a package of such things so I don't have to.
Not sure I understand what an "aligned stack" is, but if I do then you should be able to use a BoxLayout. It does horizontal and vertical positioning and components can be top/bottom or left/right aligned.
Read the section from the Swing tutorial on How to Use Box Layout for more information and working examples.
Several approaches are common:
This example nests panels having GroupLayout in a panel having a vertical BoxLayout.
This example illustrates how to add rows dynamically to a panel having GroupLayout.
The NeBeans and Eclipse GUI editors are useful for experimenting; examine the generated code to see the effect.
Try Eclipse WindowBuilder ( https://www.eclipse.org/windowbuilder/ ). It can build any swing layout for you, featuring a pretty smart code parser / generator, all swing components and layouts, and an easy to use drag and drop / property based editor. I think it will suit your needs.

How do I neatly indent some components using Java Swing layouts

Using Swing, what is the best way to indent some components underneath a checkbox or radio button? I need to make something in the style of Firefox 3.6's Options->Privacy dialog where some checkboxes are indented under a "main" checkbox. I can use any of the standard AWT/Swing layout's including GroupLayout. I also have JGoodies FormLayout available to me. I tried using setLeadingColumn offset in FormLayout at first, but it seemed like it was not going to work well unless I was indenting under a Separator. Maybe I was just doing it wrong?
Is there anything like SWT GridLayout's horizontalIndent setting? That would be perfect.
I am working with JDK1.6.0_23.
Create a JPanel for the sub components. Then you can add an EmptyBorder to the panel with the required indentation.
There are several ways to do this:
Set each components border to: new EmptyBorder (0, 10, 0, 0).
Use a GridBagLayout and use an Inset (0, 10, 0, 0) to pad the left side.
Use a GridBagLayout and have the main checkbox span two columns, whilst the sub checkboxes are offset by placing them in the rightmost column.
Supply custom checkbox icons that have some empty space added to their left hand edges.
etc.
My advice would be to learn the GridBagLayout - it is somewhat unwieldy to use but it does give you pretty much all the layout power you could want. The JGoodies stuff is useful for when you want particular automatic column sizing behaviour that GBL won't give you without some additional code on your part.
You should be able to accomplish this just fine with FormLayout, just add another column for the sub items, and have the main item span 2 columns.
Another option is to use SpringLayout and add padding.

Java - Layout Manager Selection

Is there a simply layout manager I can use in a JPanel to create something akin to a bar chart? FlowLayout almost meets this need. The added component orientation needs to be left to right (default for FlowLayout), but they need to "rest" on the bottom of the panel with excess space at the top (not available in FlowLayout). Also, the components will all the be the same height and width.
Thanks.
A BoxLayout will do the trick as demonstrated in this posting
If you are going to do something like a bar chart, you might want to consider not using Components at all. Just have a single JComponent that overrides (IIRC) paintComponent. It'll be easier to do the calculations in a manner appropriate to a bar chart rather than trying to use an inappropriate layout manager abstraction.
FWIW, I default to GridBagLayout, even if a simpler layout manager will do, on this basis that the code can be more consistent.
You can do exactly what you want in GridBagLayout. Yes, I know everyone hates GBL; yes, I know I'll get down-voted. But it really is not difficult to understand and you can use it for almost any layout goal.
The trick to get a component to "stick" to the bottom is to use the anchor and fill properties of the GridBagConstraints object properly (i.e. SOUTH and NONE)
A BoxLayout might work for you. It lets you layout components left-to-right or top-to-bottom, with the tightly coupled Box class to force spacing constraints.
I actually prefer the FormLayout, since it is very flexible but you have to write a lot of code though. And in the beginning its a little bit confusing with its percentage and pixel parameters.
But you can for example tell a control that it is 5 pixels left of another control (thats the main part...it layouts controls in relation to neighbors), then it takes 100% of the lasting space availabel including a border space of 5 pixels (you need to use -5 then).
I think it looks somewhat similar to this
FormData data = new FormData();
data.left = new FormAttachement(neighborControl, 5);
data.right = new FormAttachement(100, -5);
...
button.setLayoutData(data);
This example is for JFace, but there are Swing implementations as well.
I will look up my old code later this day to check if the code I wrote is right :)
HereĀ“s a additional link

Categories

Resources