Thursday, September 27, 2007

Adding Java Listeners

In my class, we are now doing GUI using Abstract Window Toolkit (AWT). In one of our lab exercises, we mimicked a messenger window where the user types in the message and by pressing the "enter" key or by clicking the SEND button, the message is transfered to the display area.

To add listeners, for the buttons SEND and QUIT I used the ActionListeners and for the "ENTER"key, I used the KeyListener and added it to the TextArea object msg.


Here's the resulting window for the given code:

/**
* @(#)MyMessenger.java
*
*
* @Erlyn Q. Manguilimotan
* Computer Science Department

* Ateneo de Zamboanga University
* @version 1.00 2007/9/26
*/

import java.awt.*;
import java.awt.event.*;

public class MyMessenger extends Frame implements ActionListener, KeyListener{

private TextArea disp; // object for the display area
private TextArea msg;
// object for the message area

public MyMessenger(){

Panel dispPanel = new Panel();
//panel where the TextArea object disp is to be placed

Panel msgPanel = new Panel();
//panel where the TextArea object msg is to be placed

Panel buttonsPanel = new Panel();
//panel where the buttons send and quit
are to be placed
Button send = new Button("SEND");//button for SEND
Button quit = new Button("QUIT");//button for QUIT

disp = new TextArea("",8,30,TextArea.SCROLLBARS_VERTICAL_ONLY);
//textarea for disp with 8 rows and 30 columns

msg = new TextArea("",8,30,TextArea.SCROLLBARS_VERTICAL_ONLY);
//textarea for disp with 8 rows and 30 columns

disp.setBackground(Color.PINK); //set background of display area to pink
disp.set
Editable(false); //set the display area to not editable mode

setSize(300,350); //set framesize
setLayout(new BorderLayout());//set Frame layout
setBackground(Color.lightGray); //set frame background color
setTitle("My Messenger"); //set Frame title

add(dispPanel,BorderLayout.NORTH); //add panels to the frame
add(msgPanel
,BorderLayout.CENTER);
add(buttonsPanel,BorderLayout.SOUTH);

dispPanel.add(disp); //add d
isp TextArea to dispPanel
msgPanel.add(msg);//add msg TextArea to msgpPanel
msg.addKeyListener(this);//add key listener to msg TextArea
buttonsPanel.add(send); //add buttons to panel
buttonsPanel.add(quit);
send.addActionListener(this); // add ActionListener to button when clicked
quit.addActionListener(this);

addWindowListener(new FrameListener()); //adding window Listener
setResizable(false);
setVisible(true);

}

public static void main(String args[])
{
MyMessenger ym = new MyMessenger(); // instantiating MyMessenger Class
}


public void actionPerformed (ActionEvent e)
{
if(e.getActionComm
and()=="SEND") //if SEND button is clicked
{ String temp=""; //temp string
temp=msg.getText(); //recieves the text from msg TextArea
disp.append(temp); //transfers by append method to display area
msg.setText(""); //sets message area to empty again
}
else
System.exit(0);

}

public void keyPressed (KeyEvent e){


if(e.getKeyCode()== e.VK_ENTER) //if Enter key is pressed
{ String temp=""; //temp string
temp=msg.getText(); //recieves the text from msg TextArea
disp.append(temp); //transfers by append method to display area
msg.setText(""); //sets message area to empty again
}

}


public void keyReleased(KeyEvent e){
}

public void keyTyped (KeyEvent e){
}

}



To close the window, I used a class called FrameListener.java which extends WindowAdapter. Here's the code:

import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

class FrameListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

You may try this code. This still needs some improvement or polishing. You may suggest some codes on how to improve the GUI.

Thank you...

2 comments:

J.Hanrahan said...
This comment has been removed by the author.
J.Hanrahan said...

his doesn't work for me. The return type of the first method isn't defined. Otherwise, thanks for teaching me listeners!