I am trying to write a GUI application using NetBeans, and I am getting increasingly tired of it..
I constructed a "Java Desktop Application" using NetBeans. It creates the main form as a org.jdesktop.application.FrameView descendent. I would like to know what is the proper way to add some code that is executed when the form is closed.
My current attempt so far is to add a WindowAdapter object using getFrame().addWindowListener in the constructor, which doesn't work. Because you can't call getFrame while the frame is not constructed yet. And I can't see it as an event somewhere i the GUI builder.
Java Desktop Application which is available in NetBeans IDE 6.9.1 is only for historical purposes and is not recommended for use in projects. The NetBeans IDE 6.9.1 also shows this warning when we try to create a new project using Java Desktop Application option.
Given that let me answer your question assuming you are still using the Swing Application Framework and you want to add a windowClosing listener to the Main Window.
When you create a Java Desktop Application you get three classes (assuming you typed DesktopApplication1 as the name of your application):
DesktopApplication1.java
DesktopApplication1AboutBox.java
DesktopApplication1View.java
To add the window closing listener, write the code in configureWindow method of the class DesktopApplication1 as follows:
#Override protected void configureWindow(java.awt.Window root) {
root.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// write your code here
System.out.println("Window Closing");
}
});
}
with regards
Tushar Joshi, Nagpur
This is just an addition to Tushar's answer.
If you want to provide a confirmation dialog on the window closing, then you must change the default exit behavior by adding an ExitListener into the startup method and making the canExit method return false:
#Override
protected void startup() {
addExitListener(new ExitListener() {
public boolean canExit(EventObject event) {
return false;
}
public void willExit(EventObject event) {
}
});
show(new DesktopApplication1View(this));
}
Related
First, I have only been into Java for couple of months. Delphi before that, and before that, Clipper (showing my age!).
I want to enable a checkbox when my program starts. I have tried the following in the WindowActivated Event in Netbeans but it does nothing.
private void formWindowActivated(java.awt.event.WindowEvent evt){
cbMyCheckbox.setEnabled(true);
}
I know it is firing because I set a breakpoint on it and see that it is being executed.
Place cbMyCheckbox.setEnabled(true); in the class constructor right after the call initComponents();
Like so
public class YourClass (){
public YourClass() {
initComponents();
cbMyCheckbox.setEnabled(true);
}
}
Or you can select your chekbox and in the properties check the "selected" option
try to do this code cbMyCheckbox.setEnabled(true); after calling the function show();
I want to create a program in Java with a main window, that can open more subprograms in other windows.
I created a simple JavaFX program for the main window, and it works as expected, like so:
public class MainThread extends Application {
#Override
public void start(Stage primaryStage) {
// code goes here...
}
}
... and I created other Java classes the same way.
I tried to run them simply with new SubProgramThread();- as I would create a new instance of any object- but that hasn't worked for me- it doesn't run the start() method in the subprogram classes.
Is that the right way to create an Application instance?
Thanks.
I do this all the time. I have a menu of items. When a user clicks the item button I create the new window by calling start() on the FX class:
new TyAltSvr().start(new Stage());
Some people don't like calling start() but it hasn't caused me any problems.
I have a problem as follows:
I've written a simple minimalistic Application that utilizes the SWT for the GUI.
In a specific tab, where it displays a table that get's filled with Informition via a REST Api Call. Additionaly, i have another method to export this table into a CSV-file.
This works absolutely fine. Now I need some kind of autoupdate/-export for which I implemented a Swing-Worker like this:
protected class AutoExportWorker extends SwingWorker<Integer, String> {
#Override
public Integer doInBackground() throws Exception {
System.out.println("Worker Start!");
while (true) {
System.out.println("In Loop");
//updateTable();
//exportToCSV();
for (int i = 0; i<interval;i++) {
System.out.println(interval - i);
Thread.sleep(1000);
}
}
}
The Class is a subclass of the Composite which views the table and holds the methods "updateTable()" and "exportsToCSV()". Now I'm aware that this Worker doesn't do much when I call these Methods but I can't figure out, how to do this correctly. Do you have any hints for me?
Best regards!
If you are using Eclipse RCP, you should use Jobs API. In plain SWT, there is no built-in solution. You need to use Display.asyncExec to update the table.
I am using Swing Application Framework JSR(296) for my Swing based Java application.
Similar to AboutBox, I have followed the usage of #Action and added some JDialog classes to my project.
The problem is, when I close the main frame, my application still runs in background.
To overcome this I added following code to the configureWindow() of my main application class:
protected void configureWindow(java.awt.Window root) {
root.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// write your code here
Application.getInstance(MyApp.class).exit();
}
});
}
But with this modification whenever I close the the dialog (including AboutBox), it also closes the main frame.
What should I do to prevent the entire application from exiting and just close the dialog box?
Update:
I am using NetBeans IDE 7.01 which allows to create Swing Application Framework project.
It generates a project skeleton as shown below:
MyApp
|--myapp
| |--MyApp.java
| |--MyAppAboutBox.java
| |--MyAppView.java
|
|--myapp.resources
|--MyApp.properties
|--MyAppAboutBox.properties
|--MyAppView.properties
NetBeans IDE allows to add actions from Window->Properties menu.
MyApp class extends org.jdesktop.application.SingleFrameApplication which is my main class.
MyAppView extends FrameView which is my main view.
Implementation classes of javax.swing.JDialog are in the myapp.view package.
The WindowEvent class has a method call getWindow(), which returns the window that is closing.
Inside your windowClosing method you can check: if the window is the main application window, use the code that you currently have. If it is not, just call Window.dispose()
Edit: I didn't notice that you were creating custom dialogs in your application. Maybe you forget to dispose them? You should add code like the one in the auto generated about box:
#Action public void closeAboutBox() {
dispose();
}
and call this action whenever the dialog closes. If this is not the problem, a thread dump will probably help you in order to find out which thread is running when you close the main window.
I think what you are actually looking for is setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);. Have a look at the JavaDoc API...
I would like to use Window Builder and use the MVC paradigm simultaneously. It is very messy with a complex window when Window Builder adds all the code to just one file.
I would like the default file created to be the 'view'.
I would like to keep my control actions (event listeners) in a 'controller' class. Is there a way to have Window builder automatically put the event listeners in a class of your choice rather than adding to one monolithic file?
I don't know how Eclipse's Window Builder works, but I do know that NetBean's creates anonymous inner classes that call a custom method for each button and then allows the programmer to alter the body of the custom method. If Eclipse is similar, then you can simply have this custom method call a method of your Control object. Sure it adds a layer of indirection, but it's a small price to pay to give you complete control over your control.
For instance, if I create a JButton called "myButton" and then have the code generator create an action for my button, it will create this code:
myButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
myButtonActionPerformed(evt);
}
});
and will allow me to access and write code in the generated method, myButtonActionPerformed:
private void myButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
And inside of this method I would call my Control's method:
private void myButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (myControl != null) {
myControl.myButtonAction();
}
}
The control class could look something like
class MyControl {
void myButtonAction() {
//TODO: implement control code
}
}
The GUI would need a setControl(MyControl myControl) method in order to "inject" the control into the GUI.
Another way of splitting up your code is to use the wizard to create JPanels as your own controls, then add those in a layout in the main window using the "Choose component" tool (looks like three beans) from the palette. Easy and much cleaner. Although I understand it still merges the "V" and "C" in MVC...