Multi-processing/Multithread not working

Asked

Viewed 250 times

3

I want to try to solve the problem of when the client cannot connect to the server(connection failure, server below, etc).

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

inserir a descrição da imagem aqui

Something like this, Jdialog is created but is only designed when the operation is over, that is while the main thread tries to connect does not draw anything, only when it gets the connection does it draw both the Jdialog and the main frame.

Code to connect to 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 appear Jdialog already tried:

Creating a normal class, but it didn’t work;

Creating a thread that creates Jdialog, also failed

Now last I’m trying with the swingworker, but with some problems...

Questions:

  • SwingWorker is the path I must follow?
  • What should I insert as input parameters SwingWorker<Integer, Integer>
  • It is at Inbground that I must create Jdialog or the constructor?

EDIT I have serious doubts whether my problem is in multithreading or the way java processes what I want to do...

Sketch: inserir a descrição da imagem aqui

I don’t know if it helps this edition...

  • I asked a question more or less on the same subject, see if it helps you: http://answall.com/q/38078/14674

  • The program is not falling into any Exception ?

  • @Andrélizardo not, is not, the code is all working, inside doInBackground I created a for loop to make printf and these printfs shows on the console, but Jdialog does not (just draw at the end)

  • @Earendul may say so, but my problem is just before yours, how did you create Jdialog inside Stringworker? my big problem is, if I run Jdialog alone it works, but if I call it inside Stringworker Jdialog is only visible after the process of connecting to the server which is when the client form appears too...

  • You can answer your own question with the solution you found, ;)

  • @Earendul many apologies, the problem is STILL not solved, remains exactly the same, it seemed but I was wrong

Show 1 more comment

1 answer

1

You can do with Thread, I made a simple example has Jframe the main thread and a button that when you click starts another Thread that displays the jDialog you can put that your code within the run() method and pass by reference the Sockets, you need for all code needed to run in parallel, creating only 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
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.