0

私は2つのメソッドを持ついくつかのクラスを持っています。最初の方法は確認ダイアログの呼び出しで、2 番目の方法は完了ステータスを待機するスレッド リスナーです。リスナーが実行されたら、確認ダイアログを閉じたい。出来ますか?

私のコード:

public class NewNetworkGame implements ThreadListener {

ReadMsg read;
Network net;
// my dialog
JOptionPane cancelDialog;

boolean accepted = false;
boolean readStopped = false;

public NewNetworkGame(Network net) {
    this.net = net;
    read = new ReadMsg(this.net);
    ThreadHandler rm = read;
    rm.addListener(this);
    rm.start();

    JPanel cancelConn = new JPanel();
    cancelConn.add(new JLabel("Waiting for host response..."));

    // showing dialog
    int result = cancelDialog.showConfirmDialog(null, cancelConn, "Host response", JOptionPane.CANCEL_OPTION); 
    // client clicked on cancel option while thread is still reading host response       
    if (result == JOptionPane.CANCEL_OPTION && !accepted && !readStopped) {
        net.sendReject();   
    }

    if (!readStopped) {
        read.interrupt();
    }

}

@Override
public void notifyOfThreadComplete(Thread thread) {

    readStopped = true;

    if (net.getAcceptMsg().equals(read.getStr())) {
        accepted = true;
    }

    // closing dialog
    cancelDialog.setValue(JOptionPane.CANCEL_OPTION);
    // generates: java.lang.NullPointerException
}

}

私はリスナーハンドラーjava.lang.NullPointerExceptionから取得しています。cancelDialog.setValue(JOptionPane.CANCEL_OPTION)確認ダイアログを正しく閉じる方法を教えてください。

ソリューションで更新します。 作業コード:

public class NewNetworkGame implements ThreadListener {

ReadMsg read;
Network net;

boolean accepted = false;
boolean readStopped = false;

final JDialog dialog = new JDialog();


public NewNetworkGame(Network net) {
    this.net = net;
    read = new ReadMsg(this.net);
    ThreadHandler rm = read;
    rm.addListener(this);
    rm.start();


    JPanel cancelConn = new JPanel();
    cancelConn.add(new JLabel("Waiting for host response..."));
    Object[] options = {"Abort"};

    JOptionPane.showOptionDialog(dialog, cancelConn, "Host response", JOptionPane.CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);        

    System.out.println("clicked");

    if (!accepted && !readStopped) {
        System.out.println("aborted");
        net.sendReject();   
    }

    if (!readStopped) {
        read.interrupt();
    }

}

@Override
public void notifyOfThreadComplete(Thread thread) {

    readStopped = true;

    if (net.getAcceptMsg().equals(read.getStr())) {
        accepted = true;
    }

    dialog.setVisible(false);

}

}
4

1 に答える 1

0

このコードのサンプル行を試してください

    @Override
    public void notifyOfThreadComplete(Thread thread) {

        readStopped = true;

        if (net.getAcceptMsg().equals(read.getStr())) {
            accepted = true;
        }

        // closing dialog
         if(cancelDialog != null)
             {
        cancelDialog.setValue(JOptionPane.CANCEL_OPTION);
         }

}
于 2013-05-04T15:57:34.037 に答える