Reputation: 579
I am developing a java swing desktop application with NetBeans and I want to set the JFrame to the centre of the screen.
from the net I understand that I can use
setLocationRelativeTo(null);
to set the frame to the centre
But i am unable to insert the code into the NetBeans IDE
because both the frame.pack()
and frame.setVisible()
are generated codes of the NetBeans 7 IDE
and it will not permit any code insertion between the two methods.
I need to to obtain the following :
frame.pack()
setLocationRelativeTo(null);
frame.setVisible()
Any suggestion on how to fix the problem?
Upvotes: 10
Views: 87966
Reputation: 1
in order to make sure that jframe is at the center in netbeans follow the instructions :
right click on jframe --> properties -->
now turn on the : generate center option and make sure that form size property --> generate resize code
Upvotes: 0
Reputation: 1
try....
public class_name{
initComponents();
setLocationRelativeTo(this);
}
Upvotes: 0
Reputation: 69
at the constructor writing give code below will make your jframe at the center of the screen
public ProjectWork_jframe() {
initComponents();
Dimension screenSize,frameSize;
int x,y;
screenSize=Toolkit.getDefaultToolkit().getScreenSize();
frameSize=getSize();
x=(screenSize.width-frameSize.width)/2;
y=(screenSize.height-frameSize.height)/2;
setLocation(x, y);
}
Upvotes: 0
Reputation: 1261
Within the Netbeans Designer area, choose your JFrame, go to code.
Within code, change Form Size Policy to "Generate Resize Code"
Then choose the Generate Center Option.
Upvotes: 0
Reputation: 41
Follow the following simple steps:
That's it.
Upvotes: 4
Reputation: 171
On the constructor of the frame, you have this:
public frame() {
initComponents();
}
You only have to put this line: "this.setLocationRelativeTo(null);" under the "initComponents();"
And you'll have this:
public frame() {
initComponents();
this.setLocationRelativeTo(null);
}
Run it, and it the frame should show on the center of the screen =)
Upvotes: 16
Reputation: 339
From the link @DerekMannering posted:
Netbeans actually generates centering logic on its own versus through property use, so you won't see setLocationRelativeTo option. Go to the Code tab in the properties window. Locate the "Form Size Policy" label in the Code tab. In the drop-down select "Generate Resize Code". Then make sure the property with the label "Generate Center" is checked. Too you'll want to make sure "Generate Size" is checked as well. Should be by default, but you should check anyways. Now, that will work best for JFrame or Frame extensions.
Upvotes: 0
Reputation: 21
I am not sure if you got an answer to your problem, but a solution was given in the following link by Wade Chandler on the NetBeans forum. Unfortunately the originator of the query being answered was impatient to say the least and you have to work through some angst to get to the answer.
http://forums.netbeans.org/ptopic37419.html
Wade shows how to centre the GUI and what aspects of the code you can modify.
As an aside, if you want to place the GUI at a set position on the screen then you can use:
This requires access to the Properties ->Code window as well. 1. Ensure you select the Frame (not a component or outside!) 2. Go to the Code tab in the properties window for the JFrame. 3. Locate the "Form Size Policy" label. 4. In the drop-down select "Generate Resize Code". 5. Modify the form position by clicking the "..." next to it. 6. Also, make sure "Generate Size" is checked as well.
If you want to place the GUI top left on the screen then use the default [0,0] for form position. To position a second GUI next to the first use [450,0]. In this case the x-value has been changed to 450, the Y-value is kept a 0. To move the GUI down the screen then change the Y-value from 0 to say 450.
Finally, there is a NetBeans bug 226740 that can result in problems when trying to centre some aspects of a GUI. It will probably not affect you, but it is useful to be aware of it.
Regards
Derek
Upvotes: 2
Reputation: 4727
Is setVisible()
on generated code? Strange. Anyway, you can right click the JFrame
in Navigator
and select Properties
. Go to Code
and select it to do nothing. Then manually insert you code after initComponents()
in the JFrame
constructor.
Upvotes: 2