Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 27th, 2004, 02:59 PM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with Event handling

I need some help clearing up the following code:

// Handles element type menu items
class TypeListener implements ActionListener
{
private int type;

TypeListener(int type)
{this.type=type;}

// sets the element type
public void actionPerformed(ActionEvent e)
{elementType = type;}

// add listener
lineItem.addActionListener(new TypeListener(LINE));

I am confused about what an event listener does. When the JButtonItem lineItem is clicked i know an event object is created. But is this object combined with the Listener object and then that object is what is passed to the actionPerformed method.

I cant see how when the event is passed to the actionPerformed method it has somehow gotten the member type added to it. How exactly is this working??


also in the code:

lineItem.addActionListener(new TypeListener(LINE));

how exactly does the (new TypeListener(LINE)) part of the code work. I would normally write the following:

TypeListener aVariable = new TypeListener;
lineItem.addActionListener(aVariable(LINE));

here i have obviously created a variable to hold a reference to the new TypeListener object. but when it is written as:

lineItem.addActionListener(new TypeListener(LINE));

Where is the new TypeListener object being stored???

thanks for the help
Reply With Quote
  #2 (permalink)  
Old October 11th, 2004, 03:55 PM
Registered User
 
Join Date: Aug 2003
Location: Terre Rouge, MU, Mauritius.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I don't know why you are doing it that way? Event Listeners can be
added to the components like this <abutton.addActionListener(this)>. In a complete program it can simply look like the program below.

Look at AddHandlers method and then go to actionPerformed to have an idea how it is done. These two examples should help you correct your code. Remember Event handlers have to be added to the components before the latter are added to the containers.

//Program ID Callers.
//Author Judex for CIS 210 Assignment 2 of 2003
//6 Feb 2003
//# This program simulates a Telephone Exchange It is done through a pad of
// 8 button that symbolizes a phone . A click on one these butttons starts
// the process of connection request and reply.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;

//# compo is a user defined interface that holds the global variable of this
//class

public class Callers extends JFrame implements ActionListener,compo,
                                               MouseListener {

                             int capacity, numberphoned;
                             charges newcharge ;

public Callers(){
super("Telephone Exchange Simulation");
initComponents();
AddHandlers();
CreateVisualDisplay();
setContentPane(phonepad);
}



//# This method actually ensures (in a bid for simplicity) that all users are
// eligible
public void initComponents(){

capacity=0;
for(int i=0; i<=7; i++){eligible[i]=true;
                        status[i]=1; }

}

public void AddHandlers(){
Phone1.addActionListener(this);
Phone2.addActionListener(this);
Phone3.addActionListener(this);
Phone4.addActionListener(this);
Phone5.addActionListener(this);
Phone6.addActionListener(this);
Phone7.addActionListener(this);
Phone8.addActionListener(this);

Phone1.addMouseListener(this);
Phone2.addMouseListener(this);
Phone3.addMouseListener(this);
Phone4.addMouseListener(this);
Phone5.addMouseListener(this);
Phone6.addMouseListener(this);
Phone7.addMouseListener(this);
Phone8.addMouseListener(this);

}


//creation of the pad used to symbolize the phones
public void CreateVisualDisplay(){

GridLayout grid = new GridLayout(4,4,10,20);
phonepad.setLayout(grid);

phonepad.add(Phone1);
phonepad.add(label1);
phonepad.add(label2);
phonepad.add(Phone2);
phonepad.add(Phone3);
phonepad.add(label3);
phonepad.add(label4);
phonepad.add(Phone4);
phonepad.add(Phone5);
phonepad.add(label5);
phonepad.add(label6);
phonepad.add(Phone6);
phonepad.add(Phone7);
phonepad.add(label7);
phonepad.add(label8);
phonepad.add(Phone8);
}

public static void main(String[] args){
        JFrame frame = new Callers();

        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };
        frame.addWindowListener(l);

        frame.pack();
        frame.setVisible(true);

    }

//#actionPerformed method is to check out which phone is being clicked
public void actionPerformed(ActionEvent evt){ Object source = evt.getSource();

       if (source == Phone1) { EvaluateAction(2100);}
       if (source == Phone2) { EvaluateAction(2101);}
       if (source == Phone3) { EvaluateAction(2102);}
       if (source == Phone4) { EvaluateAction(2103);}
       if (source == Phone5) { EvaluateAction(2104);}
       if (source == Phone6) { EvaluateAction(2105);}
       if (source == Phone7) { EvaluateAction(2106);}
       if (source == Phone8) { EvaluateAction(2107);}

  }
Hope to have help you,

Judex

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Event Handling Apocolypse2005 Visual Basic 2005 Basics 1 March 10th, 2009 07:14 AM
Event Handling C# 2.0 AccessAasif@aol.com C# 2005 2 January 25th, 2008 11:37 AM
Event handling in c# bhavna VS.NET 2002/2003 1 March 7th, 2007 12:19 PM
Event handling in c# bhavna General .NET 1 March 7th, 2007 05:32 AM
Event Handling Bill Crawley ASP.NET 2.0 Basics 0 February 28th, 2007 07:32 AM



All times are GMT -4. The time now is 08:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.