2011-12-20 24 views
0

嗨我正在創建一個應用程序在swing中打開JeditorPane中的特定URL。現在我想它自動地重新加載JEditorpane內容。我執行點擊按鈕的代碼是Automaticlly在特定時間段內在JButton上執行點擊事件

loadButton = new JButton("Load"); 
     loadButton.addActionListener(new ActionListener() 
     { 

      public void actionPerformed(ActionEvent event) 
      { 
       try 
       { 
        // remember URL for back button 
        urlStack.push(url.getText()); 

        editorPane.setPage(url.getText()); 


       } 
       catch(Exception e) 
       { 
        editorPane.setText("Error: " +e); 
       } 

      } 

     }); 

我希望它每30秒執行一次。我怎樣才能做到這一點。

回答

1

重新使用操作/章24 -listener在定時器

Action loadAction = new AbstractAction("Load") { 
     public void actionPerformed(...) { 
      // do stuff 
     } 
} 
JButton loadButton = new JButton(loadAction); 
Timer timer = new Timer(30000, loadAction);