Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Event handling  (Read 1304 times)
0 Members and 1 Guest are viewing this topic.
Offline jobiho

Senior Newbie

« Posted 2013-02-21 19:26:51 »


I am actually not sure if this is the right place for my question but I probably will notice if not.
I come from an ActionScript background and am now dealing with Java to which I would like to port some graphical stuff I have made in "Flash" before.
The rendering is no problem for me at all but which makes me some headache is the way events are handled in Java because I have some swing gui that interacts with my model.

For instance given a custom GUI component (which extends JPanel) is composed of two other GUI components:JLabel and JButton.
If the user clicks on the JButton instance I would like to now the state of the JLabel (from a parent component which instanciated my custom component) without nowing much about the custom component (loose coupling).

So imho it would be the best solution to forward the new state(in this case it is the String of the label) as part of the custom event.

In ActionScript I couldy easily dispatch an custom event like this:
class MyCustomComponent extends Sprite {
  private const myButton : MyButton = new Button();

  public function MyCustomComponent() {
    myButton.addEventListener(MouseEvent.CLICK, onButtonClick);

  private function onButtonClick(event : MouseEvent) : void {
    dispatchEvent(new CustomEvent("change", "new string"));

Another GUI component could handle events of MyComponent like this:
var myCustomComponent : MyCustomComponent = new MyCustomComponent();
myCustomComponent.addEventListener("change", onCustomComponentChange);

In onCustomComponentChange I then could handle the changed state of the component.

So how is this solved "correctly" in Java?

Unfortunately it seems that I can not dispatch custom events that forward objects to the listener.
I thought about some objective c delegate solution:
myCustomComponent.delegate = myDelegate;

where the delegate needs to implement some component specific callbacks.
But this smells boilerplate.

Maybe I have missed something fundamental?

Thank you for your time, any help is appreciated Smiley
Offline xsvenson
« Reply #1 - Posted 2013-02-21 22:58:22 »

My mind is currently somewhere else but seems You'd like something like this ?
class MyComp implements SomeListener
   JLabel label = new JLabel("With text");
   JButton button = new JButton("Press me");
   /* some init method, constructor or whathave You */
   void init()

   // This from the action listener
   void someActionPerformed(SomeActionEvent e)
      if ( e.getObject() == button )
         //At this point You have access to the button and to the label

Note: this is more like a pseudo code than a real code, but the idea is valid.
There should be some tutorial around, that show the basic swing event handling.

Check, what "add*Listener" methods are available for the components You use. Then just implement the appropriate listener and use the events provided to the listener methods.

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Offline BoBear2681

JGO Coder

Medals: 19

« Reply #2 - Posted 2013-02-22 00:38:49 »

If you need custom events, and not built-in events as xsvenson speaks of, there are two available options.  You can choose which one is more appropriate for your situation.

1. Use java.beans.PropertyChangeEvents.  JComponents can fire such events by any of their many firePropertyChange() overloads.  Then the other component interested in these events would call customComponent.addPropertyChangeListener("propName", this);  This technique is common if you have say boolean, int or String properties that other components might be interested in receiving change notifications for.

2. The second option is more legwork, but allows you to notify listeners with an entire event object containing arbitrary data, instead of just an old/new value pair.  Roll your own Event class (subclassing java.util.EventObject), Listener interface (extending java.util.EventListener), and make your custom component accept Listeners of that type.  It follows the same pattern as the "built-in" event types like ActionListener, so it's pretty straightforward.  A simple example can be found here.  Note that JComponents already have a protected EventListenerList field named "listenerList", so you don't have to add such a field in your custom component yourself.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jobiho

Senior Newbie

« Reply #3 - Posted 2013-02-22 08:16:14 »

Thank you very much BoBear2681,
so basicly I need to implement the Observer Pattern by myself given the interface provided by Java.
While this is exactly what I have been looking for, I wonder why(if) there isn't any implementation but only the private field :/.
However, this helped me a lot Smiley
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

bilznatch (28 views)
2015-08-04 11:03:17

SHC (44 views)
2015-08-01 03:58:20

Jesse (25 views)
2015-07-29 04:35:27

Riven (48 views)
2015-07-27 16:38:00

Riven (26 views)
2015-07-27 15:35:20

Riven (28 views)
2015-07-27 12:26:13

Riven (19 views)
2015-07-27 12:23:39

BurntPizza (42 views)
2015-07-25 00:14:37

BurntPizza (56 views)
2015-07-24 22:06:39

BurntPizza (35 views)
2015-07-24 06:06:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!