0

I add a ActionListener which create a new chat JFrame that can send and receive message from the Server. this is the code in the ActionPerformed() method

BananaChat chat = new BananaChat(name, password, IP, port, status);
    try {
        chat.chatting();
    } catch (Exception e) {
        showInfo("fail");
    }

so it create a new chat frame, if I didn't invoke the chat.chatting() method, I can send the message to Server normally, But cannot receive the message from server. So I have to invoke this method because I need to keeping listening the message from server if it does send the message.

here is the code of chatting()

String line = null;

    try {
        while ((line = in.readLine()) != null) {
            if (line.equals("your user name is already registered") || line.equals("user name doesn't exist") || line.equals("wrong password")) {
                showMessage(line);
                break;
            }

            showMessage(line);
        }

    } catch (IOException e) {
        showMessage("can not receive the message");
    }

It is the while loop. If I create my chat frame and invoke this method in the main method, it can work, but if I create the chat frame in ActionListener, it is stuck. it seems that the ActionListener cannot have a while Loop which doesn't end at all.

I don't know how to solve it, is there a better way to create a new chat interface from the login interface?

高亮节
  • 169
  • 1
  • 1
  • 9
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson May 02 '15 at 09:53
  • 2
    Do not run blocking code in the event dispatch thread. You need to run such code in a background thread - usually that is most conveniently done with a [SwingWorker](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html). – kiheru May 02 '15 at 10:05
  • Can you add more code to the question, like for example the `BananaChat` class? This will make more specific suggestions possible. – Freek de Bruijn May 02 '15 at 11:43

1 Answers1

0

As already suggested by kiheru, it is probably a good idea to run your chatting code in a background thread - for example using a SwingWorker (which is made especially for use with Swing GUIs). With some guess work regarding the BananaChat class, a very simple (one directional) chat program could look like this:

A main class:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Chat {
    private int nextPort = 80;

    public static void main(final String[] args) {
        new Chat().launchGui();
    }

    private void launchGui() {
        final JFrame frame = new JFrame("Stack Overflow: chat with multiple servers");
        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel();
        final JButton startChatButton = new JButton("Start a new chat");
        startChatButton.addActionListener(actionEvent -> {
            final BananaChat bananaChat = new BananaChat("user name", "password",
                "192.0.0.1", String.valueOf(nextPort), "???");
            nextPort++;
            bananaChat.startChatting();
        });
        panel.add(startChatButton);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
    }
}

A BananaChat class:

import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;

public class BananaChat extends JFrame {
    private final String userName;
    private final String password;
    private final String IP;
    private final String port;
    private final String status;
    private final Random dummyServerMessages;
    private final JTextArea messagesTextArea;

    public BananaChat(final String userName, final String password, final String IP,
                      final String port, final String status) {
        super("Chat with " + IP + ":" + port);
        this.userName = userName;
        this.password = password;
        this.IP = IP;
        this.port = port;
        this.status = status;
        dummyServerMessages = new Random(123456);
        messagesTextArea = new JTextArea();
        add(new JScrollPane(messagesTextArea));
        setBounds(1000, 100, 400, 200);
        setVisible(true);
    }

    public void startChatting() {
        final SwingWorker chatWorker = new SwingWorker<Void, String>() {
            @Override
            protected Void doInBackground() {
                while (!isCancelled()) {
                    final String message = getMessageFromServer();
                    publish(message + "\n");
                    pause(1000);
                }
                return null;
            }

            @Override
            protected void process(final List<String> messages) {
                messages.forEach(messagesTextArea::append);
            }
        };

        chatWorker.execute();
    }

    // Generate silly random messages. Can be replaced by a call to in.readLine().
    private String getMessageFromServer() {
        final String message;
        if (dummyServerMessages.nextInt(6) < 2)
            message = "Hello";
        else
            message = "Silence";
        return message + " from " + userName + "@" + IP + ":" + port;
    }

    private void pause(final int milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }
    }

    public String getPassword() {
        return password;
    }

    public String getStatus() {
        return status;
    }
}
Community
  • 1
  • 1
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28