Ajouter un commentaire

Niroken

Hello,

Voici un code complet qui marche : (je n'ai pas mis de connexion à une base de données tu feras ca)

import graphic.JListViewer;


public class JListViewerLauncher {

    /**
     * @param args
     */
    public static void main(String[] args) {
        new JListViewer();
    }

}

package graphic;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;

import net.ProcessClient;

public class JListViewer  extends JFrame {
    
    private static final long serialVersionUID = 1L;
    
    public JList mList;
    
    public JListViewer() {
        setSize(200, 200);
        setLayout(new FlowLayout());
        
        mList = new JList();
        DefaultListModel vDefaultListModel = new DefaultListModel();
        vDefaultListModel.addElement("Pouet");
        vDefaultListModel.addElement("Pouet");
        mList.setModel(vDefaultListModel);
        
        JButton vButton = new JButton("Refresh");
        vButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent pE) {
                displayDatasFromServer();
            }
            
        });
        
        add(mList);
        add(vButton);
        
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    
    private void displayDatasFromServer() {
        Thread vThread = new Thread() {
            
            public void run () {
                try {
                    mList.setModel(new ProcessClient().getDatasFromServer());
                } catch (Exception e) {
                    e.printStackTrace();
                }                
            }
            
        };
        vThread.start();
    }
    
}

package net;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;

import javax.swing.DefaultListModel;

public class ProcessClient {
    
    public DefaultListModel getDatasFromServer() throws Exception {
        DefaultListModel vResult = new DefaultListModel();
               
        Socket vClientSocket = new Socket("localhost",3500); 
        OutputStream vClientOutputStream = vClientSocket.getOutputStream();
        InputStream vClientInputStream = vClientSocket.getInputStream();
        
        PrintWriter vOutputPrintWriter = new PrintWriter(vClientOutputStream,true); 
        BufferedReader vInputBufferedReader = new BufferedReader(new InputStreamReader(vClientInputStream)); 
        String vLineFromServer = null;
        //Debbut de la conversation client-serveur
        writeLineInPrintWriter(vOutputPrintWriter, "OK");
        vLineFromServer = vInputBufferedReader.readLine();
        vResult.addElement(vLineFromServer);
        writeLineInPrintWriter(vOutputPrintWriter, "OK");
        
        while(vLineFromServer!=null) { 
            try {
                vLineFromServer = vInputBufferedReader.readLine();
                vResult.addElement(vLineFromServer);
                writeLineInPrintWriter(vOutputPrintWriter, "OK");
            } catch (SocketException e) {
                // On met vLineFromServer a null pour sortir de la 
                // boucle si le serveur interromp la connexion
                vLineFromServer = null;
                e.printStackTrace();
            }
        }
        
        vClientSocket.close(); 
                
        return vResult;
    }
    
    private void writeLineInPrintWriter(PrintWriter pPrintWriter, String pMessage) {
        if (pPrintWriter != null) {
            pPrintWriter.write(pMessage + System.getProperty("line.separator"));
            pPrintWriter.flush();
        }
    }
    
}

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


public class ProcessServer {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Thread threadlistpseudo=new Thread() 
        { 
            public PrintWriter out=null; 
            public BufferedReader in=null; 
            public ServerSocket sEcoute=null; 
            public Socket sService=null; 
            
            public void run() 
            { 
                try { 
                        sEcoute =new ServerSocket(3500); 
                        sService=sEcoute.accept(); 
                        out=new PrintWriter(sService.getOutputStream(),true); 
                        in=new BufferedReader(new InputStreamReader(sService.getInputStream())); 
                        
                        String query=in.readLine(); 
                        System.out.println(query);
                        
                        for (int i = 0; i < 10; i++) {
                            out.write(i + "Pouet\r\n");
                            out.flush(); 
                            in.readLine();
                        }
                        
                        out.flush(); 
                        
                        sEcoute.close(); 
                    
                } catch(Exception ex) {
                    ex.printStackTrace();
                } 
            } 
        }; 
        threadlistpseudo.start(); 


    }

}

Bonne chance,
Niroken

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 RRRR   EEEE   CCC  ZZZZZ   AA  
R R E C Z A A
RRRR EEE C Z AAAA
R R E C Z A A
R RR EEEE CCC ZZZZZ A A