user1978786
user1978786

Reputation: 25

Java - Socket Swing Application Crashes

I am making a Java Socket Swing Application. I created this void:

private static void sendMessage(JTextField message) {
    try {
        String data = user + ": " + message.getText();
        out.println(data);
        System.out.println(in.readLine());
    }
    catch(Exception exc) {
        JOptionPane.showMessageDialog(dpanel,
            "Could not send message. Reason: " + exc, "",
            JOptionPane.ERROR_MESSAGE);
    }
}

The program gets jammed up after I try to send the second message to the server. Can someone provide any recommendations for my code? Thanks!

P.S.

sendMessage() is triggered by a MouseLisitener for a JButton.
There is a PipeStream for System.err and out to a JTextArea.
This is what in out and connection is/are:

try {
    connection = new Socket(ipa, port);
    out = new PrintWriter(connection.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(connection.getInputStream())));
}
...

Upvotes: 0

Views: 196

Answers (3)

user1978786
user1978786

Reputation: 25

I deleted void part and put it in the mouse listener and open and closed the connection every time I sent a message. That prevented the program crashing. Thanks for helping me realize my errors.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347204

It sounds like you are trying to call potentially blocking I/O from within the context of the Event Dispatching Thread. This is NEVER a good idea, anything that will block the EDT will stop (amongst other things) repaint requests and stop the EDT from processing mouse and keyboard events...

All interaction with the UI (creation and modification) should be done from within the context of the EDT.

I would suggest you take a look at Concurrency in Swing for some background...

In your case, you are going to need some kind of Thread or background worker that is capable of sending and receiving data via your socket. This would allow you to queue out going messages and process the results without blocking the EDT.

But how this is actually implemented will come down to exactly what you requirements are...

Upvotes: 3

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Issues:

  • Why the static method? You should avoid all statics unless there's a good reason for them, and there isn't one here.
  • You don't mention how you are handling your threading, and in all likelihood this is probably causing your problems. Are you using a SwingWorker for background thread creation? Are you taking care to make all Swing calls on the Swing event thread?
  • You state that you're having a JButton use a MouseListener which is not good practice. JButtons were built to best respond to ActionListeners. This will trigger the visual changes to the JButton's state, and will also allow you to disable the action by disabling the JButton. I recommend that you read the Oracle Swing Tutorial Button section for more details on this.

Upvotes: 3

Related Questions