Creating the Chat Room Interface


The ChatRoom.java file creates the user interface that allows end users to enter the chat room by specifying the name of the chat room for the Connector application. Listing 2-6 shows the contents of the ChatRoom.java file:

Listing 2-6: The ChatRoom.java File

start example
 /*Imports required swing classes*/ import javax.swing.*; import javax.swing.event.*; /*Imports required awt classes*/ import java.awt.*; import java.awt.event.*; /* class ChatRoom-This class is used to create a GUI to enter in a existing room Constructor: ChatRoom-This constructor creates GUI. Methods: */ class ChatRoom extends JFrame implements ActionListener {    /*Declares objects of JLabel class.*/    JLabel roomnamelabel;    JLabel pageheadelabel;    /*Declares object of JTextField class.*/    JTextField roomnametext;    /* Declares objects of JPanel class.*/    JPanel chatroompanel;    JPanel buttonpanel;    /* Declares objects of JButton class.*/    JButton okbutton;    JButton cancelbutton;    /* Declares object of String class.*/    String roomnamestring;    /* Declares object of JabberClient class.*/    JabberClient jabberclient;    public ChatRoom(JabberClient jabberclient)    {       super("Enter Chat Room");       Container contentpane=getContentPane();       this.jabberclient=jabberclient;       /* Initializes object of JLabel class.*/             pageheadelabel=new JLabel("Enter Chat Room", JLabel.CENTER);       /*        Sets the font to the object of JLabel  class.        */       Pageheadelabel.setFont(new Font("Verdana", Font.BOLD, 12));       /* Adds the label to the contentpane.*/             contentpane.add(pageheadelabel, BorderLayout.NORTH);       /*       Initializes the object of JLabel  class.*/       roomnamelabel=new JLabel("Room Name");       /*       Initializes the object of JTextField class.        */       roomnametext=new JTextField();       /*       Initializes the objects of JPanel class.*/       chatroompanel=new JPanel();       buttonpanel=new JPanel();       /*       Sets the size of the objects of JLabel class.        */       roomnamelabel.setPreferredSize(new Dimension(120, 23));       roomnametext.setPreferredSize(new Dimension(170, 23));       /*        Declare and initialize the object of FlowLayout class.       */       FlowLayout chatroomflowlayout=new FlowLayout();       /*        Set the FlowLayout to chatroompanel object.        */       chatroompanel.setLayout(chatroomflowlayout);       /*Adds the label to the panel.*/       chatroompanel.add(roomnamelabel);       /*Adds the roomnametext to the panel.*/       chatroompanel.add(roomnametext);       contentpane.add(chatroompanel);       /*       Initializes the objects of JButton class.*/       okbutton=new JButton("OK");       cancelbutton=new JButton("Cancel");       /*       Adds the okbutton to the buttonpanel.       */       buttonpanel.add(okbutton);       /* Sets the size of the buttons.*/       okbutton.setPreferredSize(new Dimension(80, 23));       cancelbutton.setPreferredSize(new Dimension(80, 23));       /*       Adds the ActionListener to the buttons.       */       okbutton.addActionListener(this);       cancelbutton.addActionListener(this);       okbutton.setActionCommand("ok");       cancelbutton.setActionCommand("cancel");       buttonpanel.add(cancelbutton);       contentpane.add(buttonpanel,BorderLayout.SOUTH);       setSize(320,140);       setVisible(true);    }    public void actionPerformed(ActionEvent ae)    {       String actioncommand=ae.getActionCommand();       String roomquerystring="";       if (actioncommand.equals("ok"))       {          roomnamestring=roomnametext.getText().trim();          if (roomnamestring.equals(""))          {             JOptionPane.showMessageDialog(null,"Room name is a required             field.","Error",JOptionPane.PLAIN_MESSAGE);             return;          }          roomquerystring="<iq from='"+jabberclient.getUserName()+"@"+jabberclient.          getServerName()+"/Home' id='1' to='"+roomnamestring+"@conference."+jabberclient.          getServerName()+"' type='get'>";          roomquerystring=roomquerystring+"<query          xmlns='http://jabber.org/protocol/disco#info'/></iq>";          jabberclient.setMessageInToTextBox(roomquerystring);          jabberclient.setRoomName(roomnamestring);       }       setVisible(false);    } } 
end example

Download this listing.

In the above code, the constructor of the ChatRoom.java class takes an object of the JabberClient class as an input parameter to invoke the methods of the JabberClient class. The ChatRoom class allows you to create the Enter Chat Room window to enter the name of the chat room in the Connector application.

The methods defined in Listing 2-6 are:

  • actionPerformed(): Acts as an event listener and activates an appropriate method based on the action the end user performs.

  • getText(): Requests the end users to specify the name of the chat room.

Select File-> Enter Chat Room to enter the chat room. The ChatRoom.java generates the Enter Chat Room window, as shown in Figure 2-9:

this figure shows the enter chat room window that allows end users to enter the name of a chat room.
Figure 2-9: The Enter Chat Room Window




Developing Applications Using Jabber
Developing Applications Using Jabber
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 68

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net