Switch to full style
Graphics and animation Java code examples
Post a reply

Window events-handling by WindowListener

Sat Feb 09, 2013 7:22 pm

Window events-handling by WindowListener
java code
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class WindowEventExample extends Applet
implements WindowListener,
ActionListener {
String lineSep;
TextArea textAreaObj;
Frame frameObj;
Button btnOne, btnTwo;
static final String SHOW = "show";
static final String CLEAR = "clear";


public void init() {
btnOne = new Button("Click to bring up a window.");
btnOne.setActionCommand(SHOW);
btnOne.addActionListener(this);

btnTwo = new Button("Click to clear the display.");
btnTwo.setActionCommand(CLEAR);
btnTwo.addActionListener(this);

textAreaObj = new TextArea(5, 20);
textAreaObj.setEditable(false);

setLayout(new BorderLayout());
add("North", btnOne);
add("Center", textAreaObj);
add("South", btnTwo);


frameObj = new Frame("Frame with Window Event ");
frameObj.addWindowListener(this);
frameObj.add("Center",
new Label("The applet listens to this window for window events."));
frameObj.pack();

lineSep = System.getProperty("line.separator");
}

public void stop() {
frameObj.setVisible(false);
}

public void windowClosing(WindowEvent e) {
frameObj.setVisible(false);
printMessage("Window closing", e);
}

public void windowClosed(WindowEvent e) {
printMessage("Window closed", e);
}

public void windowOpened(WindowEvent e) {
printMessage("Window opened", e);
}

public void windowIconified(WindowEvent e) {
printMessage("Window iconified", e);
}

public void windowDeiconified(WindowEvent e) {
printMessage("Window deiconified", e);
}

public void windowActivated(WindowEvent e) {
printMessage("Window activated", e);
}

public void windowDeactivated(WindowEvent e) {
printMessage("Window deactivated", e);
}

void printMessage(String prefix, WindowEvent e) {
textAreaObj.append(prefix
+ ": "
+ e.getWindow()
+ lineSep);
}

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == SHOW) {
frameObj.pack();
frameObj.setVisible(true);
} else {
textAreaObj.setText("");
}
}
}




Post a reply
  Related Posts  to : Window events-handling by WindowListener
 How can a GUI component handle its own events     -  
 check keyboard events using C++     -  
 Handle Focus events with FocusListener     -  
 Text Listener for events TextListener-on TextArea-TextField     -  
 Exception handling     -  
 What is Event Handling?     -  
 try-with-resource automatic handling     -  
 ComponentListener event handling     -  
 Error handling in AJAX     -  
 Session handling using Struts     -  

Topic Tags

Java Applet, Java AWT, Java Events