Window closing in Swing Application Framework - java

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...

Related

Open JFrame, only after successfull login verification with database. Using Eclipse?

what I'm trying to do is open up a main application from a login screen, only after the login information has been verified in the connected database.
Using Eclipse, what I have so far:
database.java: connection to MS Access Database using UCanAccess. (Success)
login.java: A login window that extends JFrame. When a username and password is entered, it is verified with the database. (Success)
Home.java: The main application window, that I want to only be accessible with a correct username and password. Does not extend JFrame, but has a JFrame within it.
Now, I have been able to set it up so that if the entered username and password are correct, a window pops up saying "Successful login". However, how do I approach setting it up so that after the successful login, it opens up Home.java?
I have looked at:
Open a new JFrame - I have tried the setVisible with my home but Eclipse returns an error saying to create a setVisible method in Home...I thought this is supposed to be an automatic control? After trying to create the method, more issues just arise.
JFrame Open Another JFrame - which suggests adding actionListener and then setting it visible..which I have done: public void actionPerformed(ActionEvent e) {this.setVisible(false); new Home().setVisible(true); but Eclipse just doesn't open up the login window at all. Initially, I thought it could be because my success message is in the actionListener, however even after removing that it still does not work.
Call Jframe from Java class and Open window after button click - My only conclusion is that this is not working since Home.java does not extend JFrame? However, I read through other sources that it is not good to use "extends JFrame"?
I guess I also don't have an understanding of the difference between "extends JFrame" vs a new JFrame within a class? I have been learning java on my own and I'm new to GUI creation. Maybe I am missing something very obvious, but I just can't find a solution.
Any ideas? Thanks
To give an idea, my Home.java starts like this:
public class Home {
private JFrame frame;
private JTable data;
private JTextField Column1;
private JTextField Column2;
private JTable table;
// Launch the application.
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Areas window = new Areas();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
// Create the application.
public Home() {
initialize();
}
//Initialize the contents of the frame.
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 697, 518);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Start by defining a simple work flow which allows you to understand the logical path you want your code to take.
Break down the areas of responsibility so that your objects only do the work that they absolutely have to, for example, your login component should only collect the credentials from the user, it should not be responsible for validating them, that should be the responsibility of some kind of controller.
Equally, neither the login component or it's controller should be responsible for determine what happens after a successful login, that is the responsibility for another controller
This decouples the code and encourages reuse, so the next time you need to present some "login" view, you don't have to recode the whole thing, simply update the controller, model and/or view as required, or re-use it as it is
The concept of a controller in Swing is a little different then a normal MVC implementation, because Swing is already a form of MVC.
What I tend to do instead, is define a contract between the controller and the view which describes what events the view generates (and the events that the controller can expect), for example attemptLogin. This disconnects the controller from the view's implementation, so the view is free to form the view in what ever way it feels like, so long as when it wants to validate the actual credentials it calls attemptLogin
So, you would start with a "main controller" which is responsible for controlling the login and main application controllers. It defines the work flow between the login and the main application and monitors for appropriate events which the controllers may generate to make decisions about what it should do next
A basic flow of operation might look something like
This concept is demonstrated in Java and GUI - Where do ActionListeners belong according to MVC pattern?
Just create a method in your Home class that sets its JFrame to be visible:
public void setJFrameVisible(boolean visible)
{
frame.setVisible(visible);
}
Then, assuming your instance of your Home class is called "home", all you would have to do is:
home.setJFrameVisible(true);
Let me add a bit more context. When you're extending JFrame, the class inherits all the methods/properties of the JFrame class. That's why when you extend JFrame you can just call obj.setVisible(true), because your class inherited the setVisible method from the JFrame class. What you have is a class that contains a JFrame, so you have to call the setVisible method on the internal JFrame, not the class.

How to start new JavaFX (Application) thread?

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.

Minimize intro part in eclipse application

I have created my own intro in eclipse application as follows:
public class CustomIntro extends IntroPart {
public void createPartControl(Composite container) {
//add intro, works perfectly fine
}
//override other essential methods
}
The above code works perfectly fine, now I want to minimize this intro programatically. Upon a click of button the intro should be minimized. Actually I want to launch a internal browser upon click of button, and the intro should be minimized and launched internal browser should be visible.
As suggested by #greg-449, I extended the IntroPart than implementing IIntropart. Thanks for that, but my issue still remains. Any help is appreciated.
As #greg-449 pointed, you should extends IntroPart abstract class as is explained in the documentation.
You can use setPartState().
Use this in your IntroPart:
this.getIntroSite().getPage().setPartState(this.getIntroSite().getPage().getActivePartReference(), IWorkbenchPage.STATE_MINIMIZED);

How to get the TrimBar of a TrimWindow in Eclipse RCP 4?

I put a toolbar on the top of my TrimmedWindow in my application. I have a handler which has to check whether a check button is pressed on this menu bar or not.
I tried putting EMenuService in my execute() method of the handler but it has no useful methods. If I debug into my application I can see my menu in the EMenuService object however.
How can I get my menu from the Eclipse context?
Without code it's hard to help you.
But the basic idea for your handler is the following :
public class BrokerHandler {
#Inject
// the services you need
#Execute
public void execute(IEclipseContext context, #Named(IServiceConstants.ACTIVE_SHELL) Shell shell)
throws InvocationTargetException, InterruptedException {
// do some stuff
}
}
Then, in your application.e4xmi you need to create a Window>Trimmed Window>Trim Bars>Window Trim>Toolbar>Handled Tool Item wich points to your Commands>Command that is binded to your Handlers>Handler pointing to your java class with a method annotated #Execute as described above.
Then each execution of the #Execute method means the user has pressed the toolbar button.
You can pass messages to other parts of your app with the event broker service, or store some of your own stuff in the IEclipseContext.
You can have a look here: http://xseignard.github.com/demoCamp2012/prez/#1
Hope this helps, but your question is too blurry.

A windowClosing event for a Java jdesktop FrameView?

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));
}

Categories