Tuesday, November 30, 2010

Java swing event handling example programs for beginners

Java uses delegation event model approach to handle events.

What is delegation event model?
Concept is quite simple:a source generate an event and sends it to one or ore listeners.In this scheme, the listener waits until it receive an event. Once an event is received, the listener process the event and and then returns.

Here are short definitions of event, source, listeners.

Event :
An event is an object that describes a state change in a source. It can be generated as a consequence of a person interacting with the elements in a graphical user interface.


Event Source:
A source is an object that generates an event. This occurs when the internal state of that object changes ijn some way. Sources may generate more than one type of event.

Event Listener:
A listener is an object that is notified when an event occurs. It has 2 major requirements.
  
 1. Listener must have been registered with one or more source.
 2. It must implement methods to receive and process these notifications.

To implement Event in a program you need to import awt.event package.
Now lets see a sample program.



import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class SwingEvent{
JButton ja,jb;

JLabel jlab;
public void display(){
JFrame jfrm=new JFrame("Swing application with event handlers");

//set size
jfrm.setSize(500,500);

//wen window is closed?
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//by default layout is border set to flow layout
jfrm.setLayout(new FlowLayout());

//set visible frame
jfrm.setVisible(true);

//display a label
jlab=new JLabel("Event driven swing");
jfrm.add(jlab);
ja=new JButton("alpha");
jb=new JButton("beta");

//add action listener for alpha
ja.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
jlab.setText("YOU PRESSED ALPHA");
}
});

//add action listener for beta
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
jlab.setText("YOU PRESSED BETA");
}
});

JLabel jlab2=new JLabel("press any button");
jfrm.add(jlab2);

//add to frame pane
jfrm.add(ja);
jfrm.add(jb);
}

//event dispacthing thread
public static void main(String k[]){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
SwingEvent sw=new SwingEvent();
sw.display();
}
});
}
}

Explanation:
If you are new to swing then, see how to start a swing.
First import packages for event handling.
Initialize two buttons using, JButton().
And then add action listener to the button. Using addActionListener(), this methods takes ActionListener.
And actionperfomed(), it takes ActionEvent ae as a parameter.

ja.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
jlab.setText("YOU PRESSED ALPHA");
}
});

Make a note that semi colon is given at the end.

Jlab.setText(), is used to display message when you press ja button.
And everything is simple and self explanatory.

See the output screen:
Click on image
 Other posts:
 Java swings examples.
 Java packages example tutorials.
Java programming in Eclipse IDE.
Start learn java from beginning.

6 comments:

  1. The blog was absolutely fantastic! Lots of great information and
    inspiration, both of which we all need!b Keep 'em coming... you all do
    such a great job at such Concepts... can't tell you how much I, for
    one appreciate all you do!

    ReplyDelete
  2. I like your blog! I check it once in a while and always find interesting reading.

    ReplyDelete
  3. Hello Frndz....
    Your blog has always been a good source for me to get quality tips on blogging. Thanks once again.

    Swing handle

    ReplyDelete
  4. Hello Frndz....
    Nice Blog...
    Your blog has always been a good source for me to get quality tips on blogging. Thanks once again.

    Swing handle

    ReplyDelete