2017-01-10 8 views
0

저는 기본적으로 우편 주소에 대해 붙여 넣은 텍스트의 각 줄을 확인하고 데이터베이스의 현지화를 확인하는 작은 응용 프로그램 인 Java로 구축하고 있습니다.처리 중에 내 진행 표시 줄 업데이트

모두 정상적으로 작동하지만 확인 프로세스에 시간이 오래 걸릴 수 있으며 줄을 확인할 때마다 진행률 표시 줄을 추가하여 값을 업데이트하려고합니다.

이 주제에 대한 다른 스레드를 읽었음에도 불구하고 진행률 표시 줄이 프로세스가 끝날 때만 업데이트되는 이유를 알 수 없습니다. GUI를 동결시키지 않는 과정에서 진행률 표시 줄을 분리해야한다는 것을 모았습니다. 그러나 실제로 도움이 될만한 방법을 설명하고 이유를 설명하는 것이 좋을 것입니다. 누구든지 도와 드릴 수있는 사람에게 미리 감사드립니다.

여기에 주 코드가 있으며 진행률 막대에서 진행 상황을 업데이트하려는 프로세스는 button2 수신기에서 시작한 프로세스입니다.

import javax.swing.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.IOException; 

import static sun.security.krb5.Confounder.intValue; 

public class mainform { 
private JButton button1; 
private JTextField txt_1; 
private JPanel panelmain; 
private JLabel outlab; 
private JTextField codepostal; 
private JTextArea paste_lignes; 
private JButton button2; 
private JProgressBar progressBar1; 
private JLabel progress; 




public mainform() { 

    button2.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      String[] lines = paste_lignes.getText().split("\\n"); 
      String resultQPV = ""; 
      int numLines = 0; 

      for (int i = 0; i < lines.length; i++) { 
       numLines++; 
      } 

      System.out.println(numLines); 

      for (int i = 0; i < lines.length; i++) { 
       String adresse = ""; 
       String CP = ""; 
       String[] cells = lines[i].split("\\t"); 
       float percent_d = ((float) i/(float) numLines) * 100; 
       System.out.println(i + " " + numLines + " " + percent_d); 
       int percent = (int) percent_d; 

       progressBar1.setValue(percent); 


       try { 
        Thread.sleep(50); 
       } catch (InterruptedException e1) { 
        e1.printStackTrace(); 
       } 


       for (int j = 0; j < cells.length; j++) { 

        if (CheckAdresse.identify_me(cells[j]) == "CP") { 
         CP = cells[j]; 
        } 

        if (CheckAdresse.identify_me(cells[j]) == "adresse") { 
         adresse = cells[j]; 
        } 

       } 


       String QPV = null; 
       String voie = null; 
       String numero = null; 

       if (adresse == "" || CP == "") { 
        QPV = "erreur sur la ligne"; 
       } else { 
        voie = ParserAdresse.voie(adresse); 
        numero = Integer.toString(ParserAdresse.numVoie(adresse)); 
        try { 
         QPV = CheckAdresse.Check(CP, voie, numero); 
        } catch (IOException e1) { 
         e1.printStackTrace(); 
        } 
       } 


       resultQPV = resultQPV + QPV + "\n"; 


       //System.out.println(i + " " + percent + " " + voie + " " + CP + " " + numero); 

      } 

      paste_lignes.setText(resultQPV); 


     } 
    }); 
} 


public static void main(String[] args) { 
    JFrame frame = new JFrame("mainform"); 
    frame.setContentPane(new mainform().panelmain); 
    frame.setSize(900, 400); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 

} 

}

+0

아래의 링크를 읽을 수 있습니다 당신 [예] (http://stackoverflow.com/questions/5691979/progressbar-doesnt-change-its-value-in-java?rq=1) – Visveswaran

+0

그 트릭을 했어! 이 링크를 가져 주셔서 감사합니다. 참조 된 스레드의 솔루션이 저에게 효과적이었습니다! – Elyss

답변

0

두 가지 작업을 원하지만 하나 개의 스레드에서 실행 때문입니다. 그래서 그들은 집행을 공유해야하고 평행선을 일할 수 없습니다.
필요한 것은 별도의 스레드에서 진행 막대를 실행하는 것입니다.
그래서 당신은

  • 하나의 스레드 진행률 막대 응용 프로그램
  • 에 대한

    • 하나 개의 스레드를해야합니다

    자바에서 스레드를 사용하는 방법 당신이 도움이 될 수 있습니다 여기 https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html