Advertisement
Guest User

ClientGUI.java

a guest
Dec 23rd, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.48 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package chatgui4;
  6.  
  7. import java.awt.EventQueue;
  8. import java.io.*;
  9. import java.net.Socket;
  10. import java.net.UnknownHostException;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.SwingUtilities;
  13.  
  14. /**
  15.  *
  16.  * @author sulav
  17.  */
  18. public class ClientGUI extends javax.swing.JFrame implements Runnable {
  19.  
  20.     /**
  21.      * Creates new form ClientGUI
  22.      */
  23.     private static Socket clientSocket = null;
  24.     // The output stream
  25.     private static PrintStream os = null;
  26.     // The input stream
  27.     private static DataInputStream is = null;
  28.     private static BufferedReader inputLine = null;
  29.     private static boolean closed = false;
  30.     public static String uname;
  31.  
  32.     public ClientGUI() {
  33.         initComponents();
  34.     }
  35.  
  36.     /**
  37.      * This method is called from within the constructor to initialize the form.
  38.      * WARNING: Do NOT modify this code. The content of this method is always
  39.      * regenerated by the Form Editor.
  40.      */
  41.     @SuppressWarnings("unchecked")
  42.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  43.     private void initComponents() {
  44.  
  45.         jScrollPane1 = new javax.swing.JScrollPane();
  46.         jTextArea1 = new javax.swing.JTextArea();
  47.         jLabel1 = new javax.swing.JLabel();
  48.         jLabel2 = new javax.swing.JLabel();
  49.         jButton1 = new javax.swing.JButton();
  50.         jButton2 = new javax.swing.JButton();
  51.         jTextField1 = new javax.swing.JTextField();
  52.         jButton3 = new javax.swing.JButton();
  53.         jScrollPane3 = new javax.swing.JScrollPane();
  54.         jList2 = new javax.swing.JList();
  55.         jLabel3 = new javax.swing.JLabel();
  56.         jMenuBar1 = new javax.swing.JMenuBar();
  57.         jMenu1 = new javax.swing.JMenu();
  58.         jMenu2 = new javax.swing.JMenu();
  59.  
  60.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  61.         setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  62.  
  63.         jTextArea1.setColumns(20);
  64.         jTextArea1.setEditable(false);
  65.         jTextArea1.setRows(5);
  66.         jScrollPane1.setViewportView(jTextArea1);
  67.  
  68.         jLabel1.setText("Online Users");
  69.  
  70.         jLabel2.setText("Terminal");
  71.  
  72.         jButton1.setText("Connect");
  73.         jButton1.setMargin(new java.awt.Insets(2, 2, 2, 2));
  74.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  75.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  76.                 jButton1ActionPerformed(evt);
  77.             }
  78.         });
  79.  
  80.         jButton2.setText("Disconnect");
  81.         jButton2.setMargin(new java.awt.Insets(2, 2, 2, 2));
  82.  
  83.         jTextField1.addActionListener(new java.awt.event.ActionListener() {
  84.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  85.                 jTextField1ActionPerformed(evt);
  86.             }
  87.         });
  88.  
  89.         jButton3.setText("Send");
  90.         jButton3.setMargin(new java.awt.Insets(2, 2, 2, 2));
  91.         jButton3.addActionListener(new java.awt.event.ActionListener() {
  92.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  93.                 jButton3ActionPerformed(evt);
  94.             }
  95.         });
  96.  
  97.         jList2.setModel(new javax.swing.AbstractListModel() {
  98.             String[] strings = { "ram", "a", "Item 3", "Item 4", "Item 5" };
  99.             public int getSize() { return strings.length; }
  100.             public Object getElementAt(int i) { return strings[i]; }
  101.         });
  102.         jScrollPane3.setViewportView(jList2);
  103.  
  104.         jLabel3.setText("jLabel3");
  105.  
  106.         jMenu1.setText("File");
  107.         jMenuBar1.add(jMenu1);
  108.  
  109.         jMenu2.setText("Availability");
  110.         jMenuBar1.add(jMenu2);
  111.  
  112.         setJMenuBar(jMenuBar1);
  113.  
  114.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  115.         getContentPane().setLayout(layout);
  116.         layout.setHorizontalGroup(
  117.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  118.             .addGroup(layout.createSequentialGroup()
  119.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  120.                     .addGroup(layout.createSequentialGroup()
  121.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
  122.                         .addGap(24, 24, 24)
  123.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  124.                             .addComponent(jLabel1)
  125.                             .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))
  126.                     .addGroup(layout.createSequentialGroup()
  127.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  128.                             .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
  129.                             .addGroup(layout.createSequentialGroup()
  130.                                 .addGap(20, 20, 20)
  131.                                 .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)))
  132.                         .addGap(7, 7, 7)
  133.                         .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
  134.                     .addGroup(layout.createSequentialGroup()
  135.                         .addComponent(jLabel2)
  136.                         .addGap(230, 230, 230)
  137.                         .addComponent(jButton1)
  138.                         .addGap(11, 11, 11)
  139.                         .addComponent(jButton2)))
  140.                 .addContainerGap(28, Short.MAX_VALUE))
  141.         );
  142.         layout.setVerticalGroup(
  143.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  144.             .addGroup(layout.createSequentialGroup()
  145.                 .addGap(10, 10, 10)
  146.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  147.                     .addComponent(jButton1)
  148.                     .addComponent(jButton2)
  149.                     .addGroup(layout.createSequentialGroup()
  150.                         .addGap(5, 5, 5)
  151.                         .addComponent(jLabel2)))
  152.                 .addGap(7, 7, 7)
  153.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  154.                     .addGroup(layout.createSequentialGroup()
  155.                         .addComponent(jLabel1)
  156.                         .addGap(6, 6, 6)
  157.                         .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  158.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
  159.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  160.                     .addGroup(layout.createSequentialGroup()
  161.                         .addGap(36, 36, 36)
  162.                         .addComponent(jButton3))
  163.                     .addGroup(layout.createSequentialGroup()
  164.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  165.                         .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
  166.                         .addGap(15, 15, 15)
  167.                         .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  168.                 .addContainerGap(23, Short.MAX_VALUE))
  169.         );
  170.  
  171.         java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  172.         setBounds((screenSize.width-491)/2, (screenSize.height-381)/2, 491, 381);
  173.     }// </editor-fold>                        
  174.  
  175.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  176.         int portNumber = 2222;
  177.         String host = "localhost";
  178.         try {
  179.             clientSocket = new Socket(host, portNumber);
  180.            // System.out.println(clientSocket);
  181.             System.out.println("connected");
  182.             inputLine = new BufferedReader(new InputStreamReader(System.in));
  183.             os = new PrintStream(clientSocket.getOutputStream());
  184.             is = new DataInputStream(clientSocket.getInputStream());
  185.             //"from @! to @! message@! type of msg";
  186.             os.println(uname + " @! server @! " + uname + " @! iamonline");//@!@!
  187.             this.setTitle("Talk to me: "+uname);
  188.            
  189.         } catch (UnknownHostException e) {
  190.             System.err.println("Don't know about host " + host);
  191.         } catch (IOException e) {
  192.             System.err.println("Couldn't get I/O for the connection to the host "+ host);
  193.         }
  194.        
  195.        
  196.         if (clientSocket != null && os != null && is != null) {
  197.             try {
  198.  
  199.               new Thread(new ClientGUI()).start();
  200.                
  201.             } catch (Exception e) {
  202.                 System.err.println("IOException:  " + e);
  203.             }
  204.         }        
  205.     }                                        
  206.  
  207.     private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
  208.         String typeMsg = jTextField1.getText();//@!
  209.         String selected = jList2.getSelectedValue().toString();
  210.         //System.out.print("msg sent to: "+selected+" "+typeMsg);
  211.         //os.println(uname + "@!" + selected + "@!" + typeMsg + "@!msg");
  212.         String Quit="quit";
  213.         //System.out.println(selected);                          
  214.         if(Quit.equalsIgnoreCase(typeMsg.trim()))
  215.         {os.println(uname + "@!" + selected + "@!" + typeMsg + "@!quit");
  216.         System.out.print("oi quit vo re");
  217.         jTextArea1.append(typeMsg);
  218.         }
  219.         else{
  220.         os.println(uname + "@!" + selected + "@!" + typeMsg + "@!msg");
  221.         //System.out.print("msg sent to: "+selected+" "+typeMsg);
  222.         jTextArea1.append(typeMsg+"\n");
  223.         }
  224.         jTextField1.setText("");
  225.     }                                          
  226.  
  227.     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  228.         String typeMsg = jTextField1.getText();//@!
  229.         String selected = jList2.getSelectedValue().toString();
  230.         String Quit="quit";
  231.         //System.out.println(selected);                          
  232.         if(Quit.equalsIgnoreCase(typeMsg.trim()))
  233.         {os.println(uname + "@!" + selected + "@!" + typeMsg + "@!quit");
  234.         System.out.print("oi quit vo re");
  235.         jTextArea1.append(typeMsg);
  236.         }
  237.         else{
  238.         os.println(uname + "@!" + selected + "@!" + typeMsg + "@!msg");
  239.        // System.out.print("msg sent to: "+selected+" "+typeMsg);
  240.         jTextArea1.append(typeMsg+"\n");
  241.         }
  242.         jTextField1.setText("");
  243.     }                                        
  244.  
  245.     public static void main(String args[]) {
  246.  
  247.          System.out.println("lau main() vitra po chirecha ta.achamma hai:0)");
  248.  
  249.         /*
  250.          * Set the Nimbus look and feel
  251.          */
  252.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  253.         /*
  254.          * If Nimbus (introduced in Java SE 6) is not available, stay with the
  255.          * default look and feel. For details see
  256.          * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  257.          */
  258.         try {
  259.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  260.                 if ("Nimbus".equals(info.getName())) {
  261.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  262.                     break;
  263.                 }
  264.             }
  265.         } catch (ClassNotFoundException ex) {
  266.             java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  267.         } catch (InstantiationException ex) {
  268.             java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  269.         } catch (IllegalAccessException ex) {
  270.             java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  271.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  272.             java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  273.         }
  274.         //</editor-fold>
  275.  
  276.         /*
  277.          * Create and display the form
  278.          */
  279.         java.awt.EventQueue.invokeLater(new Runnable() {
  280.  
  281.             public void run() {
  282.                 new ClientGUI().setVisible(true);
  283.                 System.out.println("lau yo ni printed lol");
  284.                
  285.             }
  286.         });
  287.     }
  288.     // Variables declaration - do not modify                    
  289.     private javax.swing.JButton jButton1;
  290.     private javax.swing.JButton jButton2;
  291.     private javax.swing.JButton jButton3;
  292.     private javax.swing.JLabel jLabel1;
  293.     private javax.swing.JLabel jLabel2;
  294.     private javax.swing.JLabel jLabel3;
  295.     private javax.swing.JList jList2;
  296.     private javax.swing.JMenu jMenu1;
  297.     private javax.swing.JMenu jMenu2;
  298.     private javax.swing.JMenuBar jMenuBar1;
  299.     private javax.swing.JScrollPane jScrollPane1;
  300.     private javax.swing.JScrollPane jScrollPane3;
  301.     private javax.swing.JTextArea jTextArea1;
  302.     private javax.swing.JTextField jTextField1;
  303.     // End of variables declaration                  
  304. String responseLine;
  305.     @Override
  306.     public  void run() {
  307.         /*
  308.          * Keep on reading from the socket till we receive "Bye" from the
  309.          * server. Once we received that then we want to break.
  310.          */
  311.         System.out.println("aba yo print vayo hai");
  312. //        String responseLine;
  313.         try {
  314.             while ((responseLine = is.readLine().trim()) != null) {
  315.                 String[] decodedResponseLine = responseLine.split("@!");
  316.                                
  317.                 if (decodedResponseLine.length == 4) {
  318.                     if (decodedResponseLine[1].trim().equals(uname)) {
  319.                         System.out.println("jkjlkjkljjjjjjjjjj"+java.awt.EventQueue.isDispatchThread());
  320.                         jTextArea1.setText("aayooooooooo hai");
  321.                         jLabel3.setText(decodedResponseLine[2].toString().trim());
  322.  
  323.                     }
  324.                 } else if (decodedResponseLine.length == 3) {
  325.                     //jList1.
  326.                 } else if (decodedResponseLine.length == 5) {
  327.                 }
  328.                
  329.                 java.awt.EventQueue.invokeLater(new Runnable() {
  330.                 public void run() {
  331.                 System.out.println("EDT running here "+java.awt.EventQueue.isDispatchThread());
  332.                 System.out.println("from invokelater");
  333.                 jTextArea1.append(responseLine);
  334.                 jLabel3.setText("suleman");
  335.             }
  336.         });
  337.                 System.out.println("jkjlkjkl"+java.awt.EventQueue.isDispatchThread());
  338.                
  339.                
  340.  
  341.                 if (responseLine.indexOf("*** Bye") != -1) {
  342.                     break;
  343.                 }
  344.             }
  345.             closed = true;
  346.         } catch (IOException e) {
  347.             System.err.println("IOException:  " + e);
  348.         }
  349.     }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement