Rumen
Rumen

Reputation: 137

I can't disable with setEnabled(false), the button is not disabled in correct phase

I have Selenium WebDriver callSe.test(); + JFrame. Here is the constructor of frame:

public AutoFrame() {
    textFieldVersion.setColumns(10);
    textFieldUrl.setColumns(10);
    textPaneIsBuildCorrect.setBackground(UIManager.getColor("menu"));
    btnRun.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            btnRun.setEnabled(false);
            getEnteredVer();
            CheckBuildVersion callSe = new CheckBuildVersion();
            callSe.test();
            textPaneIsBuildCorrect.setText(callSe.getIsBuildCorrect());
            if (textPaneIsBuildCorrect.getText().contains("The Build is correct!")) {
                textPaneIsBuildCorrect.setForeground(Color.blue);
            }
            else {
                textPaneIsBuildCorrect.setForeground(Color.red);
            }
            textPaneCurrentBuild.setText(callSe.getBuild());
        }
    });
    initGUI();
}

So I expect after btnRun.setEnabled(false); the button to be disabled, but is not. It's only marked and the Frame just kind of freeze. The button becomes non clickable (false, disabled) only when whole constructor finish. Why is happen like that? I want, when I press the button to be disabled, then I will put to be enable. Maybe I have to use modal dialog with PleaseWait?

Upvotes: 0

Views: 82

Answers (1)

Madhan
Madhan

Reputation: 5818

Run the Selenium Task in a separate Thread.

   Thread thread = new Thread() {
        public void run() {
            //your selenium actions
        }
    };
    thread.start();

For your case

 btnRun.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        btnRun.setEnabled(false);
        getEnteredVer();
        Thread thread = new Thread() {
            public void run() {
                  CheckBuildVersion callSe = new CheckBuildVersion();
            callSe.test();
            textPaneIsBuildCorrect.setText(callSe.getIsBuildCorrect());
            if (textPaneIsBuildCorrect.getText().contains("The Build is correct!")) {
                textPaneIsBuildCorrect.setForeground(Color.blue);
            }
            else {
                textPaneIsBuildCorrect.setForeground(Color.red);
            }
            textPaneCurrentBuild.setText(callSe.getBuild());
            }
        };
    thread.start();

    }
});

Upvotes: 1

Related Questions