Resize JFrame and Components Based On Computer Resoultion - java

I've made a java application in netbeans and am wondering how to have the size of the jframe half the width and height of the computer resolution and also having the components comply with this change. I tried putting code and it did make the frame half the height of the computer resolution but my components, such as buttons and textfields, stopped showing. How can I achieve this? Thanks.

(EDITED)
Set the JFrame's layout manager to GridLayout. In the properties window of the GridLayout itself (select in the navigator window) set columns to 1 and rows to 2. This should give you what you want and you won't have to get into the code.
This is the key code being called within the initComponents() method of your JFrame subclass (created by NetBeans) but it is important to understand where it is:
getContentPane().setLayout(new java.awt.GridLayout(2, 1));
I love Netbeans but you do have to understand the basics.
Good luck with your project. Swing is an awesome toolset that was way ahead of it's time.

As usual in this situation the key is using the right combination of layout managers for your containers. You're probably using NetBeans generated code (something I recommend you avoid until you are very comfortable with Swing coding), and it's probably having you use GroupLayout, a fine layout, but one that might not behave as well as you'd like on resizing components. I suggest that you go through the layout manager tutorial and try to nest JPanel containers and play with different layouts that re-size well such as GridLayout, GridBagLayout and BorderLayout to try to create the best layout that can re-size well.

Related

How can I make a resizable GUI with Java swing?

Hi :) I'm making my first GUI with Java swing, and I have a problem: when I change window dimension, "internal components" don't change their size.
I use absolute layout, it may be an important thing to know.
Absolute layout means that you are using absolutely defined constraints about position size e.t.c. To create resizable GUI. you need to use more "flexible" layouts.
One simple option to start is BorderLayout. Hovewer you have to leverage use of panels in this case.
Documentation
Another possibility may be the Grid bag layout but that often involves more work to do.
You can solve this problem by using the MigLayout in combination with the WindowsBuilder. This is a WYSIWIG editor for SWT and Swing Layouts. But BorderLayout and GridBag Layout can srink and grow as well.

Best Layout Manager for creating two columns in a single panel?

I am trying to create a GUI for a program with an undefined number of rows but stay with two columns. Currently the only way I can get it to look how I want is using FlowLayout but the window of course needs to be smaller than desired. It needs to happen within one panel as well because it is being added to a tabbed pane.
What would be the best layout manager to solve the problem that is in the JDK? Or would just kind of brute forcing it with AbsoluteLayout be the best approach (as the user shouldn't really be resizing the window)?
I have attached the desired appearance.
Thanks for any help in advance.
Several layouts can do what you want. I'd suggest GridLayout (easy to use, but columns will be equal width) or GridBagLayout (harder to use, but you have lots of control). You could also use a BorderLayout and put all the fields in a sub-panel on the WEST and all the drop-downs in a sub-panel on the EAST. The difficulty with that is ensuring that the rows have the same height, since they won't be constrained by the layout itself.
The best thing to do would be to go through the Java tutorial on layouts and get up to speed on what the various layout managers can do.
Also, since you're using Swing, you could just use a JTable (as Gilbert Le Blanc suggests in his comment).

How to resize components within JFrame when JFrame is resized?

RESOLVED
I've looked around for a while now and I can't seem to figure out how to do this.
I have lots of components within my JFrame and I am able to resize my JFrame. When I resize the JFrame I want the internal components to resize along with the JFrame, but instead they stay exactly where they were when the GUI was first run.
How can I fix this problem? Is there a value I'm not setting somewhere or what?
Edit:
Also, I'm not using any layout manager.
getContentPane().setLayout(null);
You state:
Edit: Also, I'm not using any layout manager. getContentPane().setLayout(null);
That's your problem. You should almost never use null layout. It is much easier to use and maintain a gui that uses layout managers in a smart way. Read the layout manager tutorials as they can help.

Adding more than one JPanel in a single JFrame

I have created a JFrame - now I want to add the 4 JPanel in that frame at a particular location. How can set the location of panels in the frame?
Use (possibly nested1) layouts for the logic. See Laying Out Components Within a Container for details. They can:
Include default spacing in the constructor (often)
Calculate how big the GUI needs to be in order to display the components (in whatever PLAF, on whatever system the app. is deployed).
Extra spacing can be organized by adding an EmtpyBorder to child components.
See the nested layout example
Placing components in a container is quite a complicated subject in Swing. Instead of defining the exact places for your components, you would normally use a layout manager that arranges them in a certain way.
Here is the tutorial you should read to get a (visual) clue about the different layout managers: A Visual Guide to Layout Managers
However, the standard layout managers of Swing can be cumbersome for more complex layouts. Either, you could use nested layouts to get the desired result, or you could use a very powerful third-party library: JGoodies Forms. The downside is of course that you have to learn yet another library. Therefore, I would only recommend it for a bigger project.
For me it is good way to set GridbagLayout for the container of the frame. There are several visual swing GUI editors available to do this easily. You can use NetBeans GUI editor or GWT Designer (https://developers.google.com/web-toolkit/tools/gwtdesigner/) for complex GUI designing tasks
If its 4 locations, you can use BorderLayout,by default its the CENTRE, but it also have EAST, WEST , NORTH, SOUTH locations for the placement of the components. You can also use setLocation to put the panels in the appropriate locations, if a layout isn't used.
Its even better to use GroupLayout developed my NetBeans team in 2005, use Windows Builder Pro, now provided by google for free.
set the layout of the Frame to be null via setLayout(null)
create 4 JPanel and set their location using setLocation method
add these panels using JFrame's add method

Resizing a JFrame and all the contents inside

I am making a game, and I am wondering if there is any way that you can resize the frame so that everything inside will also resize with it too?
That is the job of the LayoutManager. Choose your LayoutManager carfully so that it arranges your components properly. For example, if you use a BorderLayout, the center component will be stretched to take all the space not occupied by NORTH-SOUTH or WEST-EAST.
You need to choose the right LayoutManager for you. You can have layouts within layouts by putting JPanels within JPanels.
If you find the Java Swing library annoying to work with like that, then I suggest using Netbeans IDE where you can design visually how your GUI will look like.

Categories