简体   繁体   中英

ActionListener invoke while loop method

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?

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;
    }
}

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