Why aren't my JOptionPane messages working? - java

I am writing a message dialog within an if statement. When I finish the message there are no errors. When I add an error message or any other message an error says "no suitable method" and "not applicable".
The code looks like this:
OptionPane.showMessageDialog(null, "Wrong! Try again", JOptionPane.ERROR_MESSAGE);

Thats because there is no suitable method. You need to specify title and message of the dialog. Look here
For example:
JOptionPane.showMessageDialog(null, "Wrong! Try again", "Title", JOptionPane.ERROR_MESSAGE);

Related

toast setGravity is not working it show normal default toast

i add the toast like this
Toast t = Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER,0,0);
t.show();
it is not working it shows normal default toast.

Setting the icon for a MessageDialog causes issues

I'm trying to set an Icon for a Message Alert popup, and I'm using the methods I've found online. However, they're causing issues where the image:
Doesn't even get set as the icon
Is way too oversized
Placed in the JPanel
The screenshot below shows what I mean:
Screenshot of Issue
And my code doesn't seem to have any issues:
final ImageIcon iconF = new ImageIcon(LOGIN.class.getResource("/resources/offline.png"));
JOptionPane.showMessageDialog(null, "ERROR: Please check your username and password then try again.", "Login Failed", JOptionPane.ERROR_MESSAGE, iconF);
I don't understand what's causing this mess of an issue when this seems to be the "answer" to my own question.

Joptionpane.showMessageDialog truncates title if its longer than message

JOptionPane.showMessageDialog(this, "short message", "Title has to be very longer than text, so long that title is cropped.", JOptionPane.ERROR_MESSAGE);
use null instead of this if u are testing directly in main method.
As written in the above code , if joptionpane title is larger than message it gets cropped ? is there a way to set its size ?
I could not post its screenshot . Run the single line of code in your main function

Handling alerts in Selenium

I have a scenario in which clicking on "Save" button can give three different alerts based on the data entered. It can give "Saved successfully", "User already registered" and "Username already exist" alerts. I have tried:
driver.findElement(By.id("dnn_ctr5995_View_btnsavesession")).click();
Thread.sleep(10000);
Alert alert = driver.switchTo().alert();
String A = alert.getText();
if (A == "Username already exist"){
System.out.println("Admission number already exist.");
alert.accept();
} else if (A == "User already registered"){
System.out.println("User already registered");
alert.accept();
} else if (A == "Saved Successfully."){
System.out.println("Saved Successfully.");
alert.accept();
}
But it is showing No Alert present exception. I have tried increasing the sleep time but still it shows the same exception.
I'm guessing you never actually call alert.accept() because you're using A == "User already registered" instead of "User already registered".equalsIgnoreCase(A). If you change the if conditions, that should fix your problem.
As per the Html shared, the id of the element is not "save". So the first line of the shared code should be modified a bit to achieve the output.
Change the element identification tag to :
driver.findElement(By.id("dnn_ctr5995_View_btnsavesession")).click();
or if the 'id' of the element is changing on every instance, then it can be changed to:
driver.findElement(By.xpath("//*[contains(#id,'_View_btnsavesession')]")).click();
Looks like your clicking is not actually clicking because the 'save' id is no like in the element.
Try to do it like this:
driver.findElement(By.cssSelector("[id*=save"])).click();
And then trying to catch the alert. Please note the '*' in selector.

Android Toast Messages not working

I'm developing a game via Andengine for Android. I have MainActivity class and GameScene class. I use Toast messages in GameActivity. And it is working.
Toast.makeText(this, " Hello World", Toast.LENGTH_SHORT).show();
So I wanna use Toast messages in GameScene class. But it doesn't work. Here is the code:
Toast.makeText(activity, " Hello World", Toast.LENGTH_SHORT).show();
I have to use "activity" instead of "this". But it doesn't work
why?
EDITED:
when I use second one, an error occurs.
LogCat:
http://s29.postimg.org/k8faj9mdj/Capture.png
You're trying to display a Toast in a background thread. You should do all your UI operations on the main UI thread.
The exception RuntimeException: Can't create handler inside thread that has not called Looper.prepare() can be a little cryptic for beginners but essentially it tells you that you're in a wrong thread.
To solve it, wrap the toast to e.g. runOnUiThread():
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(...).show();
}
});
There could be two reasons for your code to not work. It's ether your activity parameter is null or...
Short time after you showing the toast the activity is die, in that case it will kill the toast as well, to avoid this you can call activity.getApplicationContext() like in #Mehmet Seçkin answer.
use one of the following
Toast.makeText(getApplicationContext(), " Hello World", Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(),"please Create your Account First", Toast.LENGTH_SHORT).show();
Toast.makeText(GameActivity.this,"please Create your Account First", Toast.LENGTH_SHORT).show();
Use:
Toast.makeText(getApplicationContext(), " Hello World", Toast.LENGTH_SHORT).show();
or
Toast.makeText(activity.this, " Hello World", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
try this.
Since you asked why; i think you're giving an activity reference as a context to the Toast message, this is why it isn't working.
If you're trying to show a Toast message from outside of an activity, you could try :
Toast.makeText(activity.getApplicationContext(), " Hello World", Toast.LENGTH_SHORT).show();
or from the GameActivity
Toast.makeText(GameActivity.this, " Hello World", Toast.LENGTH_SHORT).show();
or from the MainActivity
Toast.makeText(MainActivity.this, " Hello World", Toast.LENGTH_SHORT).show();
Since You are calling it from the class. you need to get the context from the activity through the class constructor or else you need to use GetApplicationcontext().
Make sure the app you are testing has notifications turned on. That was my story and why toasts weren't working either. I had gone looking for a straight answer and it just happens that toasts are considered part of the notifications. Interesting stuff, I had no clue.
If you think your code is correct, try to close your emulator tab then open AVD manager, next wipe data, then restart. Or you can delete the current AVD and add a new one.

Categories