在特定时间段内自动在按钮上执行Click Event

时间:2011-12-20 11:59:42

标签: swing jbutton

您好我正在创建一个在JeditorPane中打开特定网址的应用程序。现在我希望它自动重新加载JEditorpane内容。我执行点击按钮的代码是

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 个答案:

答案 0 :(得分:1)

在Timer中重用Action / -Listener

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