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".
Related
I am developing a Java Swing-based application with different perspectives. For the "main menu" perspective I do not want the window (JFrame) to be decorated, while in other perspective I do want the window to be decorated. In other words, I need want to change the decoration attribute dynamically.
I have tried to use setUndecorated(false) and setUndecorated(true), but I seems I can only set this once, before actually showing the window.
Is there a way to achieve this?
From the javadoc:
Disables or enables decorations for this frame. This method can only be called while the frame is not displayable.
Therefore, once the JFrame is packed and/or displayed, you can no longer change that value. If you want to change the undecorated state of a JFrame you will need to dispose() it first, then change the state and eventually make it visible again.
After all, I had to take a different approach.
The former solution did work, as I stated in my last comment.
However, it was showing the default LAF window decoration, while I was using a different LAF.
So the result was graphically inconsistent with the rest of the LAF. Finally, I came with the right solution, I used setUndecorate(true) for my frame. Then, when I had to change my perspective to one using decorations I simply had to use the following code
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
And when I had to revert to the non decorate perspective, I use
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
This approach didn't need to dispose the window and show it again (which actually produced a brief but still visible hide/show of the window)
I am a beginner and I am trying to make a text editor and I want to create a pop up window for text format when I press a menu button where I can put all things like font face, font size , font style etc. Can you tell me how I can make this new window? Thanks for your patience!
For example Notepad:
I think what you're after is a dialog of some kind.
Take a look at How to Make Dialogs for more details.
What I would do is design the basic UI onto a JPanel. I would then add this JPanel to an instance of a JDialog (possibly even using a JOptionPane) and show this dialog, making sure to make it modal, so you can easily retrieve the values set by the user.
This means that you can decide how best to show the user interface or even show it in a number of different ways as it's not constrained to a single top level container
You can simply create a brand spanking new JFrame and it will still be counted as the same application.
Tip: Use Eclipse Window Builder
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.
I am developing a Java Swing-based application with different perspectives. For the "main menu" perspective I do not want the window (JFrame) to be decorated, while in other perspective I do want the window to be decorated. In other words, I need want to change the decoration attribute dynamically.
I have tried to use setUndecorated(false) and setUndecorated(true), but I seems I can only set this once, before actually showing the window.
Is there a way to achieve this?
From the javadoc:
Disables or enables decorations for this frame. This method can only be called while the frame is not displayable.
Therefore, once the JFrame is packed and/or displayed, you can no longer change that value. If you want to change the undecorated state of a JFrame you will need to dispose() it first, then change the state and eventually make it visible again.
After all, I had to take a different approach.
The former solution did work, as I stated in my last comment.
However, it was showing the default LAF window decoration, while I was using a different LAF.
So the result was graphically inconsistent with the rest of the LAF. Finally, I came with the right solution, I used setUndecorate(true) for my frame. Then, when I had to change my perspective to one using decorations I simply had to use the following code
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
And when I had to revert to the non decorate perspective, I use
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
This approach didn't need to dispose the window and show it again (which actually produced a brief but still visible hide/show of the window)
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);
}