views:

64

answers:

1

Hi, sorry for posting a lot of code!!I don't know that why my ListFrame doesn't work??? these are the classes.At first I run the MainServer and then I will run the MainFrame in the other package.and then by inserting a correct user name and password ,the Listframe will be shown,BUT I click on menu bar or list or delete button but nothing will happen.why?? please help me.

MainSerevr class :

public class MainServer {

static Socket client = null;
static ServerSocket server = null;

public static void main(String[] args) {
    System.out.println("Server is starting...");
    System.out.println("Server is listening...");

    try {
        server = new ServerSocket(5050);
    } catch (IOException ex) {
        System.out.println("Could not listen on port 5050");
        System.exit(-1);

    }
    try {
        client = server.accept();
        System.out.println("Client Connected...");
    } catch (IOException e) {
        System.out.println("Accept failed: 5050");
        System.exit(-1);
    }
    try {
        BufferedReader streamIn = new BufferedReader(new InputStreamReader(client.getInputStream()));

        boolean done = false;
        String line;
        while (!done) {
            line = streamIn.readLine();
            if (line.equalsIgnoreCase(".bye")) {
                done = true;
            } else {
                System.out.println("Client says: " + line);
            }
        }

        streamIn.close();
        client.close();
        server.close();
    } catch (IOException e) {
        System.out.println("IO Error in streams " + e);
    }
}}

ListFrame:

 public class ListFrame extends javax.swing.JFrame implements PersonsModelChangeListener {

    private InformationClass client;
    private static DefaultListModel model = new DefaultListModel();
    private ListSelectionModel moDel;

    /** Creates new form ListFrame */
    public ListFrame(InformationClass client) {
        initComponents();
        this.client = client;
        jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


        fillTable();
        Manager.addListener(this);
    }

    private void deleteAPerson() {
        int index = jList1.getSelectedIndex();
        String yahooId = (String) jList1.getSelectedValue();
        model.remove(index);
        Manager.removeApersonFromSQL(yahooId);
        int size = model.getSize();
        if (size == 0) {
            jButton1.setEnabled(false);
        } else {
            if (index == size) {
                index--;
            }
            jList1.setSelectedIndex(index);
            jList1.ensureIndexIsVisible(index);

        }
    }                       



  private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        AddAPerson frame = new AddAPerson(client);
        frame.setVisible(true);


    }                                          

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        deleteAPerson();
    }                                        

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {                                    
        MainClient.setText("");
        MainClient.runAClient();
        ChatFrame frame = new ChatFrame(client);
        frame.setVisible(true);
    }  
    public void fillTable() {
    try {
        List<InformationClass> list = null;
        list = Manager.getClientListFromMySQL();
        if (list == null) {

            JOptionPane.showMessageDialog(this, "You should add a person to your list", "Information", JOptionPane.OK_OPTION);
            return;
        } else {


            for (int i = 0; i < list.size(); i++) {
                InformationClass list1 = list.get(i);
                model.add(i, list1.getId());
            }

            jList1.setModel(model);
        }


    } catch (SQLException ex) {
        Logger.getLogger(ListFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}

MainClient class:

public class MainClient {


private static InformationClass info = new InformationClass();
private static Socket c;
private static String text;

public static String getText() {
    return text;
}

public static void setText(String text) {
    MainClient.text = text;
}

private static PrintWriter os;
private static BufferedReader is;
static boolean closed = false;

/**
 * @param args the command line arguments
 */
public static void runAClient() {
    try {
        c = new Socket("localhost", 5050);

        os = new PrintWriter(c.getOutputStream());
        is = new BufferedReader(new InputStreamReader(c.getInputStream()));

        String teXt = getText();
        os.println(teXt);
        if(c!=null&& is!=null&&os!=null){
        String line = is.readLine();
        System.out.println("Text received: " + line);
        }


        c.close();
        is.close();
        os.close();


    } catch (UnknownHostException ex) {
        System.err.println("Don't know about host");

    } catch (Exception e) {
        System.err.println("IOException:  " + e);

    }

}


}

EDIT:I have found the problem,which is because of writting MainClient.runAClient() in code ,where should I put it? please help me.

+1  A: 

This article contains an sscce that illustrates a simple client-server GUI. You may find it instructive. If so, consider how you would address the bug found in the last line of the Echo(Kind kind) constructor.

trashgod
I have found the problem which is because of writing the MainClient.runAClient() method in the code.whould you please help me that where should i put this line?thanks
Johanna
I have no way to test your code. In general, you can start your client thread any time after the server is ready to accept new connections. In the `main()` method of the example I cited, the server starts first, followed by the client; both run until either is closed.
trashgod