2016-07-06 10 views
0

スレッドに関する質問があります。 Excelスプレッドシートを読み込んでデータをデータベースに保存する方法があります。しかし、このメソッドはまた、セルの内容が期待値であることをチェックし、そうでなければ、最も適切なオプションを選択するためにフレームを呼び出さなければなりません。コードが書かれているので、ユーザーがオプションを選択するためにフレームが開いているのを待っていないので、最初のループに戻ります。 (ポストのために、私は、コードの一部を省略している、長すぎるではないでしょう、重要なものだけ残して)コードの 以下の部分:今スレッドを終了して前のコードに戻す方法

public HashMap<String, Historico> importaDadosDoExcel(){ 
    HashMap<String, Historico> mapa = new HashMap<String, Historico>(); 
    HSSFCell cell= null; 
    HSSFRow row = null; 
    Historico update = new Historico(); 
    int rowsCount = 0; 
    String[] statusStr = {"Aguardando Boleto", "Aguardando Lançamento", "Em Workflow","Liberado para Tesouraria", "Pago", "Outros"}; 
    String aux; 
    for (int i = 1; i <= rowsCount; i++) { 
     cell = row.getCell(ActvUtils.u.devolveNumColuna("D")); 
     aux = ActvUtils.u.devolveCampoLido(cell); 
     if (Arrays.asList(statusStr).contains(aux)) { 
      update.setStatus(aux); 
     }else{ 
      //Here, I would like the frame was called (passing as a parameter the value read in the cell) to which the user then chooses the best option, then, that choice was setted in the object. 
      Runnable runnable = new EscolheStatus(aux); 
      Thread thread = new Thread(runnable); 
      thread.start(); 
      //Here, I would like to read the option that the user chose on a string, some like: 
      // String str = runnable.getStatus(); 
     } 
    } 
    return mapa;  
} 

そして、私のフレームクラス:

public class EscolheStatus extends JFrame implements ActionListener,Runnable{ 


private String[] statusStr = {"Aguardando Boleto", "Aguardando Lançamento", "Em Workflow","Liberado para Tesouraria", "Pago", "Outros"}; 

private JRadioButton boleto; 
private JRadioButton lancamento; 
private JRadioButton workflow; 
private JRadioButton tesouraria; 
private JRadioButton pago; 
private JRadioButton outros; 

private String status; 
private String statusEncontrado; 

public EscolheStatus(String statusEncontrado){ 

    this.statusEncontrado = statusEncontrado; 

    boleto = new JRadioButton(statusStr[0]); 
    boleto.addActionListener(this); 

    lancamento = new JRadioButton(statusStr[1]); 
    lancamento.addActionListener(this); 

    workflow = new JRadioButton(statusStr[2]); 
    workflow.addActionListener(this); 

    tesouraria = new JRadioButton(statusStr[3]); 
    tesouraria.addActionListener(this); 

    pago = new JRadioButton(statusStr[4]); 
    pago.addActionListener(this); 

    outros = new JRadioButton(statusStr[5]); 
    outros.addActionListener(this); 

    ButtonGroup group = new ButtonGroup(); 
    group.add(boleto); 
    group.add(lancamento); 
    group.add(workflow); 
    group.add(tesouraria); 
    group.add(pago); 
    group.add(outros); 

    JPanel radioPanel = new JPanel(); 
    radioPanel.setLayout(new GridLayout(6, 1)); 
    radioPanel.add(boleto); 
    radioPanel.add(lancamento); 
    radioPanel.add(workflow); 
    radioPanel.add(tesouraria); 
    radioPanel.add(pago); 
    radioPanel.add(outros); 

    radioPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Status '" + statusEncontrado + "' não reconhecido. Escolha:" )); 
    setContentPane(radioPanel); 
    pack(); 
    this.setSize(350, 200); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setLocationRelativeTo(null); 
    this.setVisible(true); 
    teste(); 

} 

@Override 
public void actionPerformed(ActionEvent ev) { 
    Object opcao = ev.getSource(); 

    if(opcao.equals(boleto)){ 
     status = statusStr[0]; 
    }else if(opcao.equals(lancamento)){ 
     status = statusStr[1]; 
    }else if(opcao.equals(workflow)){ 
     status = statusStr[2]; 
    }else if(opcao.equals(tesouraria)){ 
     status = statusStr[3]; 
    }else if(opcao.equals(pago)){ 
     status = statusStr[4]; 
    }else if(opcao.equals(outros)){ 
     status = statusStr[5]; 
    } 

    this.dispose(); 
} 

public String getStatus() { 
    return status; 
} 

public void setStatus(String status) { 
    this.status = status; 
} 

@Override 
public void run() { 
    new EscolheStatus(statusEncontrado); 

} 
(それは、ユーザ入力を要求するスレッドを停止するのみの場合)ここ

}

+0

'Thread.join'を使用しますか? – Mena

+0

'wait()'と 'notify()'メソッドを見てください。基本的に 'wait()'を使ってスレッドに何らかの "イベント"と "notify()"を待って、イベントが起こったことを通知します。 EDTはワーカースレッドを開始し、ワーカースレッドはExcelを読み取り、ユーザー対話が必要なときにダイアログを開き(EDTで処理されます)、ダイアログが閉じられるのを待ちます。 – Thomas

+4

スレッドを使用しない別のオプションは['JOptionPane'](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html)です。 – MasterBlaster

答えて

0

は、MasterBlasterが提案ものの例は次のとおり

Thread worker = new Thread(new Runnable() {  
    @Override 
    public void run() { 
    long start = System.currentTimeMillis(); 

    while((System.currentTimeMillis() - start) < 10000) {   
     System.out.println("opening dialog"); 
     String result = JOptionPane.showInputDialog("Tell me something!"); 
     System.out.println("user entered: " + result); 
    }   

    System.out.println("finished"); 
    } 
}); 

worker.run(); 

これはダイアログを開き、スレッドが少なくとも10秒間実行されるまで何度も入力を待機します。

あなたのニーズに適応できるはずです。もちろん

、複数の(労働者)のスレッドが通信する必要がある場合は、両方のスレッドを並列に実行する必要があり、もう1つは、単にいくつかのタスクを完了しなければならない場合は、他のスレッドが終了するか、wait()notify()を待つThread.join()を使用することができますし、続けてください。

// String str = runnable.getStatus();は、まだ実行中のスレッドに関する情報を取得することを意味します。その場合、共有オブジェクトを使用して書き込みと読み取りを行うことができます(同期が必要な場合があります)。

+1

信頼できる結果を得るには、このコード行 'String result = JOptionPane.showInputDialog("何か教えてください! ");がイベントディスパッチスレッドで呼び出される必要があります。 –

+0

@AndrewThompsonあなたはおそらく正しいでしょう。私は長い間スイングをしていませんでしたが、どうやってそれについて行っていますか?私が誤解していないなら、スレッドを同期させるためにwait/notifyを使わなければならないでしょうか? – Thomas

+0

私はまだ質問を理解していません。私は現在のコードのMCVEを見るのを待つでしょう。 –

関連する問題