Skip to main content

Event Handling in Java

Handling Events with an Anonymous Class
If an event handler is specific to a component (that is, not shared by other components), there is no need to declare a class to handle the event. The event handler can be implemented using an anonymous inner class. This example demonstrates an anonymous inner class to handle key events for a component.
component.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
}
});
Handling Action Events
Action events are fired by subclasses of AbstractButton and includes buttons, checkboxes, and menus.
AbstractButton button = new JButton(quot;OK");
button.addActionListener(new MyActionListener());
public class MyActionListener
implements ActionListener {
public void actionPerformed(ActionEvent evt) {
// Determine which abstract
// button fired the event.
AbstractButton button =
(AbstractButton)evt.getSource();
}
}
Handling Key Presses
You can get the key that was pressed either as a key character (which is a Unicode character) or as a key code (a special value representing a particular key on the keyboard).
component.addKeyListener(new MyKeyListener());
public class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent evt) {
// Check for key characters.
if (evt.getKeyChar() == 'a') {
process(evt.getKeyChar());
}
// Check for key codes.
if (evt.getKeyCode() == KeyEvent.VK_HOME) {
process(evt.getKeyCode());
}
}
}
Handling Mouse Clicks
component.addMouseListener(
new MyMouseListener());
public class MyMouseListener
extends MouseAdapter {
public void mouseClicked(MouseEvent evt) {
if ((evt.getModifiers() &
InputEvent.BUTTON1_MASK) != 0) {
processLeft(evt.getPoint());
}
if ((evt.getModifiers() &
InputEvent.BUTTON2_MASK) != 0) {
processMiddle(evt.getPoint());
}
if ((evt.getModifiers() &
InputEvent.BUTTON3_MASK) != 0) {
processRight(evt.getPoint());
}
}
}
Handling Mouse Motion
component.addMouseMotionListener(
new MyMouseMotionListener());
public class MyMouseMotionListener
extends MouseMotionAdapter {
public void mouseMoved(MouseEvent evt) {
// Process current position of cursor
// while all mouse buttons are up.
process(evt.getPoint());
}
public void mouseDragged(MouseEvent evt) {
// Process current position of cursor
// while mouse button is pressed.
process(evt.getPoint());
}
}
Detecting Double and Triple Clicks
component.addMouseListener(
new MyMouseListener());
public class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent evt) {
if (evt.getClickCount() == 3) {
// triple-click
} else if (evt.getClickCount() == 2) {
// double-click
}
}
}
Handling Focus Changes
component.addFocusListener(
new MyFocusListener());
public class MyFocusListener
extends FocusAdapter {
public void focusGained(FocusEvent evt) {
// The component gained the focus.
}
public void focusLost(FocusEvent evt) {
// The component lost the focus.
}
}

Comments

Popular posts from this blog

Asynchronous Vs. Synchronous Communications

Synchronous (One thread):   1 thread -> |<---A---->||<----B---------->||<------C----->| Synchronous (multi-threaded):   thread A -> |<---A---->| \ thread B ------------> ->|<----B---------->| \ thread C ----------------------------------> ->|<------C----->|

WebSphere MQ Interview Questions

What is MQ and what does it do? Ans. MQ stands for MESSAGE QUEUEING. WebSphere MQ allows application programs to use message queuing to participate in message-driven processing. Application programs can communicate across different platforms by using the appropriate message queuing software products. What is Message driven process? Ans . When messages arrive on a queue, they can automatically start an application using triggering. If necessary, the applications can be stopped when the message (or messages) have been processed. What are advantages of the MQ? Ans. 1. Integration. 2. Asynchrony 3. Assured Delivery 4. Scalability. How does it support the Integration? Ans. Because the MQ is independent of the Operating System you use i.e. it may be Windows, Solaris,AIX.It is independent of the protocol (i.e. TCP/IP, LU6.2, SNA, NetBIOS, UDP).It is not required that both the sender and receiver should be running on the same platform What is Asynchrony? Ans. With messag...

Advantages & Disadvantages of Synchronous / Asynchronous Communications?

  Asynchronous Communication Advantages: Requests need not be targeted to specific server. Service need not be available when request is made. No blocking, so resources could be freed.  Could use connectionless protocol Disadvantages: Response times are unpredictable. Error handling usually more complex.  Usually requires connection-oriented protocol.  Harder to design apps Synchronous Communication Advantages: Easy to program Outcome is known immediately  Error recovery easier (usually)  Better real-time response (usually) Disadvantages: Service must be up and ready. Requestor blocks, held resources are “tied up”.  Usually requires connection-oriented protocol