2012-09-26 2 views
0

클릭하면 GUI를 사용하여 다운로드 할 수있는 HTTP 링크를 시도합니다. 다운로드 시작 버튼이 다운로드를 시작합니다. 다운로드가 작동하지만 ActionListener에서 선언 된대로 다운로드가 시작될 때 레이블이 변경되지 않습니다. . 도와주세요.JAVA : JLabel 이벤트가 변경되지 않습니다.

여기 다운로드가 시작될 때와 완료 될 때 코드

라벨 변경해야합니다.

public class SwingGUI2 extends javax.swing.JFrame {  
private javax.swing.JLabel jLabel1; 
private javax.swing.JButton jButton1; 
private URLConnection connect; 

private SwingGUI2() { 
    setLayout(new FlowLayout()); 

    jLabel1 = new javax.swing.JLabel(); 
    jLabel1.setText(""); 
    add(jLabel1); 

    jButton1 = new javax.swing.JButton(); 
    jButton1.setText("Start Download"); 
    add(jButton1); 

    jButton1.addActionListener(new java.awt.event.ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      jButton1ActionPerformed(e); 
     } 
    }); 


    pack(); 
} 
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    jLabel1.setText("Download Started"); //NOT WORKING 
    try { 
     String DownURL = "http://www.number1seed.com/music/flymetothemoon.mp3"; 
     URL url = new URL(DownURL); 
     connect = url.openConnection(); 
     File file = new File("download"); 

     BufferedInputStream BIS = new BufferedInputStream(connect.getInputStream()); 
     BufferedOutputStream BOS = new BufferedOutputStream(new FileOutputStream(file.getName())); 
     int current; 
     while((current = BIS.read())>=0) BOS.write(current); 
     BOS.close(); 
     BIS.close(); 

     jLabel1.setText("Completed"); //NOT WORKING 

    } catch (Exception e) {} 
} 


public static void main(String[] args) throws ClassNotFoundException, InstantiationException { 
    try { 
     javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
    } catch (IllegalAccessException ex) { 
     Logger.getLogger(SwingGUI2.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (UnsupportedLookAndFeelException ex) { 
     Logger.getLogger(SwingGUI2.class.getName()).log(Level.SEVERE, null, ex); 
    } 

    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      SwingGUI2 gui = new SwingGUI2(); 
      gui.setVisible(true); 
      gui.setSize(300,200); 
      gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     } 
    }); 
} 

}

답변

1

당신은 작업이 수행되는 동안 동결하기 위해 GUI를 일으키는 원인이되는 이벤트 디스패처 스레드에서 다운로드를 실행하려고합니다.

다운로드 작업을 백그라운드 스레드로 실행하려면 SwingWorker을보십시오. 다른 사람은 아무 잘못이 발생하면 볼 수 없습니다,이 캐치에 예외를 삼키지 않도록주의하십시오 :

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    jLabel1.setText("Download Started"); 
    SwingWorker sw = new SwingWorker() { 
      public Object doInBackground(){ 
       try { 
        // ... 
        // Do the whole download thing 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       jLabel1.setText("Completed"); 
       return null; 
      } 
     }; 
    sw.execute(); 
} 

NB는 : 귀하의 actionPerformed 방법은 다음과 같이해야한다.

+0

정말 고마워, 내가 뭘 잘못하고 SwingWorker에 대해 알지도 못했는데 ... 큰 시간 Java 멍청 아! 이제 progressbar로 진행해 보겠습니다. – rohitpal

+0

Java에서 SWT에 해당하는 SwingWorker는 무엇입니까? – rohitpal

0

잘못된 이벤트 발송 스레드에서 다운로드를 실행하고 있습니다. 이렇게하면 EDT 스레드가 다운로드 이외의 작업을 수행하지 못하게됩니다.

별도의 스레드에서 다운로드를 수행하면 레이블이 업데이트되기 시작합니다.

관련 문제