/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tchat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author bboybaki
*/
public class Fenetre extends javax.swing.JFrame {
Socket socket;
BufferedReader in;
PrintWriter out;
BufferedReader ecrire = new BufferedReader(new InputStreamReader(System.in));
Reception r;
/**
* Creates new form Fenetre
*/
public Fenetre() {
initComponents();
try {
socket = new Socket(InetAddress.getLocalHost(),9000);
in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
// Reception
r = new Reception();
r.start();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class Reception extends Thread {
public Reception() {
}
public void run() {
try {
String message_distant;
while(!(message_distant = in.readLine()).equals("DeconnexionServeur")) {
Conteneur.setText(Conteneur.getText() + message_distant + "\n");
}
javax.swing.JOptionPane.showMessageDialog(null,"Deconnexion");
System.exit(0);
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 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")
// //GEN-BEGIN:initComponents
private void initComponents() {
jMenuItem4 = new javax.swing.JMenuItem();
Conteneur = new javax.swing.JTextArea();
txtChampMessage = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
btnPosterMsg = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem4.setText("jMenuItem4");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Conteneur.setColumns(20);
Conteneur.setEditable(false);
Conteneur.setRows(20);
Conteneur.setToolTipText("");
Conteneur.setFocusable(false);
jLabel1.setText("Message : ");
btnPosterMsg.setText("Poster");
btnPosterMsg.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPosterMsgActionPerformed(evt);
}
});
jMenu1.setText("Fichier");
jMenuItem1.setText("Quitter");
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edition");
jMenuItem2.setText("Connexion");
jMenu2.add(jMenuItem2);
jMenuItem3.setText("Deconnexion");
jMenu2.add(jMenuItem3);
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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtChampMessage)
.addGap(18, 18, 18)
.addComponent(btnPosterMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(Conteneur, javax.swing.GroupLayout.PREFERRED_SIZE, 402, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(31, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(Conteneur, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtChampMessage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPosterMsg))
.addContainerGap(28, Short.MAX_VALUE))
);
pack();
}// //GEN-END:initComponents
private void btnPosterMsgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPosterMsgActionPerformed
out.println(txtChampMessage.getText());
out.flush();
}//GEN-LAST:event_btnPosterMsgActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//
/*
* 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(Fenetre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Fenetre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Fenetre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Fenetre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Fenetre().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextArea Conteneur;
private javax.swing.JButton btnPosterMsg;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JTextField txtChampMessage;
// End of variables declaration//GEN-END:variables
}