Multi Processing / Multithread is not working

3

I want to try to solve the problem of when the client can not connect to the server (connection failure, server down, etc.).

In my main class is the connection to the server and I want that in case of giving error caught by try ... catch(ConnectException ex) throw a JDialog to inform the client that is not yet connected ...

JDialogisnotaJDialogobject,butitisaJDialogobject.

Code to connect to the server:

public Socket connect() {
    try {       

        this.socket = new Socket("localhost", 5555);
        this.output = new ObjectOutputStream(socket.getOutputStream());

    } catch (ConnectException ex) { 
                waitinng();//crio o frame mas ele só aparece quando o servidor está conectado
                System.out.println("tenta conectar");
                connect();//volto a chamar a função até que se consiga conectar
        Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnknownHostException ex) {
        Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
    }
    return socket;
}

To display JDialog I've already tried:

Create a normal class, but it did not work;

Create a thread that creates JDialog, also did not give

Now finally I'm trying with the swingworker, but with some problems ...

Questions:

  • Is swingWorker the way to go?
  • What should I enter as input parameters SwingWorker<Integer, Integer>
  • Is it in doInBackground that I should create JDialog or the builder?

EDIT I have serious doubts if my problem is in the multithread or the way java handles what I want to do ...

Outline:

I do not know if this edition helps ...

    
asked by anonymous 04.11.2014 / 12:11

1 answer

1

You can do with Thread, I made a simple example has the JFrame the main thread and a button that when you click starts another Thread that displays the jDialog you can by that your code inside the run () method and pass by reference the So, you need all the code needed to run in parallel, just creating the jDialog in a Thread will not work

Frame Class - Main Thread

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class Frame extends JFrame {

    private JButton botao;
    private JDialog dialog;

    public Frame() {

        botao = new JButton("OK");
        botao.addActionListener(new BotaoEvento());
        this.add(botao);
        botao.addActionListener(null);
        this.setSize(400, 400);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
    }

    public class BotaoEvento implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            Thread thread = new Thread(new Dialog(dialog));
            thread.start();
        }

    }

    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setVisible(true);
    }

}

Dialog class - Extra Thread

import javax.swing.JDialog;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class Dialog implements Runnable {

    JDialog dialog;

    public Dialog(JDialog dialog) {
        this.dialog = dialog;
    }

    public void run() {
        dialog = new JDialog();
        dialog.setSize(200, 200);
        dialog.setLocationRelativeTo(null);
        dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
        //operações que você deseja executar
    }

}
    
06.11.2014 / 04:24