0

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())));
}
...
andr
  • 15,970
  • 10
  • 45
  • 59

3 Answers3

3

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.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I use the static method since it is to be used under: public static void main(String[] args) { ... } – user1978786 Jan 15 '13 at 00:29
  • @user1978786: exactly -- you shouldn't do anything of the kind in the main method. The main method should do little more than set up your classes and display the GUI, that's it. You've got large problems with this project, I fear. – Hovercraft Full Of Eels Jan 15 '13 at 00:31
  • It's a lot simpler to use the voids as steps – user1978786 Jan 15 '13 at 00:33
  • @user1978786: in English please. In other words, I have no idea what you mean by "simpler to use the voids as steps". Please explain the details of just what you mean and the code not shown. – Hovercraft Full Of Eels Jan 15 '13 at 00:34
3

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...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

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.