Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (591)
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 1056 times)
0 Members and 1 Guest are viewing this topic.
Offline jobiho

Senior Newbie





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

Hi,

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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:
1  
2  
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:
1  
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 ?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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()
   {
      button.addListener(this);
   }

   // 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
         System.out.println(label.getText());
      }
   }
}

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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Gibbo3771 (7 views)
2014-11-24 19:59:16

trollwarrior1 (35 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!