I'm trying to implement a chatroom using RMI and everything was fine until I tried to bring my GUI into the picture, from what I can see the RMI aspect is fine, the client is sending the info to the server, the server sends it to all clients with callback.

They seem to get the info because I can display the strings via joptionpanes, but I can't put the strings into the textarea! As it happens I can't seem to change anything in the GUI once the program has started (.setText("awdad") doesnt do anything). Heres my GUI

classpackage ChatroomApp;
import java.rmi.RemoteException;
import java.util.Scanner;

//import java.net.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.Color;
import java.io.*;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;

public class ChatGUI extends JFrame implements ActionListener{
    String username;
    String[] users = {"john","kathrine","eddie"};
    //String sentMessage;

    JPanel contentPane,chatPanel;
    JButton sendMessage;
    JTextArea chatLog;
    JTextField messageField;

    static Chatroom remote;
    static ChatGUI gui;
    //JList roomUsers;

    ServerChanged sc;

    boolean sendPressed = false;

    public ChatGUI() throws RemoteException {
    JFrame manager = this;
    manager.setVisible(true);
       contentPane = new JPanel();
        contentPane = (JPanel)getContentPane();
        contentPane.setVisible(true);
        setBounds(150, 100, 750, 600);
         chatPanel = new JPanel();
          messageField = new JTextField();
                chatLog = new JTextArea();
                sendMessage = new JButton("Send");
                //roomUsers = new JList(users);
        
        contentPane.setBackground(Color.gray);
        
    username = JOptionPane.showInputDialog("Enter username please");
        sc = new ClientImpl(username);

        chatPanel.setBackground(Color.lightGray);
        chatPanel.setBorder(BorderFactory.createLineBorder(Color.black,1));
        chatPanel.setLayout(null);

 
	chatLog.setBackground(Color.white);
        chatLog.setEditable(true);
        chatLog.setLineWrap(true);
        chatLog.setText("chat log \n");
        chatLog.setBorder(BorderFactory.createLineBorder(Color.black,1));

       
        messageField.setBackground(Color.white);
        messageField.setEditable(true);
        messageField.addKeyListener(new MyKeyListener());
        messageField.addActionListener(this);
        messageField.setBorder(BorderFactory.createLineBorder(Color.black,1));


        
        sendMessage.addActionListener(this);

        
        //roomUsers.setBorder(BorderFactory.createLineBorder(Color.black,1));

        chatPanel.add(chatLog);
        chatPanel.add(messageField);
        chatPanel.add(sendMessage);
        //chatPanel.add(roomUsers);
        chatLog.setBounds(20,20,500,420);
        messageField.setBounds(20,460,500,60);
        sendMessage.setBounds(540,460,120,60);
        //roomUsers.setBounds(540,20,120,420);

        contentPane.add(chatPanel);

        

        
    }


    public static void main(String args[]) throws RemoteException{
        /*JFrame manager = new ChatGUI();
	manager.setTitle("Text Editor");
	manager.setVisible(true);

        manager.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent event)
            {
                System.exit(0);
            }
        });*/

        System.setSecurityManager( new RMISecurityManager());
        try  {
                gui = new ChatGUI();

      //sc.setName(username);
      remote=(Chatroom)Naming.lookup( "rmi://localhost/ChatroomServer");
      //UnicastRemoteObject.exportObject(sc,1099);
      remote.register(gui);
      remote.updateClients("YOYO", "ADAWDAW");


       }catch (Exception e){
           System.out.println(e.getMessage());
            e.printStackTrace();
       }

    }


    public void send(){
        try {
            remote.updateClients(messageField.getText(), username);
            
        } catch (RemoteException ex) {
            Logger.getLogger(ChatGUI.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public String getMessage(){
        String messageToServer = new String();
        
        messageToServer = messageField.getText();
        messageField.setText("");

        return messageToServer;
    }

    public void addToLog(String message, String sender, String receiver){
        String forLog = sender + " : \n\t" + message + " and receiver : " + receiver;
        chatLog.append(forLog);
        messageField.setText("AWDAW");
        contentPane.updateUI();
        contentPane.repaint();
        contentPane.revalidate();
        JOptionPane.showConfirmDialog(null,forLog);     //this outputs forLog fine

    }

    public String getUsername() throws RemoteException{
        return sc.getName();
    }


    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == sendMessage)
        {
           send();
        }

    }

    public class MyKeyListener implements KeyListener
    {
        public void keyTyped(KeyEvent e) {

        }


        @Override
        public void keyReleased(KeyEvent e) {

        }                
                          
        @Override
        public void keyPressed(KeyEvent e){
        int key = e.getKeyCode();
             if (key == KeyEvent.VK_ENTER) {
                 send();
             }
        }

    }

}

Heres the chatroom code where callback is done

package ChatroomApp;
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import javax.swing.JOptionPane;

public class ChatroomImpl extends UnicastRemoteObject implements Chatroom{
     Vector clients;
      private float balance=0;
      private String toAll;
      private String sender;

      public ChatroomImpl () throws RemoteException{
         super();   clients =new Vector();
      }

    public void register (ChatGUI newClient) throws RemoteException {
        synchronized(clients){
            clients.addElement(newClient);
        }
        for(int i=0;i<clients.size();i++){
            ChatGUI tmpClient = (ChatGUI)clients.elementAt(i);
            System.out.println("\n" + tmpClient.getUsername());
        }
    }
    public void updateClients(String message,String username){
        JOptionPane.showConfirmDialog(null, "IN UPDATE CLIENTS");
         int count, n ;
         ChatGUI tmpClient;
         synchronized(clients){
              if ( (n =clients.size())>0)
                 for (count=0;count<n;count++){
                    tmpClient= (ChatGUI)clients.elementAt(count);
                    try {
                        String hi = tmpClient.getUsername();
                        tmpClient.addToLog(message,username,hi);
                    } catch(Exception e){
                        System.out.println("failed to update client:"+tmpClient.toString());
                    }
                }
           }
      }

    public void sendMessage (String message, String username) throws RemoteException {
        JOptionPane.showConfirmDialog(null, "SEND MESSAGE - CHATROOMIMPL");
        toAll = message;
        sender = username;
        //updateClients();
    }
}

I can't say for certain what's blocking your UI update, but you could try executing the two statements that update the text fields in a task on the event queue like so

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        chatLog.append(forLog);
        messageField.setText("AWDAW");
    }
});

That will make sure the GUI update is invoked on the event queue. What it won't do is ensure that the event queue is not blocked waiting on something else. I could be overlooking something obvious, but without being able to run the app I'm not sure what else to suggest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.