Trace:
Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
jvx:reference [2020/06/24 15:32] cduncan [The advanced usage: Anchor Configuration] |
jvx:reference [2020/06/24 15:43] cduncan [Parameters or no parameters? To throw or not to throw?] |
||
---|---|---|---|
Line 1749: | Line 1749: | ||
===== Conclusion ===== | ===== Conclusion ===== | ||
- | The [[https://sourceforge.net/projects/jvx/|JVx]] FormLayout allows to quickly and easily create complex, good looking and working layouts which are still flexible enough for the cases when a component is swapped, removed or added. It can be used in many different circumstances and is still easy enough to use to make sure that even beginners are able to create a basic layout within seconds. | + | The [[https://sourceforge.net/projects/jvx/|JVx]] FormLayout allows us to quickly and easily create complex, good looking, and functioning layouts that are still flexible enough for the cases when a component is swapped, removed, or added. It can be used in many different circumstances and is still easy enough to use to make sure that even beginners are able to create a basic layout within seconds. |
====== Events ====== | ====== Events ====== | ||
Line 1755: | Line 1755: | ||
Let’s talk about events and event handling in [[https://sourceforge.net/projects/jvx/|JVx]]. | Let’s talk about events and event handling in [[https://sourceforge.net/projects/jvx/|JVx]]. | ||
- | ===== What are events… ===== | + | ===== What Are Events… ===== |
- | Events are an [[https://en.wikipedia.org/wiki/Event-driven_programming|important mechanism no matter to what programming language or framework you turn to]]. It allows us to react on certain actions and “defer” actions until something triggered them. Such triggers can be anything, like a certain condition is hit in another thread, the user clicked a button or another action has finally finished. Long story short, you get notified that something happened, and that you can now do something. | + | Events are an [[https://en.wikipedia.org/wiki/Event-driven_programming|important mechanism no matter to what programming language or framework you turn to]]. They allow us to react on certain actions and “defer” actions until something triggers them. Such triggers can be anything: a certain condition is hit in another thread, the user clicked a button, or another action has finally finished. Long story short, you are notified that something happened and you can now do something further. |
- | ===== …and why do I need to handle them? ===== | + | ===== …And Why Do I Need to Handle Them? ===== |
- | Well, you can’t skip events, they are a cornerstone of [[https://sourceforge.net/projects/jvx/|JVx]]. Theoretically, you could use [[https://sourceforge.net/projects/jvx/|JVx]] without using any of its events, but you would not only miss out on a lot of functionality but also be unable to do anything useful. But don’t worry, understanding the event system is easy, using it even easier. | + | Well, you can’t skip events, they are a cornerstone of [[https://sourceforge.net/projects/jvx/|JVx]]. Theoretically, you could use [[https://sourceforge.net/projects/jvx/|JVx]] without any of its events, but you would not only miss out on a lot of functionality, but also be unable to do anything useful. But don’t worry, understanding the event system is easy and using it even easier. |
===== Terminology ===== | ===== Terminology ===== | ||
- | For [[https://sourceforge.net/projects/jvx/|JVx]] the following terminology applies: An event is a property of an object, you can register listeners on that event which will get invoked if the event is dispatched (fired). Every event consists of the ''%%EventHandler%%'' which allows to register, remove and manage the listeners and also dispatches the events, meaning invoking the listeners and notifying them that the event occurred. There is no single underlying listener interface. | + | For [[https://sourceforge.net/projects/jvx/|JVx]] the following terminology applies: an event is a property of an object. You can register listeners on that event that will get invoked if the event is dispatched (fired). Every event consists of the ''%%EventHandler%%'', which allows ypi to register, remove, and manage the listeners and also dispatches the events, meaning it invokes the listeners and notifies them that the event occurred. There is no single underlying listener interface. |
- | Within the [[https://sourceforge.net/projects/jvx/|JVx]] framework, every event-property of an object does start with the prefix “event” to make it easily searchable and identifiable. But enough dry talk, let’s get started. | + | Within the [[https://sourceforge.net/projects/jvx/|JVx]] framework, every event property of an object starts with the prefix “event” to make it easily searchable and identifiable. But enough dry talk, let’s get started. |
- | ===== Attaching listeners ===== | + | ===== Attaching Listeners ===== |
We will now look at all the ways on how to attach a listener to an event. | We will now look at all the ways on how to attach a listener to an event. | ||
Line 1775: | Line 1775: | ||
==== Class ==== | ==== Class ==== | ||
- | The easiest way to get notified of events is to attach a class (which is implementing the listener interface) to an event as listener, like this: | + | The easiest way to get notified of events is to attach a class (that is implementing the listener interface) to an event as listener, like this: |
<code java> | <code java> | ||
Line 1800: | Line 1800: | ||
} | } | ||
</code> | </code> | ||
- | ==== Inlined class ==== | + | ==== Inlined Class ==== |
- | Of course we can inline this listener class: | + | Of course, we can inline this listener class: |
<code java> | <code java> | ||
Line 1825: | Line 1825: | ||
} | } | ||
</code> | </code> | ||
- | ==== JVx style ==== | + | ==== JVx Style ==== |
- | So far, so normal. But in [[https://sourceforge.net/projects/jvx/|JVx]] we have support to attach listeners based on reflection, like this: | + | So far, so good. However, in [[https://sourceforge.net/projects/jvx/|JVx]] we have support to attach listeners based on reflection, like this: |
<code java> | <code java> | ||
Line 1849: | Line 1849: | ||
} | } | ||
</code> | </code> | ||
- | What is happening here is that, internally, a listener is created which references the given object and the named method. This allows to easily add and remove listeners from events and keeping the classes clean by allowing to have all related event listeners in one place and without additional class definitions. | + | What is happening here is that, internally, a listener is created that references the given object and the named method. This allows us to easily add and remove listeners from events and keeps the classes clean by allowing us to have all related event listeners in one place without additional class definitions. |
==== Lambdas ==== | ==== Lambdas ==== | ||
- | Yet there is more, we can of course attach [[https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html|lambdas]] to the events as listeners, too: | + | Yet there is more. We can, of course, attach [[https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html|lambdas]] to the events as listeners too: |
<code java> | <code java> | ||
Line 1870: | Line 1870: | ||
} | } | ||
</code> | </code> | ||
- | ==== Method references ==== | + | ==== Method References ==== |
- | And last but not least, thanks to the new capabilities of Java 1.8, we can also use [[https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html|method references]]: | + | Lastly, thanks to the new capabilities of Java 1.8, we can also use [[https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html|method references]]: |
<code java> | <code java> | ||
Line 1894: | Line 1894: | ||
} | } | ||
</code> | </code> | ||
- | ===== Parameters or no parameters? To throw or not to throw? ===== | + | ===== Parameters or No Parameters? To Throw or Not to Throw? ===== |
- | By default we actually support two different classes of listeners, the specified event/listener interface itself, and ''%%javax.rad.util.IRunnable%%''. Which means that you can also attach methods which do not have any parameters, like this: | + | By default we actually support two different classes of listeners, the specified event/listener interface itself and ''%%javax.rad.util.IRunnable%%''. This means you can also attach methods that do not have any parameters, like this: |
<code java> | <code java> | ||
Line 1924: | Line 1924: | ||
} | } | ||
</code> | </code> | ||
- | Additionally, all listeners and ''%%IRunnable%%'' itself do support to throw ''%%Throwable%%'', which is then handled inside the ''%%EventHandler%%''. So you are very flexible when it comes to what methods you can attach and use as listeners. | + | Additionally, all listeners and ''%%IRunnable%%'' itself support throwing ''%%Throwable%%'', which is then handled inside the ''%%EventHandler%%''. As you can see, you are very flexible when it comes to what methods you can attach and use as listeners. |
===== Creating your own events ===== | ===== Creating your own events ===== |