Java GUI Layout managers - java

It seems that I can do everything with a single Grid Layout--even tasks that might require multiple nested layouts if I did not use Grid Layout. Is a single Grid Layout a better approach than many different nested layouts?

Most screens work very naturally with BorderLayout and it gives good resizing behavior to components inside it.
Personally I'd really rather nest various components inside a BorderLayout--possibly using Grid inside and possibly using others.
Experiment a lot with resizing behavior and see what you think.
Resizing is the key.

Related

How to setup this GUI with Java Swing?

I got a GUI to make and I've tried so many ways, but I can't seem to build it correctly, I always get the layouts wrong and my application gets all messed up. I just want some help on how to build something close to the GUI in the image, except the image part, that isn't necessary. Doesn't need to be exactly like it, I just need some ideas of what layouts to use and how. Thanks
As with all problems, start by breaking it down into manageable chunks.
Associate the various UI elements together into groups of "functionality" and separate them into separate components - this way you can focus on the individual layout requirements for each section
From this, I would then focus on breaking down each group into individual components and focus on there core functionality
So this is pretty basic, I'd use a GridBagLayout because it would allow me to anchor the components to the top position of the container. You could also use the VerticalLayout from SwingX
So, there are a number of possibilities, but, because it would be simple to do, I'd use a GridBagLayout, as it would allow me to provide more space for the two lists
A little more complicated, but still, GridBagLayout, as it allows more control over providing space to individual components
And, GridBagLayout for all the reasons highlighted above.
Finally...
Bringing it all together...
I'd probably start by placing the green, blue and yellow sections into a separate container (using, surprise, a GridBaglayout, as it allows for components to expand cells) and then combine it with the purple section onto a container using a BorderLayout
I would recommend that you take the time to look at Laying Out Components Within a Container and try somethings and see what does and doesn't work.

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

GUI Layouts - How do I place labels,textfields,buttons exactly where I want them?

So, I'm trying to design my first GUI app, and i'm stuck on putting elements exactly where I want them. I'm not trying to let anyone do my job for me, but some starters would be great.
For example:
The basic answer is, you don't. Pixel perfect positioning is an illusion in modern user interfaces. Why? Because no two platforms are equal (unless they are exact copies). Each computer will have different requirements when it comes to how information is rendered on the screen, most notably, fonts.
Font metrics will change between platforms, meaning that the way a font is rendered on your screen won't be the same as it is rendered on someone elses. This causes no end of issues not only of an individual component, but how the surrounding components should react.
The best choice is to use layout managers, which provide "guides" on how components should be laid out and how they effect surrounding components.
Based on your example above, I would suggest you would actually need (at least) three layout managers.
At the base, you would use a BorderLayout, this would separate the form from the buttons.
You would need a fields panel and a buttons panel.
The fields panel would contain the actual fields and probably use a GridBagLayout. The buttons panel would contain the buttons and probably use a FlowLayout
The form panel would be added to the CENTER position of the base panel and the buttons to the SOUTH position.
Take a look at Laying Out Components Within a Container for more details
What you want to look at is various layouts in Java (Given that you are asking a basic question I am assuming you are using Swing).
See this link for more info: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
It will show you the various layouts available in Java and how to use them. You will need to use different Components along with different layouts to render your GUI exactly the way you want it.

How to resize components when resizing a JFrame? Using Null Layout

I am using the NetBeans Designer to create a JFrame. Also it is worth mentioning I am fairly new to Java so I might not understand some things / do things correctly. The frame has about 100 panels, more buttons than I would even think about counting, about ~40 tables, basically most swing components the NetBeans designer provides are being used within the frame. Also for the main frame I am using Null Layout (in order to have a background image inside a JLabel). I know it is not recommended but it doesn't affect the general layout of things as I'm using panels/LayeredPane/TabbedPane for everything, each with it's own design (most of them on Free Design with no Layout specified - that's how I started, didn't know about Layouts and it would take ages now to rearange everything after using Grid Bag Layout for example).
Now getting to my problem, I need to be able to resize the frame and make it resize all components contained. I have to carry a presentation tomorrow of it and I just noticed it doesn't fit on smaller displays (and resizing it doesn't do it properly, it just hides components). I do not care much if it's just an improvisation / not the best approach to the problem as after the 15 min presentation I will probably never open it again.
Thanks.
I would try to go through all the components tree and try to set them smaller font and reduce all their bounds to some static %.
In other words for each component multiply x,y,widht,height to e.g. 0.75 and call setFont() passing derived font of 25% smaller.

Java Swing: Choosing the correct LayoutManager

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.

Categories

Resources