I want to show a Message Dialog to the user informing him that my application is busy and that he has to wait.
JOptionPane.showMessageDialog(
null,
"Operation in progress",
"Waiting",
JOptionPane.INFORMATION_MESSAGE);
I create this simple message dialog but user can easy close this window. This message dialog must close only from my code.
Instead of using JOptionPane.showMessageDialog() to display the predefined dialog create a new JDialog object yourself and prevent user closing with setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE).
Related
After clicking OK, the 1st loop must be initiated and then I'll try to input the log-in again. TIA Edit: Supposedly, when I put a wrong log-in details, the message dialogue will prompt, and after clicking the "OK" button of message dialogue, I should go back attempting a log-in again. But the error appears, after clicking the "OK" button of message dialogue, it just goes on and prompt me another message dialogue. I can't go back to attempting a log-in.
joptionpane problem
if (e.getActionCommand().equals("Ok")){//Button for Action Listener
int attempt=0;
{
while(attempt<5){//Max attempt is 5
if ((txtF.getText().equals("user"))&&
(passF.getText().equals("pass"))){
ProjectJava_Main finalp2 = new ProjectJava_Main();
finalp2.setVisible(true);
this.setVisible(false);
}//Redirect to another class if true
else{//Increment,invalid login
attempt++;
JOptionPane.showMessageDialog(null,"INCORRECT LOGIN ");
JOptionPane.showMessageDialog(null,"Log-In attempt:" +""+attempt);
}
if (attempt==5){//If max 5 attempt, should exit
JOptionPane.showMessageDialog(null,"You reached the maximum
attempt!");
System.exit(0);
}
}//Error on this part can't debug
}
}
else if (e.getActionCommand().equals("Exit")){
System.exit(0);
}//Exit part
I need to have a sort of "blocked alert" that can't be dismissed by the user until some event occurs. So, I created the alert and removed all the buttons from it:
Alert waitingAlert = new Alert(Alert.AlertType.INFORMATION);
waitingAlert.setTitle("Proposta Inviata");
waitingAlert.getButtonTypes().setAll();
waitingAlert.setHeaderText("La proposta è stata inviata a " + trade.getPlayer2Name());
waitingAlert.showAndWait();
Then, I need to dismiss it when a particular event occurs. I just tried doing it this way:
waitingAlert.close();
But it doesn't work.
Edit
The panel is shown when an user wants to send a request to another user in the network that needs to be accepted or refused. The principle is that the UI gets blocked until the other user says if he has accepted or refused the request. The request is send through a remote method invokation through a ring network. When I receive the response I want to close dismiss the panel.
To have a dialog that can't be dismissed by the user, but wait on a process, you will need to use dialog.show() as noted by James_D.
However, without buttons, the dialog actually will not close and you need to force it.
//for example
dialog.getButtonTypes().add(ButtonType.CANCEL);
dialog.hide();
dialog.getButtonTypes().remove(ButtonType.CANCEL);
This will allow you to close a dialog which has no buttons.
I am having a problem with certain websites that cause my browser to prompt an alert when I try to switch to a different URL, or even close the browser. Some examples:
http://grooveshark.com/
http://www.dollardays.com/
In order to workaround the alert with Selenium, I need to switch to that alert, and then sometimes accept it and sometimes reject it (depending on the contents of the alert).
I wish to avoid solving this problem that way because:
I need to guess whether I should accept the alert or reject the alert.
Switching to the alert sometimes throws an exception, even though the alert is present.
What preferences do I need to set in the Firefox-Profile, in order to prevent the browser from issuing such alerts (or any other alerts for that matter)?
Answers in Java or Python will be highly appreciated.
Thanks
To my knowledge you can only disable that behaviour globally.
There is a preference called dom.disable_beforeunload. You should change its value to true. With Selenium, you can create a new customized Firefox profile:
FirefoxProfile customProfile = new FirefoxProfile();
customProfile.setPreference("dom.disable_beforeunload", true);
FirefoxDriver driver = new FirefoxDriver(customProfile);
As far as I know it's not possible to disable native browser events like alerts, so you'll just have to handle them better.
1) You should be able to use alert.getText() to make an informed decision on whether to accept or dismiss an alert.
:
try {
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
if ( alert.getText().contains("Are you sure you want to leave this page?")) {
alert.accept();
}
else if ( alert.getText().contains("Some other text which means you need to dismiss")) {
alert.dismiss();
}
else {
//something else
}
}
catch (Exception e) {
}
2) Use a WebDriverWait to avoid race conditions. See above
I don't think Firefox profile would feature disabling such specific elements, but you can hard-code some lines of static logic that would remain consistent across the test case/project.
Like Click on the main page automatically closes the pop-up frame/alert msg on grooveshark.com
#Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/#!/genre/Rap/1748"); //complete URL becomes http://grooveshark.com/#!/genre/Rap/1748
driver.findElement(By.linkText("more…")).click(); // clicks a hyper-link which opens up that frame/pop-up
driver.findElement(By.id("lightbox-outer")).click(); // clicks outside the opened-up frame, or simply clicks on the main page in background
}
lightbox-outer is the main page.
You can't disable the popups (alert), just do alert.accept() means clicking the ok button of alert modal or alert.dismiss() means clicking the cancel or close button.
The worst in this case is that you need to wait a certain time if you are not very sure that alert is going to be present or not.
If the alert is present as a result of event sucess( just like clicing the button, webpage asks for your confirmation ) you dont need to do wait wait.until(ExpectedConditions.alertIsPresent());
you can save the time by going to next step which is switching the drver to alert. i.e
Alert alert = driver.switchTo().alert();
if ( alert.getText().contains("Are you sure you want to leave this page?")) {
alert.accept();
}
else if ( alert.getText().contains("Some other text which means you need to dismiss")) {
alert.dismiss();
}
else {
//something else
}
}
just to be sure you can use a small waiting time, but yes it depends uponthe network speed to load the web page for the case that alert is present when webpage is loaded.
I'm writing simple GUI to my client server application. GUI client application worked OK but when I added simple Login Form before my client app, communication between client and server doesn't work. Here is the code which create my client GUI (the code is in the "Log in" button in Login Form - when I click the button Login Form is close and client app is open):
new testUI().setVisible(true);
dispose(); //close Login Form
If your login form is not modal, then method setVisible () will return instantly and you will dispose form before user will see it.
Make sure your login form extends JDialog and is created with modal=true parameter passed to constructor of JDialog class.
I'm trying to ask the user if he wants to retry a login process if the initial one has failed.
I do this using an AlertDialog. The problem is i'm going around in a circle. Let me explain:
I have the login method named Login which I call from the main activity. If this fails, I open an AlertDialog. The response from AlertDialog comes on the main thread. How do I make the AlertDialog dissapear before I call Login again?
Now I have something like this onCreate->Login->AlertDialog->ResponseHandler->Login. If I do this, the AlertDialog will never close because everything happens on the same thread.
Is there a way to send the response from the AlertDialog to the activity in a asynchronous way?
You can declare a Handler object as an anonymous inner class in your activity and on its reference call the sendEmptyMessage(0). in the handlerMessage() of the handler instance just dismiss the AlertDialog.
Here is a tutorial link which will help you more http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html