Geekpedia Programming Tutorials






Basic Chatroom Skeleton Application

This Java code is not addressing the core of a chat application (sending and receiving messages) however it makes a good GUI skeleton for starting such an application.

On Sunday, August 3rd 2008 at 02:09 PM
By Andrew Pociu (View Profile)
-----   (Rated 0 with 0 votes)
Contextual Ads
More Java Resources
Advertisement
  1. import java.awt.BorderLayout;
  2. import javax.swing.JPanel;
  3. import javax.swing.JFrame;
  4. import javax.swing.JTextArea;
  5. import javax.swing.JTextField;
  6. import javax.swing.JButton;
  7. import java.awt.Dimension;
  8. import java.awt.event.*;
  9. import javax.swing.BoxLayout;
  10.  
  11. public class Chatroom extends JFrame {
  12.  
  13.         private static final long serialVersionUID = 1L;
  14.  
  15.         private JPanel jContentPane = null;
  16.  
  17.         private JTextArea jTextArea = null;
  18.  
  19.         private JPanel jPanel = null;
  20.  
  21.         private JTextField jTextField = null;
  22.  
  23.         private JButton jButton = null;
  24.  
  25.         /**
  26.          * This is the default constructor
  27.          */
  28.         public Chatroom() {
  29.                 super();
  30.                 initialize();
  31.         }
  32.  
  33.         /**
  34.          * This method initializes this
  35.          *
  36.          * @return void
  37.          */
  38.         private void initialize() {
  39.                 this.setSize(411, 331);
  40.                 this.setContentPane(getJContentPane());
  41.                 this.setTitle("Chatroom");
  42.         }
  43.  
  44.         /**
  45.          * This method initializes jContentPane
  46.          *
  47.          * @return javax.swing.JPanel
  48.          */
  49.         private JPanel getJContentPane() {
  50.                 if (jContentPane == null) {
  51.                         jContentPane = new JPanel();
  52.                         jContentPane.setLayout(new BorderLayout());
  53.                         jContentPane.add(getJTextArea(), BorderLayout.CENTER);
  54.                         jContentPane.add(getJPanel(), BorderLayout.SOUTH);
  55.                 }
  56.                 return jContentPane;
  57.         }
  58.  
  59.         /**
  60.          * This method initializes jTextArea   
  61.          *      
  62.          * @return javax.swing.JTextArea       
  63.          */
  64.         private JTextArea getJTextArea() {
  65.                 if (jTextArea == null) {
  66.                         jTextArea = new JTextArea();
  67.                         jTextArea.setEnabled(true);
  68.                         jTextArea.setEditable(false);
  69.                 }
  70.                 return jTextArea;
  71.         }
  72.  
  73.         /**
  74.          * This method initializes jPanel       
  75.          *      
  76.          * @return javax.swing.JPanel   
  77.          */
  78.         private JPanel getJPanel() {
  79.                 if (jPanel == null) {
  80.                         jPanel = new JPanel();
  81.                         jPanel.setLayout(new BoxLayout(getJPanel(), BoxLayout.X_AXIS));
  82.                         jPanel.setPreferredSize(new Dimension(0, 30));
  83.                         jPanel.add(getJTextField(), null);
  84.                         jPanel.add(getJButton(), null);
  85.                 }
  86.                 return jPanel;
  87.         }
  88.  
  89.         /**
  90.          * This method initializes jTextField   
  91.          *      
  92.          * @return javax.swing.JTextField       
  93.          */
  94.         private JTextField getJTextField() {
  95.                 if (jTextField == null) {
  96.                         jTextField = new JTextField();
  97.                         jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
  98.                                 public void keyPressed(java.awt.event.KeyEvent e) {
  99.                                         // If Enter was pressed
  100.                                         if(e.getKeyCode() == 10)
  101.                                         {
  102.                                                 addMessage();
  103.                                         }
  104.                                         setTitle("Chatroom - Typing a message...");
  105.                                         t.start();
  106.                                 }
  107.                         });
  108.                 }
  109.                 return jTextField;
  110.         }
  111.        
  112.         private void addMessage()
  113.         {
  114.                 jTextArea.append(jTextField.getText() + "\r\n"); // TODO Auto-generated Event stub mouseClicked()
  115.                 jTextField.setText("");
  116.                 setTitle("Chatroom");
  117.         }
  118.        
  119.         javax.swing.Timer t = new javax.swing.Timer(3000, new ActionListener() {
  120.         public void actionPerformed(ActionEvent e) {
  121.                 setTitle("Chatroom");  //  @jve:decl-index=0:
  122.         }
  123.      });
  124.  
  125.         /**
  126.          * This method initializes jButton     
  127.          *      
  128.          * @return javax.swing.JButton 
  129.          */
  130.         private JButton getJButton() {
  131.                 if (jButton == null) {
  132.                         jButton = new JButton();
  133.                         jButton.setPreferredSize(new Dimension(80, 50));
  134.                         jButton.setText("SEND");
  135.                         jButton.addMouseListener(new java.awt.event.MouseAdapter() {
  136.                                 public void mouseClicked(java.awt.event.MouseEvent e) {
  137.                                         addMessage();
  138.                                 }
  139.                         });
  140.                 }
  141.                 return jButton;
  142.         }
  143.  
  144. }  //  @jve:decl-index=0:visual-constraint="10,10"
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by lakshmi narahari on Wednesday, November 19th 2008 at 06:51 AM

please send me also all these code to my mail id

as
chunduru.narahari@gmail.com


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs Java Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons