how to set jbutton icon for specific time to show when it is clicked and how to disappear the icon when time finished

故事扮演 提交于 2021-01-28 07:16:57

问题


how to code java for set timer to my button for set button icon when i clicked and some time delay for process while my button icon has to show then the icon have to set null for time end.

i have tried following way but it is work when i am not to clicked the button

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         


  timer = new Timer(5000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(chromeShown) {
                    jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/r/ajaxloading.gif")));
                    chromeShown = true;
                } else {
                    jButton3.setIcon(null);
                    chromeShown = false;
                }
            }
        });
        timer.start();

        this.getContentPane().add(JButton);
        this.setVisible(true);

回答1:


Based on my understanding of your problem, your logic is a little skewed, the actions should following along something like

Button Clicked -> Icon Changed -> Timer Started ...(waiting)... -> Timer triggered -> Icon Changed.

At the moment, you're trying to change the initial state of the icon in the Timer, which doesn't make sense. I think you want to do something more like this...

click.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        click.setEnabled(false);
        click.setText("I'm running >>");
        Timer timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                click.setText("I be done");
                click.setEnabled(true);
            }
        });
        timer.start();
    }
});

Basically, when the button is clicked, this sets the button's text and disables the button (so you can't click it again) and the after 1 second, it changes the text and enables the button



来源:https://stackoverflow.com/questions/41950734/how-to-set-jbutton-icon-for-specific-time-to-show-when-it-is-clicked-and-how-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!