I'm trying to learn Java so sorry if this is a stupid question, but I seem to be unable to change the default close operation on my JFrame in NetBeans.
In NetBeans I created a JFrame and implemented some controls on it using the NetBeans GUI designer. However, I noticed that after I close the JFrame, my application would close even though I have another form that is supposed to appear afterwards.
After digging through the JFrame generated code I found:
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
From research it appears that this causes the application to close when the frame is closed. I would prefer to not have this happen. From looking around, it appears I either need to use DISPOSE_ON_CLOSE or DO_NOTHING_ON_CLOSE in order to get the window to close but still have additional code to run.
However, I can't figure out how to get NetBeans to change this value through the designer. I found the defaultCloseOperation in the Properties -> Bindings window, but every time I enter something into the text area and press enter my text disappears. The binding has an elipsis but I can't figure out what the binding window actually does.
How can I change my setDefaultCloseOperation() to a new value?
It is right there in the properties panel. Just make sure that the whole JFrame is selected. You can select your JFrame from Inspector panel.
Try to update defaultCloseOperation value from Properties -> Properties tab, not binding tab
If you open the file with a different editor (i.e. notepad) you will realize that netbeans puts some lines in the file to make specific sections read only.
You can always remove those lines, or even edit whatever you want with another editor.
You can always right-click on your frame, choose Customize Code and make your changes there.
Related
So I made a JPanel and a JFrame using the Design functions in NetBeans and when I try to drag the panel onto the JFrame, I get the error "The component cannot be instantiated. Please make sure it is a JavaBeans component."
What I did was create an empty Frame and then create the Panel and put it all together using the panels, buttons, and text fields. I didn't modify the source code or anything. Is there some kind of code I need to add into the panel to make it work with the JFrame? Or perhaps I missed a step in the design tab? I've included a picture that shows exactly what it is I made.
jpanel
Clean and build the project (and sub projects as required).
If you're using Netbeans 7+, you can check the messages.log found in C:/Users/{username}/AppData/Roaming/NetBeans/{Netbeans version}/var/log and check for the exception that caused the problem.
I think the new 7.2 beta reports the problems to the user now (thankfully).
I have build an application using the Netbeans Wizard Framework as outlined here and here. I have my application working properly, but the problem I have is that the application does not show in the windows taskbar. I am pretty sure the problem is that the Wizard Framework uses a JDialog instead of a JFrame, but I can't figure out if that can be changed.
If anyone has experience with this framework, please let me know if it can be made to appear in the taskbar.
Changing is quite easy.
Open the form in NetBeans.
Change extends javax.swing.JDialog to extends javax.swing.JFrame.
Save the file and close it.
Reopen the file.
In designer mode, make a change. I usually just change the name of a component.
Save the file.
Undo the change and save the file again.
You should be all set.
Wizard Framework uses a JDialog instead of a JFrame, but I can't figure out if that can be changed.
don't use more than one JFrames, this's road to the hell, you cann't to setToFront, setOwner, iconify, etc ...
use JDialog or JWindow instead with parent to the one single JFrame
use undecorated JDialog, with setModal() or is possible to set various ModalityTypes too
If anyone has experience with this framework, please let me know if it can be made to appear in the taskbar.
this Swing Framework is long time depreciated
you can use SplashScreen repeatly instead of JDialog/JWindow
I made a GUI in Eclipse using the Google WindowBuilder plugin. The problem is that when I right-click on the design representation of my code and click, "Test/Preview," my GUI works and looks perfectly (the picture on the right), but when I click the "run" button in Eclipse to actually run the code, the GUI looks all weird (the picture on the left).
Does anybody have a clue as to what the problem is?
Ok this could have multiple reasons, the most obvious one being, that you (accidentally?) set your JButtons' background Color to the same dark-gray as your Frame's background. Check This first! WindowBuilder's preview has a quirk of sometimes not showing recent design changes on some elements immediately.
If that Is not the case with you, it might be some weird formatting thing.
Have you manually changed your buttons' formatting within the code and not within the design tab? Then add (Say your button is called okButton) okButton.repaint(); after the changes you manually made.
If this still doesnt work,
Try adding okButton.setVisible(true); (Althought that is pretty far fetched, seeing that a button outline can be seen!)
I've created a stand-alone java desktop application in Netbeans 6.9. I want to set the action for the close button of my application. I want to know how and where to set the code for the action of that close button. Can anyone please help me regarding this?
You have to register an ActionListener on your close button. In this listener you can define what do to.
How add ActionListener to JButton in Java Swing
I think answers for How to close a java swing application from the code will be helpful too
Right-click on the button then, > Events > Action > actionPerformed. NetBeans will generate the action listener for you:)
Edit: If you want a close listener, then read here.
Once you have the handler working, one convenient approach is to "set the default button by invoking the setDefaultButton() method on a top-level container's root pane." See the tutorial section How to Use JButton Features for details.
I dislike to wake up the zombies but, here is what I would do (this is considering that you are working in a Window based application, otherwise this is going to be useless!!!):
I would set a window listener for the close operation here is the way to do it.
Then I will delete manually the temp folders and files...
Obviously the window listener I'm talking about would go on the last window you (as a user) should close, this would not work either if you want it to happen when the last window is closed, but there is not an order specified to do that, some workarounds for that is making all your windows to share a flag/counter to indicate if it is time to delete the temp files/folders.
Again if your application is going to work underground sometimes (I mean that you can dispose all windows without shutting down the application), or is a daemon this won't work
If you just want to exit the application when the close button is hit, you can use
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
as explained in the Oracle tutorial
This is driving me crazy. When I set an appropriate size for my window in design-mode it just ignores this size and uses something else when I compile and run. Even tough I set the minimumSize and preferredSize it just ignores this... Why? How can I set my own size?
Even if you've set the size with minimumSize and preferredSize, you might have forgotten to call Window.pack() in which Swing will resize the components (and all of it's subcomponents) according to the sizes set.
You call it in your window (or whatever is building your window) after all the preferred sizes are set.
Alternatively you can use the Component.setSize() method but it comes with some caveats.
Have you checked if you really set the size of the JFrame or of a contained JPanel?
Have you tried setSize?
I've found myself in a similar situation while using netbeans ide. I had a read of the following thread, that helped:
http://forums.netbeans.org/ptopic28011.html
Seems as though the swing application framework is saving the app's window size in a subfolder within your home dir (for Windows, the "Application Data" folder, for Linux, in your "~/" home folder).
For example, for my application 'CrapApp', swing had saved some last-window-size info into the sub-folder "~/.CrapApp/", into a file called "mainFrame.session.xml".
So no matter how I re-sized the window within the designer, upon running, it seemed to have ignored it and instead loaded the window size from the preferences within this sub-folder.
So my solution was to delete this preserved-settings sub-folder, eg, in my case, "rm -rf ~/.CrapApp/"
Then the problem went away and I could re-size within the designer and run the app with this re-sized window now visible.
This made me want to learn what triggered this issue. I noticed that simply running the app within the netbeans ide and closing it didn't generate the sub-folder.
After a bit of tinkering with my app, I noticed the following action triggered the generation of this "~/.CrapApp/" sub-folder.
Going to my app's "Help >> About"
Click the "Close" button in the about dialog that appears
Exit the app
And now the "~/.CrapApp/" sub-folder re-appears. This help/about dialog was auto-generated by netbeans ide, so I didn't really tinker with it, but this seems to have been the culprit in my case.
Perhaps it may be a bug in the netbeans ide, I'm using a somewhat old version (v6.8), which seems roughly from the era of your original post too.