Next question is how the parameter "this" refers the ActionListener only? Why not your own program object ButtonDemo, or a Frame or ActionEvent?
You are right cent percent to think so. Then how to answer the above question. For the answer, we must refer the method signature of addActionListener() method as defined in Button class. Following is the one.
public synchronized void addActionListener(ActionListener);
Observe, the parameter must be an object of ActionListener. When we pass "this" to addActionListener() method, the JVM tries to convert "this" into an object of ActionListener because the method demands it. The JVM finds it, in the class declaration, after implements keyword and simply links btn to ActionListener. To have a proof, just remove "implements ActionListener" and compile the program, the program does not compile.
The whole process can be put diagrammatically as follows.
This type of event handling mechanism designed by the designers follows "Observer design pattern". The ActionListener observes always the button when it gets clicked. This type of event handling model is known as "delegation event model". In this model, responsibility of event handling is delegated (assigned) to listeners.
very good explanation
Hi sir how to execute the above program(ButtonDemo program) when i am executing it is giving this error “Exception in thread “main” java.lang.NoSuchMethodError: main”
sir,
i am a student of computer science…. this website is more helpful and useful for me. because tomorow i have seminar about event handling in my class..so only…thank you sir.
Easiest way of explanation..thanks alot sir ..sir if i start going through to your all the contents of java on this site..Is it enough to be a good java programmer ??
Yes, you will become an expert indeed.
Sir,How to write a program so that a new window is opened by pressing F2 key. I have tried but it is not working.I have used window and key events.
I have no idea.
You are a good teacher, I should say. Keep it up!
Nice tutorial sir. I’s wondering if you could recommend us some of your particular favourite books of java.
Thanks again for the tuto. I really appreciated it.
Now-a-days, Internet is the best source of knowledge. So, don’t on depend on books. If you purchase one book today, tomorrow it will be obsolete. For example, if you purchase to day a Java text book, next month September, Java 8 is getting released. Then you purchase another book also.
Tell your friends also to derive knowledge from this web site.
sir, i want complete discussion on event handling??
Yes, you can have.
what does add statement do in above program??
The add(btn); adds the button btn to the frame.
sir
i have inserted 3 labels in rectangle using paint method. my intention is particular label have to be moved within a rectangle when i drag it by using mouse in java
I am sending a file having some graphics programs to your email address.
Hi Sir,
Your way of teaching is very good. it would be better for us , if you upload some your video tutorials on Java.
Apache POI is missing in your website. Please provide some good understanding of java Apache POI . Thanks in advance.
BR//
Pishu
In the later half of April, I will place some videos in this web site. Advanced Java is not yet placed. Swing will be placed by April end.
sir i want to know that…. is it possible to have different actionlistener code for two or more buttons in a class.
Yes, why not. Refer this: Java AWT Button – Learning GUI – 8 Steps
sir plz settitle not used in applet which function work of set titel in applet
An applet works in a separate html page. In html file where you write the
Sir, How to show progress bar while downloading a file in Swing
See this code.
import java.awt.* ; import java.awt.event.* ; import javax.swing.* ;
public class ProgressiveTest {
public static void main ( String args[ ] ) {
JFrame frame = new JFrame( “Swing’s ProgressMonitor” ) ;
JButton button = new JButton( “Start Iteration” ) ;
frame.getContentPane().add( button, BorderLayout.CENTER );
int min = 0 ; int max = 100 ;
String[ ] message = new String[ 2 ] ;
message[ 0 ] = “Performing Iterations.” ;
message[ 1 ] = “Wait for completion…….” ;
final ProgressMonitor monitor = new ProgressMonitor(frame, message, “Iteration”,
min, max ) ;
final Runnable runnable = new Runnable( ) {
public void run( ) {
int sleepTime = 500 ;
for( int i = 1 ; i < 100 ; i++ ) { try { monitor.setNote( "Iteration " + i ) ; monitor.setProgress( i ) ; if ( monitor.isCanceled( ) ) { monitor.setProgress( 100 ) ; break ; } Thread.sleep( sleepTime ) ; } catch ( InterruptedException e ) { } } monitor.close( ) ; } } ; button.addActionListener( new ActionListener( ) { public void actionPerformed( ActionEvent event ) { // run the iterations in a separate thread Thread thread = new Thread( runnable ); thread .start( ) ; } } ) ;
Dear Sir
I’m doing project for school.
I have two sets of 4 buttonsa1a,2a,3a,4a and 1b,2b,3b,4b. I’m trying to write if script for condition: if 1a .clicked && 2b .clicked go to page2 in my site.
Any suggestions??
Thank you
I think you are using JavaScript. See the methods of window object.