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?
Properties ->Code -> check out Generate Center
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 =)
Follow the following simple steps:
select the frame and go to the properties.
click on code section and click on checkbox Generate Center.
That's it.
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.
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
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.
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.
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);
}
try....
public class_name{
initComponents();
setLocationRelativeTo(this);
}
Related
While working with Java, I find it hard to position my main window in the center of the screen when I start the application.
Is there any way I can do that?
It doesn't have to be vertically centered, horizontal alignment is the more important goal for me. But vertical alignment is also welcome.
Use setLocationRelativeTo(null)
This method has a special effect when you pass it a null. According to the Javadoc:
If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.
This should be done after setting the size or calling pack(), but before setting it visible, like this:
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
I always did it in this way:
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
where this is the JFrame involved.
You can call JFrame.setLocationRelativeTo(null) to center the window. Make sure to put this before JFrame.setVisible(true)
Just click on form and go to JFrame properties, then Code tab and check Generate Center.
As simple as this...
setSize(220, 400);
setLocationRelativeTo(null);
or if you are using a frame then set the frame to
frame.setSize(220, 400);
frame.setLocationRelativeTo(null);
For clarification, from the docs:
If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.
i am using NetBeans IDE 7.2.1 as my developer environmental and there you have an option to configure the JForm properties.
in the JForm Properties go to the 'Code' tab and configure the 'Generate Center'.
you will need first to set the Form Size Policy to 'Generate Resize Code'.
I am using NetBeans IDE 7.3 and this is how I go about centralizing my JFrame
Make sure you click on the JFrame Panel and go to your JFrame property bar,click on the Code bar and select Generate Center check box.
If you use NetBeans, simply click on the frame on the design view, then the code tab on its properties. Next, check 'Generate Center'. That will get the job done.
If you explicitly setPreferredSize(new Dimension(X, Y)); then it is better to use:
setLocation(dim.width/2-this.getPreferredSize().width/2, dim.height/2-this.getPreferredSize().height/2);
You can use this method, which allows the JFrame to be centered and full screen at the same time.
yourframe.setExtendedState(JFrame.MAXIMIZED_BOTH);
In Net Beans GUI -
go to jframe (right click on jFrame in Navigator) properties, under code, form size policy property select Generate Resize Code. In the same window, Untick Generate Position and tick Generate Size and Center.
Enjoy programming.
Ramana
I will provide 3 methods to your question :
You can either use the simplest method to center it, relative to your viewport, which is :
setLocationRelativeTo(null);
The second one, is to use :
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width/2 - frame.getSize().width/2, dim.height/2 - frame.getSize().height/2);
And the third one, the most customizable one, is a bit more complicated, but is the most useful. (in my opinion) :
1.) You create a JButton and add it to your panel.
2.) You add an ActionListener event to it.
3.) On your button click, you invoke the method :
frame.getLocationOnScreen(); which you need to save to a Point
variable => Point location = frame.getLocationOnScreen;
4.) After that, you invoke : System.out.println(location);, so that you can get the location on the screen that you want to put your frame
to - it will print it to the console.
5.) You delete the button and ActionListener, after you've got the x, y coordinates.
6.) You invoke : frame.setLocation(x, y);
That's it.
The code looks like this :
JButton button = new JButton();
button.setText("Get Location");
panel.add(button);
ActionListener onButton = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Point location = frame.getLocationOnScreen();
System.out.println(location);
}
};
button.addActionListener(onButton);
After you have the location, do this :
frame.setLocation(445, 195);
Hope I was helpful. ^
I'm making a JFrame that contains a lot of JButtons and JTextfields which contain data from a database. In the design everything is okay, but when I run my program the JButton and the JTextfield change their places and I don't know why.
Here is a screen shot from the design window and the run window:
Seems like you have aligned the textfields to each other and the buttons too. But you should have aligned one button to one textfield. One way to do this would be to use a GridBagLayout instead of the FreeDesign option in NetBeans. To do this right-click onto your frame and select Set Layout>Grid Bag Layout.
You can then right-click again and select Customize Layout... in order to place your components as you wish.
I have JTabbedPane with five tabs and each have Jpanel i want add different images for each panel in NetBeans IDE
Right click on your project and add a new package, name it resources. This will need to be done so Netbeans imports your picture into that folder
Add a JLabel to the Panel
Hightlight the JLabel and go to the Properties pane on the right
In the property that says icon click the ... button. That will take you to a dialog
Choose External Image radio button
Click the ... next to the file text field
Pick your Image and click OK.
Click Import to Project
Click OK, You should see the image in your graphic layout
Note this will only give you an Image, but doesn't really act as a background, because the JLabel is it's own component. I'm not really sure how to achieve a backgroud with GUI Builder. I'm not too familiar with the technology. Though if you were to write your own code, there are numerous answers here on SO that I'm sure you'll find useful. The only tricky thing about GUI Builder is that they have auto-generated code that you really can't play around with, which circumvents what I know about creating a background image.
NOTE : this only works for JLabels as JPanels don't use Icon. An alternative would be to hand write your own JPanel code in the constructor and draw the image, overriding the paintComponent method.
Change the layout of your Jframe to null.
Create a jlabel and cover whole jframe with it.
Add your image to the icon property of the inserted jlabel.
Change the layout of jframe back to free layout.
You are done 👍👍
It worked for me
May be you'll find this link useful.
This tutorial shows you how to use the IDE's GUI Builder to generate the code to include images (and other resources) in your application. In addition, you will learn how to customize the way the IDE generates image handling code.
Handling Images in a Java GUI Application
Basically, following are the steps.
Drag a Label to the JFrame
Add a new package (for the image to be stored)
Select Label and go to the Properties category
Select the Icon property and click 'Import to Project...'
Select the image and then the newly created package
In Properties window of Label, select text property and delete it.
What property I am modifying when when dragging or double click the border of a JFrame, or JDialog or similar, using the visual editor that comes bundle within every Netbeans distribution?
I thing that the IDE is somehow modifying the size attribute inherited from JComponent Class. But I see the code generated by the IDE and there is no call to setSize... so that let me really wondering if somebody know what is behind.
Firstly, when using any JFrame or JDialog you should use pack whereever possible.
Having said that, if you click the "Code" of the "Properties" window for the form, you will see two properties: "Form Size Policy" and "Designer Size".
I have an open-source java swing application like this:
http://i47.tinypic.com/dff4f7.jpg
You can see in the screenshot, there is a JPanel divided into two area, left and right area. The left area has many text links. When I click the SLA Criteria link, it will pop-up the SLA Criteria window. The pop-up window is JFrame object.
Now, I'm trying to put the pop-up window into right area of the JPanel, so that means no pop-up window anymore, i.e. when I click the SLA Criteria link, its contents will be displayed at the right area of the JPanel. The existing content of the right area of JPanel will not be used anymore. The concept is just same like in the java api documentation page: http://docs.oracle.com/javase/6/docs/api. You click the link in the left frame, you'll get the content displayed at the right frame.
The example illustration is like this:
(note: it's made and edited using image editor, this is not a real screenshot of working application)
http://i48.tinypic.com/5vrxaa.jpg
So, I would like to know is there a way to put JFrame into JPanel?
I'm thinking of using JInternalFrame, is it possible? Or is there another way?
UPDATE:
Source code:
http://pastebin.com/tiqRbWP8 (VTreePanel.java, this is the panel with left & right area divisions)
http://pastebin.com/330z3yuT (CPanel.java, this is the superclass of VTreePanel and also subclass from JPanel)
http://pastebin.com/MkNsbtjh (AWindow.java, this is the pop-up window)
http://pastebin.com/2rsppQeE (CFrame.java, this is the superclass of AWindow and also subclass from JFrame)
Instead of trying to embed the frame, you want to embed the frame's content.
There is (at least) one issue I can see with this.
The menu bar is controlled by the frame's RootPane.
Create you're self a new JPanel. Set it's layout to BorderLayout.
Get the menu bar from the frame (using JFrame#getJMenuBar) and added to the north position of you new panel.
Get the frames ContentPane and add it to the center position of the panel.
There is undoubtedly countless other, application specific issues you will run into trying to do this...
No, you don't want to "put a JFrame into a JPanel" and your illustration above doesn't demonstrate this either. Instead it's showing a subordinate window on top of (not inside of) another window. If you absolutely need to display a new subordinate window, I'd recommend that you create and display a JDialog. The tutorials will explain how to do this, or if you get stuck post your code attempt and we'll help you work with this.
Edit 1
You state:
I need to convert from the pop-up window style into the jpanel content style. It's just like the java api documentation page style: docs.oracle.com/javase/6/docs/api When you click the text in left frame, it doesn't show any pop-up, right? The content is displayed at right frame directly. So that's basicly my goal. The source code is quite big. I will try to paste the source code if possible.
What you are looking for is to simply implement a MouseListener in a JList or JTable, and when responding to the click get the content based on the selection made. This has nothing to do with placing a JFrame in a JPanel and all to do with writing the correct program logic. Again, display it in a modal JDialog -- but that's all secondary to your writing the correct non-GUI logic. You're really barking up the wrong tree here. Forget about JFrames, forget about JPanels for the moment and instead concentrate on how you're going to extract the SLA Criteria data when it is clicked on.
Edit 2
I think I see what you're trying to do -- instead of JFrames and JDialogs, use JPanels and swap them using a CardLayout which would allow you to swap views.
I had skimming the source codes, I saw that the AWindow.java has internal panel (APanel.java) to hold the window's content, and it also has a public method to return the content panel object (getAPanel()). With this, I can use it for fetching the window's contents into other container.
Finally, I decided to use JTabbedPane in the right area of VTreePanel for displaying the pop-up window's contents.
You cannot put a Jframe into a JPanel. Instead you should try to create a separate panel that has functionalities like your JFrame and embed that into your JPanel.
Since you can put a JPanel into another JPanel but not a JFrame into another JPanel