简体   繁体   中英

rmi chat room not able to send data to server

I am developing a rmi chat room with some GUI. I have to take input from client n send it to server.But I am not able to use the value read from Jtextfield. Can anyone pls help me how to use the read input from user to send to server

import java.applet.Applet;
import java.awt.*
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.*;

public class MyChatClient extends UnicastRemoteObject{

     JFrame F = new JFrame("Chat room");
 JTextField T = new JTextField(25); 
 JPanel pane = new JPanel(new GridBagLayout());
 JButton B = new JButton("submit");
 JPanel pane2 = new JPanel(new GridBagLayout());
 TextArea TA = new TextArea(15,50);
 String response;

protected  MyChatClient() throws RemoteException {


    F.setSize(400,400);
    pane.add(T);
    pane.add(B);
    F.add(pane, BorderLayout.SOUTH);

    TA.setEditable(false);
    TA.setBackground(Color.WHITE);

    pane2.add(TA);
    F.add(pane2, BorderLayout.BEFORE_FIRST_LINE);
    F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    F.setVisible(true);


        B.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                response = T.getText();  // I am not able to send this to server                                                                  

             }});
    }

   public static void main(String[] args) throws RemoteException {

    MyChatClient mc = new MyChatClient();
    try{
        //System.setSecurityManager(new RMISecurityManager());
        //Registry reg = LocateRegistry.getRegistry("localhost");

       ServerImpl svr = (ServerImpl)Naming.lookup("rmi://localhost/ChatServer") ;
                    System.out.println("Server found"); 
             svr.sendMessage(response); // here is the problem

     }catch(Exception e){
        System.err.print(e);
    }

    }

 }

I think your action listener is never called. Try checking this,

B.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                response = T.getText(); 
                System.out.println("inside action");                                                 

             }});

Or add a button and perform this operation inside its click event. good luck !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM