Freely position Swing elements - java

Why are there these stupid layout managers which position my stuff. I can understand doing that on mobile platforms, where there're many different device sizes. But that's not my target. I want to freely position any component based on coordinates, like a TabbedPane, ScrollPane - where I want it.
Is there any layout-manager that let's me do what I want? Like it is common with Qt Designer, WPF, WindowsForms, and so on? Like it has to be?

Layout managers give you great flexibly, even if you have a fixed sized frame.
I would strongly suggest using them. Checkout the visual guide to get an idea of which ones might be useful to you. Also, you can layer different managers to get additional effects:
http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html

if you want to use absolute positions:
myPanel.setLayout(null);
JButton myButton = new JButton(myPanel);
myButton.setBounds(left, top, width, height);

the response is :
you should use
JFrame frame = new JFrame("Laying Out Components Using Absolute Coordinate");

Related

Why null layouts and absolute positions are bad practice in Swing?

I have code with null panel:
JPanel thePanel = new JPanel();
thePanel.setLayout(null);
I used setBounds(x, y, width, heigth), for example here:
label2.setBounds(150, 220, 459, 311);
I read that this is not a good practice, can you tell me why?
Is it only because when you want to add something between one component and another you have to set them positions again and again or is it something else?
There are multiple problems with absolute positioning:
Different screen sizes and resolutions, what looks great on your machine would come out differently on another screen with a different resolution.
In the same vein, what happens when a user resizes the screen because they want to run your application side by side with some other application (to copy paste or whatever)
Different locales and font sizes, what happens to your labels when you use another locale, or another font, or change the font size.
There's probably more reasons, but using a layout manager makes sure that content is redistributed when windows are resized, or when content of a container changes, ...
Using absolute positioning is probably the easiest way in the beginning, but it pays to get to know the different layout managers and how they operate. It will save you from a lot of headaches due to, for example, changing requirements.

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.

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.

How to make resolution free app

I have set 320x480 size for canvas/widget of app. How can I make it resolution free.I have to draw some tips on particular location using AbsoluteLayout.If I change size of canvas/widget then the tips are displaying at wrong coordinates.
You should not work with absolute layout. Learn how to use other layout managers in Java. It can look complicated at the beginning but it's your only hope to get a resizable application without all the burden of managing size by yourself. Layout managers are precisely done to handle components positionning whatever the size of the container is.
The 3 basics layout managers are :
BorderLayout
GridLayout
FlowLayout
A very usefull layout manager is BoxLayout (though the constructor is weird).
Here is a good docs from SUN about layout managers.
When you master this, and it's not so difficult, you can build almost any application in swing.
And if you work with custom components, I mean JPanel where you overrided paintComponent, then you should consider 2 options :
Not to scale at all for performance reasons,
Scale using AffineTransform on your graphics. But this is a different topics, your questions seemed more general about swing components.
Oh, and by the way, I think you should really accept answers from people and vote for what answer helped you. It's the minimal way to thank people here.

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