Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
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  
  any interface for objects creating actionevents?  (Read 2042 times)
0 Members and 1 Guest are viewing this topic.
Offline zulo

Junior Member




Java games rock!


« Posted 2006-01-18 20:46:53 »

Hi

I have a class like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public class ButtonToSpriteLibraryAdaptor implements EventListener {

   SpriteLibraryModel model;

   Button controller;

   public ButtonToSpriteLibraryAdaptor(Button component, SpriteLibraryModel m) {
      model = m;
      controller = component;
     
   }

   public void actionPerformed(ActionEvent arg0) {
      model.addSprite();

   }

}

Now ofcourse because of the class name it looks like this class's specific function is supposed to happen
when a button is clicked. But what if I want to make it general? I want to be able to register any gui component or object and listen for events.
How can I do this? is there any interface i can implement to get a basic event from any gui component?

If i name the class ActionToSpriteLibraryNewSprite then maybe you understand what i want the class to do. Or maybe I just have to implement many different
listener interfaces to support different components?
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #1 - Posted 2006-01-19 01:21:19 »

Have a look at the java.awt.event.AWTEventListener interface.

Using the Toolkit method addAWTEventListener, you can listen to all events delivered to the entire AWT event dispatch system - with this information, you can discriminate upon exactly which events you wish to take action.
Offline zulo

Junior Member




Java games rock!


« Reply #2 - Posted 2006-01-19 17:11:33 »

hi
I tried it, the problem is that i want this class to get any kind of component then add itself as a listener..I dont want to make bind this class to a JButton..it should work with other components..So i want to be able to add any kind of component and then
listen to an event. Naturally it would be some kind of component creating action events but how do i add this class as an actionlistener to the component when i
dont know what component is coming into the constructor? If only there were some kind of superobject like "ActionEventCreatorObject"..

I guess i just have to create my own event and an interface that some subclass of a component or object could implement..


This is how i want to be able to do it, where the Object is some kind of object that throws an event:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public class NewSpriteActionToSpriteLibraryAdaptor implements ActionListener{

   SpriteLibraryModel model;

   Component controller;

   public ButtonToSpriteLibraryAdaptor(Component component, SpriteLibraryModel m) {
      model = m;
      controller = component;
      controller.addActionListener(this); //component doesnt have the addActionListener method, only buttons etc..
  }

   public void actionPerformed(ActionEvent event) {
     
         model.addSprite();
           
   }

}


implementing the AWTEventListener interface kind of works but then i would have to add some kind of eventlistener to the component in the gui just to make the event fire..a dummy-actionperformed method
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #3 - Posted 2006-01-27 14:35:30 »

Simply call enableEvents(long) on the components you want your AWTEventListener to hear events for.

You could wrap it into a method in your GeneralListener - e.g.GeneralListener.registerForAllEvents(Component comp)

You maybe able to pass in 0xFFFFFFFFFFFFL to enable all conceivable events.
However, that might be an illegal parameter - give it a test.
If the above doesn't work, just pass in an OR'd together set of all the Event type flags defined in java.awt.AWTEvent.

I should probably question why exactly you want to do this - the listener pattern used in AWT is a good design, I don't see what you hope to gain from replacing it with a centralized mechanism.
Offline zulo

Junior Member




Java games rock!


« Reply #4 - Posted 2006-02-02 15:18:54 »

I was trying to make the classes as general as possible, but probably i was too focused
on making it too general.

I have Adapter classes that basically does a specific function..It has a reference to the model, for example the SpriteLibraryModel which contains the sprites.
The NewSpriteActionToSpriteLibraryAdaptor class has the function of  calling mode.addSprite(). So the point of my adapter classes is to have each function in an adapter class, and then register the object that decides when to do that function..Like a JButton. This works nice, I just make the adapter implement the ActionListener interface then registers itself as the listener on the JButton that comes in the constructor. The problem is that i was thinking too generally..what if my editor doesnt have a button for the "addSprite" function?(maybe the addsprite function should be done when the user press a button) Then i would have to get an event from a general object..But only JButtons generate ActionEvents, and some other components i guess but the Component class itself does not.. So I could create my own event and eventlistener but then i would have to subclass the JButton...and i still wanted to be able to make the gui with eclipse designer,which doesnt work with customized components i think. Anyway, I was thinking too much, my brain was fixed on creating the ultimate general library. Its strange that i was trying to do this when i cant come up on any idea of how it would be useful to use anything else then components..but still i wanted to try.  Roll Eyes

I still have the good design of model-adapter-view, so I can change the gui without breaking the real code and thats good enough for me Smiley
Offline Mr_Light

Senior Member




shiny.


« Reply #5 - Posted 2006-02-02 16:23:07 »

ever looked at the action framework?

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/Action.html


new FooComponent(Action a)

use it for menu items, toolbar buttons, normal buttons, clickable navigation trees etc.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline noblemaster

JGO Ninja


Medals: 19
Projects: 9


Age of Conquest makes your day!


« Reply #6 - Posted 2006-02-02 20:59:38 »

What are you trying to achieve?

"model-adapter-view"? Do you mean model-view-controller (MVC)? That's not how MVC works.

Offline zulo

Junior Member




Java games rock!


« Reply #7 - Posted 2006-02-03 12:12:15 »

To move the real code away from any gui is what im trying to achieve.. I know it didnt end up on mvc, it became a freak-designpattern with nothing better
then mvc..

Theres so many different descriptions of mvc. I read a tutorial on mvc in java and they used a Model class and an adapter class..the adapter class transfers the actions from the gui to the model so that the model doesnt know about the gui. and the adapter class listens to changed events in the model and updates the gui. But could I just make the gui itself listen to the changed events? Should the model only use 1 event when something has changed in the model or could i make my own special events?

My adapter classes does the conversion of the data for the gui. For example, the adapter gets the list of sprites from the model when the model has changed. Then it walks through the collection of sprites and generates a String list of the sprites names and updates the gui / JList with the new list of strings.. I know I could also just send the list of sprite objects directly to the JList and it would show the names since the sprites have theyr own toString() method. But then there would be no reason for the adapter class. What should I do here?
Offline noblemaster

JGO Ninja


Medals: 19
Projects: 9


Age of Conquest makes your day!


« Reply #8 - Posted 2006-02-03 12:34:26 »

Stick to MVC! Yes, the model does NOT know about the GUI. That's how you do it:

Model class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class SomeModel {
  // view classes add themselves as listener to model changes through these methods.
  private List listeners;
  addListener(Listener listener) {...}
  removeListener(Listener listener) {...}

  // these are the actual model functions. e.g.
  public int getNumSoldiers() {...}
  public void setNumSoldiers(int soldiers) {
    this.soldiers = soldiers;
    // now, send the change to ALL listeners
   sendChangeToAllListeners(SOLDIER_CHANGE);
  }
}


view class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public class SomeView implements Listener {
  public SomeView(SomeModel model) {
    // add myself as listener to model
    model.addListener(this);
  }
  // gets called, when the model changes
  public void update(Change change) {
    if (change == SOLDIER_CHANGE) {
      ...update the view
    }
  }
}


So basically, the model allows listeners to add themselves. Listeners listen for changes. The view classes have an instance of model and also add themselves as listener.

So, whenever the model changes, the model tells all the listener (i.e. views) about the change and the view then updates the display based on the change that occured.

Does this make sense? Sorry, my example is little bit short? However, I use that approach for my games and it really works great!

Offline zulo

Junior Member




Java games rock!


« Reply #9 - Posted 2006-02-03 15:40:05 »

Yes i understand how that works Smiley

I thought that MVC you had  3 parts.. model view and a controller..but where is your controller in your example?inside the view?

SOLDIER_CHANGE is this a public static final Change object in the model?

So I can have different changes hmm interesting Smiley

here is an image of my model:


I thought the adapter was just another name for Controller..

I guess i could just let the view do the same as the adapter.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zulo

Junior Member




Java games rock!


« Reply #10 - Posted 2006-02-03 15:54:42 »

http://www.enode.com/x/markup/tutorial/mvc.html
A description of mvc.

 I think the only difference between that one and my model is that the model updates the view, the actionlistener/controller (adapter in my design) updates the model.

And in my design the adapter updates the view and the adapter also acts as the controller. So if I let the view listen to the models change event instead of the adapter it should be a correct MVC i think ?. And the adapter is just the external controller/actionlistener listening for commands from the view then it updates the model.
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.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (11 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (108 views)
2014-04-08 23:06:04

tom_mai78101 (208 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (183 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!