Window listeners Assignment Help

Assignment Help: >> Component listeners in Java >> Window listeners

Window listeners:

We are now in a position to tackle one of the irritating issues that we have had to deal with so far. That is, the fact that when any of the example programs are run, clicking on the close window icon does not end the program, it simply closes the window. Clicking the close window icon closes the window because that is the action of that icon, but it does not interact with any programs that we have written. We are now in a position to link the clicking of that icon with our program code. We will use the WindowAdapter class, which is the adapter class for the WindowListener interface and contains seven methods including windowOpening, windowClosing and windowClosed. We will use the windowClosing method to link the clicking of the close window icon with our program and get the program to stop when the window is closed.

As before, we will first define a class to close and quit the program:

import java.awt.event. *;

public class CloseAndQuit extends WindowAdapter
{
public void windowClosing (WindowEvent e)
{
System.exit(0);
}
}

We have defined CloseAndQuit by extending WindowAdapter. In this case, we have provided it as a stand-alone class, rather than as an inner class. This is partly to show that listeners can be stand-alone classes, but also because in this case CloseAndQuit does not need access to any of the variables and methods of the surrounding class and is not as logically connected to the main program. The class CloseAndQuit is a general class that we will be using in all of our visual programs from now onward and by defining it once we will be able to avoid including it in all of our code.

Having defined the listener class CloseAndQuit, we now add an instance of the class to the window using:

addWindowListener(new CloseAndQuit( ));

So long as the class CloseAndQuit is in the same package as the program we are running, then the above line of code will work.

As an example, we will take the program in the previous section - NewMouser - and add the above single line anywhere in the constructor (after the call to super, of course). By putting the class CloseAndQuit into the same package as NewMouser we will see that when we click the close icon on the window, the whole program stops.

Java provides its own equivalent to the CloseAndQuit class and it is a part of the JFrame class. Rather than defining your own class and adding the window listener in the constructor, you can simply include the following line in the constructor:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

This method will set the operation that is to occur when the user initiates a 'close' on this frame. In this case the operation is to close the window and exit the program.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Window listeners homework and assignments? Live Window listeners experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Window listeners homework help, java assignment help and Window listeners projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd