Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package chatgui4;
- import java.awt.EventQueue;
- import java.io.*;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import javax.swing.JOptionPane;
- import javax.swing.SwingUtilities;
- /**
- *
- * @author sulav
- */
- public class ClientGUI extends javax.swing.JFrame implements Runnable {
- /**
- * Creates new form ClientGUI
- */
- private static Socket clientSocket = null;
- // The output stream
- private static PrintStream os = null;
- // The input stream
- private static DataInputStream is = null;
- private static BufferedReader inputLine = null;
- private static boolean closed = false;
- public static String uname;
- public ClientGUI() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jScrollPane1 = new javax.swing.JScrollPane();
- jTextArea1 = new javax.swing.JTextArea();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jButton1 = new javax.swing.JButton();
- jButton2 = new javax.swing.JButton();
- jTextField1 = new javax.swing.JTextField();
- jButton3 = new javax.swing.JButton();
- jScrollPane3 = new javax.swing.JScrollPane();
- jList2 = new javax.swing.JList();
- jLabel3 = new javax.swing.JLabel();
- jMenuBar1 = new javax.swing.JMenuBar();
- jMenu1 = new javax.swing.JMenu();
- jMenu2 = new javax.swing.JMenu();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- jTextArea1.setColumns(20);
- jTextArea1.setEditable(false);
- jTextArea1.setRows(5);
- jScrollPane1.setViewportView(jTextArea1);
- jLabel1.setText("Online Users");
- jLabel2.setText("Terminal");
- jButton1.setText("Connect");
- jButton1.setMargin(new java.awt.Insets(2, 2, 2, 2));
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jButton2.setText("Disconnect");
- jButton2.setMargin(new java.awt.Insets(2, 2, 2, 2));
- jTextField1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1ActionPerformed(evt);
- }
- });
- jButton3.setText("Send");
- jButton3.setMargin(new java.awt.Insets(2, 2, 2, 2));
- jButton3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton3ActionPerformed(evt);
- }
- });
- jList2.setModel(new javax.swing.AbstractListModel() {
- String[] strings = { "ram", "a", "Item 3", "Item 4", "Item 5" };
- public int getSize() { return strings.length; }
- public Object getElementAt(int i) { return strings[i]; }
- });
- jScrollPane3.setViewportView(jList2);
- jLabel3.setText("jLabel3");
- jMenu1.setText("File");
- jMenuBar1.add(jMenu1);
- jMenu2.setText("Availability");
- jMenuBar1.add(jMenu2);
- setJMenuBar(jMenuBar1);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(24, 24, 24)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel1)
- .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addGap(20, 20, 20)
- .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGap(7, 7, 7)
- .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel2)
- .addGap(230, 230, 230)
- .addComponent(jButton1)
- .addGap(11, 11, 11)
- .addComponent(jButton2)))
- .addContainerGap(28, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(10, 10, 10)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jButton1)
- .addComponent(jButton2)
- .addGroup(layout.createSequentialGroup()
- .addGap(5, 5, 5)
- .addComponent(jLabel2)))
- .addGap(7, 7, 7)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel1)
- .addGap(6, 6, 6)
- .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(36, 36, 36)
- .addComponent(jButton3))
- .addGroup(layout.createSequentialGroup()
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(15, 15, 15)
- .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(23, Short.MAX_VALUE))
- );
- java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
- setBounds((screenSize.width-491)/2, (screenSize.height-381)/2, 491, 381);
- }// </editor-fold>
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- int portNumber = 2222;
- String host = "localhost";
- try {
- clientSocket = new Socket(host, portNumber);
- // System.out.println(clientSocket);
- System.out.println("connected");
- inputLine = new BufferedReader(new InputStreamReader(System.in));
- os = new PrintStream(clientSocket.getOutputStream());
- is = new DataInputStream(clientSocket.getInputStream());
- //"from @! to @! message@! type of msg";
- os.println(uname + " @! server @! " + uname + " @! iamonline");//@!@!
- this.setTitle("Talk to me: "+uname);
- } catch (UnknownHostException e) {
- System.err.println("Don't know about host " + host);
- } catch (IOException e) {
- System.err.println("Couldn't get I/O for the connection to the host "+ host);
- }
- if (clientSocket != null && os != null && is != null) {
- try {
- new Thread(new ClientGUI()).start();
- } catch (Exception e) {
- System.err.println("IOException: " + e);
- }
- }
- }
- private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
- String typeMsg = jTextField1.getText();//@!
- String selected = jList2.getSelectedValue().toString();
- //System.out.print("msg sent to: "+selected+" "+typeMsg);
- //os.println(uname + "@!" + selected + "@!" + typeMsg + "@!msg");
- String Quit="quit";
- //System.out.println(selected);
- if(Quit.equalsIgnoreCase(typeMsg.trim()))
- {os.println(uname + "@!" + selected + "@!" + typeMsg + "@!quit");
- System.out.print("oi quit vo re");
- jTextArea1.append(typeMsg);
- }
- else{
- os.println(uname + "@!" + selected + "@!" + typeMsg + "@!msg");
- //System.out.print("msg sent to: "+selected+" "+typeMsg);
- jTextArea1.append(typeMsg+"\n");
- }
- jTextField1.setText("");
- }
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- String typeMsg = jTextField1.getText();//@!
- String selected = jList2.getSelectedValue().toString();
- String Quit="quit";
- //System.out.println(selected);
- if(Quit.equalsIgnoreCase(typeMsg.trim()))
- {os.println(uname + "@!" + selected + "@!" + typeMsg + "@!quit");
- System.out.print("oi quit vo re");
- jTextArea1.append(typeMsg);
- }
- else{
- os.println(uname + "@!" + selected + "@!" + typeMsg + "@!msg");
- // System.out.print("msg sent to: "+selected+" "+typeMsg);
- jTextArea1.append(typeMsg+"\n");
- }
- jTextField1.setText("");
- }
- public static void main(String args[]) {
- System.out.println("lau main() vitra po chirecha ta.achamma hai:0)");
- /*
- * Set the Nimbus look and feel
- */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /*
- * If Nimbus (introduced in Java SE 6) is not available, stay with the
- * default look and feel. For details see
- * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /*
- * Create and display the form
- */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new ClientGUI().setVisible(true);
- System.out.println("lau yo ni printed lol");
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JButton jButton3;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JList jList2;
- private javax.swing.JMenu jMenu1;
- private javax.swing.JMenu jMenu2;
- private javax.swing.JMenuBar jMenuBar1;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JScrollPane jScrollPane3;
- private javax.swing.JTextArea jTextArea1;
- private javax.swing.JTextField jTextField1;
- // End of variables declaration
- String responseLine;
- @Override
- public void run() {
- /*
- * Keep on reading from the socket till we receive "Bye" from the
- * server. Once we received that then we want to break.
- */
- System.out.println("aba yo print vayo hai");
- // String responseLine;
- try {
- while ((responseLine = is.readLine().trim()) != null) {
- String[] decodedResponseLine = responseLine.split("@!");
- if (decodedResponseLine.length == 4) {
- if (decodedResponseLine[1].trim().equals(uname)) {
- System.out.println("jkjlkjkljjjjjjjjjj"+java.awt.EventQueue.isDispatchThread());
- jTextArea1.setText("aayooooooooo hai");
- jLabel3.setText(decodedResponseLine[2].toString().trim());
- }
- } else if (decodedResponseLine.length == 3) {
- //jList1.
- } else if (decodedResponseLine.length == 5) {
- }
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- System.out.println("EDT running here "+java.awt.EventQueue.isDispatchThread());
- System.out.println("from invokelater");
- jTextArea1.append(responseLine);
- jLabel3.setText("suleman");
- }
- });
- System.out.println("jkjlkjkl"+java.awt.EventQueue.isDispatchThread());
- if (responseLine.indexOf("*** Bye") != -1) {
- break;
- }
- }
- closed = true;
- } catch (IOException e) {
- System.err.println("IOException: " + e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement