スレッドに関する質問があります。 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);
}
(それは、ユーザ入力を要求するスレッドを停止するのみの場合)ここ
}
'Thread.join'を使用しますか? – Mena
'wait()'と 'notify()'メソッドを見てください。基本的に 'wait()'を使ってスレッドに何らかの "イベント"と "notify()"を待って、イベントが起こったことを通知します。 EDTはワーカースレッドを開始し、ワーカースレッドはExcelを読み取り、ユーザー対話が必要なときにダイアログを開き(EDTで処理されます)、ダイアログが閉じられるのを待ちます。 – Thomas
スレッドを使用しない別のオプションは['JOptionPane'](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html)です。 – MasterBlaster