How can I work with absolute layout in javafx? - java

I'm new in javafx
and I want to start building a project
but the problem is I don't know how to use absolute layout in javafx
In swing i just write :
setLayout(null);
JButton b = new JButton("Here is a test");
b.setBounds(20, 10, width, hieght); // here is how i use the absolute layout in swing
But in javafx the things are more complex
Therefore I really need help.
I've already seen all the javafx layouts here:
Using Built-in Layout Panes
But nothing could help me.
Can anyone help me?

In JavaFX, there is no separate LayoutManager class. There is a set of predefined layout panes, subclassing Pane, which lay the child nodes out in different ways.
The Pane class does no layout, so using it is the closest equivalent to using a null layout manager in Swing. Incidentally, this approach is not really recommended, either in Swing or in JavaFX.
The tutorial runs through the built-in layout panes. There is also a useful presentation (requires registration to Parleys) on general layout in JavaFX 2 and later.

Related

JavaFX layout equivalent to GridLayout

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

how to keep GUIs from overlapping in Java

I am trying to create a Java 7 address book using Eclipse and when I test run the JFrame, JLabels aren't visible and JButtons overlap. (Buttons will take up the whole JFrame)
For example
add(saveButton);
add(cancelButton);
add(headingLabel);
headingLabel.setVisible(true);
cancelButton.setLocation(200,200);
saveButton.setLocation(400,200);
cancelButton.setSize(200,50);
saveButton.setSize(200,50);
What am I doing wrong?
You should use a LayoutManager instead of absolute coordinates. Using Layout managers, you don't have to care so much about the exact positions of your GUI elements. The layout is done for you.
Check out the Visual Guide to Layout Managers. It explains the most common layout types with visual and code examples.

Java GUI Layout managers

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.

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.

Freely position Swing elements

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

Categories

Resources