Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  I need a little help with event listeners in Swing?  (Read 1955 times)
0 Members and 1 Guest are viewing this topic.
Offline DecafJava

Senior Newbie





« Posted 2007-11-17 04:41:13 »

I don't really have any code, it's just a concept I need some help with.  Let's say I have two textboxes, and if you hit enter in one it does one thing and if you hit enter in the other it does something else.  If there's only one listener for text boxes (correct me if I'm wrong) how do you make each box do something different? Do you have to test which one triggered the event somehow? Thanks.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2007-11-17 12:46:01 »

If they do the same thing to different objects, pass in parameters to the text listeners. If they do completely different things, then just make two different listener classes and attach them to the correct buttons.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline kaosnkorruption

Senior Newbie





« Reply #2 - Posted 2007-11-17 16:32:46 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
JTextField txt1 = new JTextField();
      txt1.addActionListener(
            new ActionListener()
            {
               public void actionPerformed(ActionEvent e)
               {
                  doSomething();
               }
            });

JTextField txt2 = new JTextField();
      txt2.addActionListener(
            new ActionListener()
            {
               public void actionPerformed(ActionEvent e)
               {
                  doSomethingElse();
               }
            });
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 55



« Reply #3 - Posted 2007-11-17 17:08:53 »

The Event object also has a getSource() method, so you could check for the different textboxes in your listener

Mathias - I Know What [you] Did Last Summer!
Offline DecafJava

Senior Newbie





« Reply #4 - Posted 2007-11-17 19:21:41 »

So would I use a bunch of if statements to check which textbox (for example) it was? What if I had a lot of textboxes?
Offline alexxz4

Junior Devvie


Projects: 1



« Reply #5 - Posted 2007-11-18 20:34:54 »

I think its easier to add a bunch of "else if" statements than to create a bunch of different listeners.
Correct me if im wrong, but each listener has its own thread and if u have a lot of listeners u have a lot of threads that each check whether stuff happened every step. So its better to have one listener and one thread...

[ Motherload Unlimited looking for programmers ] [ Solitaire ]
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2007-11-18 20:48:39 »

Correct me if im wrong, but each listener has its own thread and if u have a lot of listeners u have a lot of threads that each check whether stuff happened every step. So its better to have one listener and one thread...

You're wrong.

All listeners do is associate an object with a particular interface with a particular button. They don't trigger the creation of a new thread. A single AWT event thread checks for and triggers event callbacks for all event listeners, regardless of how many you have registered. Using a big if-else-if-else chain is ugly, fragile and prone to error.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline alexxz4

Junior Devvie


Projects: 1



« Reply #7 - Posted 2007-11-18 20:54:09 »

still, its more efficient to use "else if" statements. Cant argue with that.

[ Motherload Unlimited looking for programmers ] [ Solitaire ]
Offline Jackal von ÖRF

Junior Devvie





« Reply #8 - Posted 2007-11-18 21:48:49 »

still, its more efficient to use "else if" statements. Cant argue with that.
No.

If you have one listener instance which contains the code for N different buttons, by having N if-else blocks, its time complexity is O(N) (it needs to execute the if-clauses until it finds the right component). But if you have N listeners for the N buttons, the time complexity for executing an action is only O(1).

And first of all, the code is easier to maintain when there are no long if-else chains. It's a violation of the single responsibility principle to have one listener handle many different operations.

Offline broumbroum

Junior Devvie





« Reply #9 - Posted 2007-11-18 22:34:54 »

Listeners in Swing are somehow comparable to a mailbox. When Swing detects input, event, property change or other interactivity it forwards up the EventSource to its child components, known as EventListener's, which catch the Event and proceed it. If one of the eventListeners states that the event is to be consumed (as a reader would read its mail), then it returns to Swing Dispatcher and tells it has been consumed.
The Swing dispatcher is not to be implemented, but the Listener's must be implemented to tell HOW TO PROCEED with the event received by the Event dispatcher Thread (EDT).
Then it's up to the programmer to choose whether if-else or switch or other conditions must be held to state if the event can be consumed. AS A CAUTION, each EventListener MUST EXECUTE QUICKLY TO AVOID DEAD-LOCK of the EDT. THAT'S THE RULE PRIOR TO OTHER. ONES .
Therefore "Single responsibility principle" is something that could patternize your methods and Listener's. if-else are known to be quicker to execute than switch-case. Either way is ok, but to be honest, my own experience told me to focus the code structure on Threading and not on conditions. Let's say, try to load a 3D-model with one button.
1  
2  
3  
4  
5  
JButton loadModelButton = new JButton(new AbstractAction("load my model...") { public void actionPerformed(ActionEvent e) {
           // the model has its own implementation of loading its contents (pictures, sizes, transforms animations, and so on...)
          boolean wait = true; // I want to wait for loading to complete
          myModel.load(wait);
}});
 
This first example is such a typical mistake. Hence, my model will block the EDT until its contents are fully loaded and the method returns. One would say : "Uh ! this is because it cannot break my model STATE rule, otherwise the model will fail to display correctly. Or I have visual artefacts, ....".
That's thoroughly WRONG because Swing interface is BLOCKED (Thread.State.BLOCKED) by the model load() meth'.
Let's see the second implementation :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
JButton loadModelButton = new JButton(new AbstractAction("load my model...") { public void actionPerformed(ActionEvent e) {
           // the model has its own implementation of loading its contents (pictures, sizes, transforms animations, and so on...)
          Thread t = new Thread() { public void run() {
                   boolean wait = true; // I want to wait for loading to complete
                   myModel.load(wait);        
                   synchronized(modelMonitor) { modelMonitor.notifyAll(); }
          }});
          t.start();
}});
public void waitForLoading(Object monitor) throws InterruptedException{
        synchronized(monitor) {            
             while(true) {
                  System.out.print(".");
                  monitor.wait(10);
             }  System.out.println();
       }
}

This is correct, because Swing EDT IS FREED OF CHARGE after starting the model to load its contents. Another object can be used to monitor the model state while it is loading. No other solution can replace a better Threading with Swing Events Dispatcher. Wink

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DecafJava

Senior Newbie





« Reply #10 - Posted 2007-11-22 04:21:06 »

So would it be better to make a listener for every object or just one listener for every different type of object? (Say one for buttons, text boxes, etc.)
Offline cylab

JGO Ninja


Medals: 55



« Reply #11 - Posted 2007-11-22 08:53:56 »

It's up to you. If you use Netbeans for example to design a GUI, it generates listeners for every object, just containing a method call to a method of your container. If you have some generic functionality that only needs the pointer of the component (e.g. check all textboxes for valid content), you can create just one listener, that performs the task on all components it is attached to.

As pointed out above, as soon as you have huge if/elseif blocks in your listener, your design is probably wrong, but there is imo nothing to say against handling some special cases using an if here and there in an otherwise generic listener.

Mathias - I Know What [you] Did Last Summer!
Offline beowulf03809

Junior Devvie




We live for the code, we die for the code


« Reply #12 - Posted 2007-12-04 16:57:44 »

From my (limited) personal experience I would have to support the suggestion to place your action code immediately with the object as long as it's a relatively unique action.  I am working on my final for my second Java course and it's a program to manage students, teachers, classes, classrooms, grades, etc.  Very "light" in the details but still fairly complex in everything it's trying  to do.  I went the route of setting up four main tabs, one for each resource (student, faculty, etc.)  and there are several fields and buttons on each tab. 

My original start on this app had me using one central action block and if/else logic.  This is what most of the samples in various books and the Sun site do, but I have come to regard that "style" bitterly since they really only have a very limited amount of actions being performed in the sample (often just one or two).  As my number of actions grew to double digits and kept going this style started getting overwhelming to view and cumbersome to maintain.  I finally pulled out 90% of what was in that block and attached the functionality directly to the button objects, for example.  The only thing I'm keeping in that block now are actions that are triggered from multiple places (such as a menu item and buttons that run the same report). 

Not only has this proven much easier to view and create, but also having the action code directly with the object means it has been very easy to maintain and extend.  For example, during my mock-up process I just created the GUI and put a debug statement with each button so that I could click it and see "Student Save Button Clicked" show up in the log.  This let me verify quickly that every component was doing what I wanted it to do and responding to it's actions correctly.  I am now going thru all of those componets one at a time and extending them to perform the actual functionality. 
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.

rwatson462 (35 views)
2014-12-15 09:26:44

Mr.CodeIt (26 views)
2014-12-14 19:50:38

BurntPizza (59 views)
2014-12-09 22:41:13

BurntPizza (94 views)
2014-12-08 04:46:31

JscottyBieshaar (54 views)
2014-12-05 12:39:02

SHC (70 views)
2014-12-03 16:27:13

CopyableCougar4 (72 views)
2014-11-29 21:32:03

toopeicgaming1999 (132 views)
2014-11-26 15:22:04

toopeicgaming1999 (123 views)
2014-11-26 15:20:36

toopeicgaming1999 (34 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!