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.
Related
-- Little note, I attempted to upload an image of my game to illustrate my question, however I do not yet have the required reputation to do so. I apollogise for this.
I would like to create a drop down screen from the top HUD element on my game which the player can type into, effectively becoming a chat window, the actually window is not an issue and I understand that you can disable background and boarder rendering of Java's Swing components so that isn't an issue.
My question is simple, can I take advantage of java's Swing components like JTextField and position them exactly within the bounds of this area, without having to deal with java's layout classes. So this is a summary:
How do I set the final size of the swing components MANUALLY and
How do I set coordinates of the components MANUALLY With out using a layout manager
Yes you can use a null layout on the container and call setBounds(...) on the component to MANUALLY place them. And this is usually a VERY BAD THING to do as it forces you to paint yourself into a layout corner making it very hard to upgrade or enhance your GUI later. It also guarantees that your GUI will look terrible on all platforms and screen resolutions other than one. Many newbies usually go this route initially, and then most leave it eventually after gaining more experience with Swing as they run into its failings, weaknesses and limitations.
For a more complete answer, consider giving more specifics and in image (we can help with this) of your GUI layout requirements.
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 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.
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.
I am making a gui application where I need a wide panel on one side of the frame, and a thinner panel on the other. The panels are the same height, but when I re-size them I want them to keep a consistent width difference from each other while still changing size.
Here is what it would look like when restored to a smaller size:
And here is what it would look like when it was maximized or made bigger:
I was trying to use Grid Layout for this, but I couldn't quite figure out how to make one smaller and change at a lesser rate when you were making the window bigger. My question is which layout would get me to what I want, and what functions within the layout manager would lead me to the result?
You could use GridBagLayout, but that always seems more trouble than its worth.
Instead, take a look at JGoodies, especially their FormLayout. It's been years since I've done Swing programming, but remember the JGoodies layouts making life a lot easier.