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.
Related
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.
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.
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.
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.
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.