Results 1 to 5 of 5
  1. #1
    xt57 is offline Member
    Join Date
    Jun 2015
    Posts
    6
    Rep Power
    0

    Default Java Events Is there a direct Java analogue to Qt's signals/slots & C#'s delegate?

    I am a Java newbie, but actively learning the language. Does Java provide a simple syntax that allows the developer to define, subscribe to, raise/activate, then receive notification of events that are coordinated entirely within his/her use of native Java syntax? (As a newbie) I am studying (staring at) lambda and anonymous inner class examples, but I haven't, yet, come even close to understanding how to use them for general-purpose, event-driven logic in Java, if possible. My Qt/C++ and C# experience has enjoyed lots of success with Qt's signals/slots feature and C#'s delegate feature.

    If you know of any very simple examples of this type of general-purpose event support, within native Java syntax, please provide links, or code examples. I appreciate your time and consideration! Best wishes!

    Here is a simplistic example of my use of C#'s delegate feature for general application event processing :

    Java Code:
    //
    // C#, delegateExample.cs - simple event processing example
    //
    
    using System;
    using System.Threading;
    
    namespace csDelegateExample
    {
        public class	Widget
    					{	public string wSerialNum { get; set; }	}
    
        public class	WidgetEventArgs : EventArgs
    					{	public Widget Widget { get; set; }	}
    
        public class 	MachineShop
        {
    		//	define event and subscription mechanism
    		public event EventHandler<WidgetEventArgs> ehSubscribers;
            
    		// fabricate and test a new widget, then notify subscribers of completion
            public void StampAndTestNewWidget( Widget inWidget)
            {
                Console.WriteLine( "building widget ..." );		
                Thread.Sleep( 2500 );							// perform our task
                Console.WriteLine( "widget completed ..." );
                
    			// notify any event "subscribers" that this widget build has completed 
                if ( ehSubscribers == null )
    				Console.WriteLine( "no subscribers to notify ..." );
    			else
    			{
    				Console.WriteLine( "notifying subscriber(s) ..." );
    				ehSubscribers(this, new WidgetEventArgs() { Widget = inWidget } );
    			}
            }
        }
        
        public class 	InventoryDept
        {	public void OnWidgetCompletion(object source, WidgetEventArgs e)
            {	Console.WriteLine( "preparing for serial # : " + e.Widget.wSerialNum ); }
        }
    
        public class 	MainLogic
        {	static void Main( string[] args)
            {
    			var widget			= new Widget() 			{ wSerialNum = "7157" };
                var machineShop		= new MachineShop();	// event publisher
                var inventoryDept	= new InventoryDept();	// event subscriber;
                
    			//	register a "completion event" subscriber
                machineShop.ehSubscribers += inventoryDept.OnWidgetCompletion;
                
                machineShop.StampAndTestNewWidget( widget);	//	create a new widget
                Console.ReadKey();
                return;
            }
        }
    }

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Java Events Is there a direct Java analogue to Qt's signals/slots & C#'s delega

    Big themeblock, so just a few remarks from my end:

    1. Ignore lambdas for the time being. Fundamentally it is just a shortened version of the explicit coding.
    2. event programming in Java is very similar to c#: You have an object that is hooked up to an event (plus an interface plus the event definition). The hook-up:
    a. Defines which event you are listening to (mouse event, action event, key event, yada yada)
    b. Defines where the event is being handled
    3. There is a big repository of events already available in the different APIs, here Java is much easier to deal with than c#, since 95% of what will be used is already there.
    4. You can program your own events (not surprisingly), ' not sure where to send you for this since the info is available everywhere. There is a great tutorial in this forum. Here is a good wrap-up:
    http://stackoverflow.com/questions/6...-event-in-java


    Concerning 2b:
    You have 4 options where you can handle the event, all options implement the Listener interface:
    1. In the same class
    2. Inner class to 1.
    3. Anonymous inner class -> a littlebit of a specialty, start with 1. or 2. until you have understood the idea.
    4. External class -> for complex codes, gives a better overview. You have to pass the instance of the class to the constructor of the handler class.

    Beside finding piles of tutorials about events and eventhandling on the net it might be helpful to watch some videos on youtube.

    I hope, this didn't confuse you even more. Good Luck!
    Last edited by benji2505; 08-28-2015 at 04:40 AM. Reason: more info

  3. #3
    xt57 is offline Member
    Join Date
    Jun 2015
    Posts
    6
    Rep Power
    0

    Default Re: Java Events Is there a direct Java analogue to Qt's signals/slots & C#'s delega

    Hi benji2505,

    The topic's coverage within the stackoverflow answer you linked for me is EXACTLY the simple, yet complete, start I have been seeking!!! I am very grateful for the time you spent to help me! I will follow all of your advice, as I progress in my understanding.

    Thanks
    xt

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Java Events Is there a direct Java analogue to Qt's signals/slots & C#'s delega

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Java Events Is there a direct Java analogue to Qt's signals/slots & C#'s delega

    When handling events, don't forget about adapter classes. They can rid your code of extra clutter. For example, the various mouse interfaces have a total of about eight methods. By using an inner (or outer) class and extending the adapter of choice you only need to override the method of interest.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Direct file IO with Java ?
    By peller in forum Advanced Java
    Replies: 1
    Last Post: 12-19-2012, 04:21 PM
  2. does java have delegate like c#
    By trathanhcao in forum New To Java
    Replies: 5
    Last Post: 01-17-2012, 08:21 PM
  3. does java have delegate like c#
    By trathanhcao in forum AWT / Swing
    Replies: 2
    Last Post: 01-17-2012, 07:28 AM
  4. Signals and slots in Java?
    By arcelivez in forum New To Java
    Replies: 1
    Last Post: 04-13-2011, 12:46 PM
  5. Direct to URL to download java using JOptionPane
    By Basit in forum Java Applets
    Replies: 6
    Last Post: 08-21-2009, 08:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •