Setting the icon for a MessageDialog causes issues - java

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.

Related

How to create a Lottie Alert Dialog in Java

I've tried searching for the Java tutorial regarding creating my LottieAlertDialog, but I can't find any. Everywhere it's in Kotlin, but I need the Java code as my project is in Java.
I've tried creating my LottieAlertDialog in this way:
LottieAlertDialog.Builder alert=new LottieAlertDialog.Builder(context,DialogTypes.TYPE_CUSTOM,
"social.json") //Here social.json is inside assets folder
.setTitle("Social")
.setDescription("social");
alert.build();
But the dialogbox doesn't show, when I run the app. To check whether my alert dialogbox was being created or not I tried testing it by printing the description set in the dialog in a Toast:
Toast.makeText(context,alert.getDescription(),Toast.LENGTH_SHORT).show();
The toast works and its showing "social"! That means the dialog is being created. But unfortunately it doesn't show in my app. What do I do? I've implemented all the dependencies as shown in the below link:
lottiealertdialog
Ok, after much hankering, I finally came to the solution. It's
alert.build().show();
The thing is not related to Kotlin or Java as such, you need to show the dialog once you have built it. So far your code is correct. You just need to show it further like this
LottieAlertDialog.Builder alert = new LottieAlertDialog.Builder(context, DialogTypes.TYPE_CUSTOM,
"social.json")
.setTitle("Social")
.setDescription("Social")
.build()
.show();

Create jDialog Swing with a message of error with image

I'm new of Java and I want to do a jDialog that is opening when I push a button in the main JFrame and show a message of error in this way:
I can't put the image in another way in NetBeans? I create in the source package a directory with the image and a try much thing:
jDialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
jDialog1.add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResourceAsStream("/img/error_button.png")))));
jDialog1.pack();
jDialog1.setLocationByPlatform(true);
jDialog1.setVisible(true);
Is there a simple way to do this?
We can use an option pane for this. It includes its own icons according to the type of message (and look and feel).
Exception e = new Exception("Error!");
JOptionPane.showMessageDialog(f, e, e.getMessage(), JOptionPane.WARNING_MESSAGE);

Random Image in Java Dialog Popup Boxes

Random images keep appearing in all of my my Java popup boxes: The "inKeep editor" should not be there... the 'inkeep editor' image is a file called 'icon' and is used for the apple dock.
String response = JOptionPane.showInputDialog(Home.toolbar, "Please confirm your password to make changes", "Confirm Password", JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog(Home.toolbar, "Wrong Password", "Verify Account", JOptionPane.ERROR_MESSAGE);
I'm setting this image as the dock icon (mac) but not for pop boxes (to my knowledge).
Application application = Application.getApplication();
Image image = Toolkit.getDefaultToolkit().getImage("src/resources/icon.png"); // Sets apple icon image
application.setDockIconImage(image);
Does anyone know how to get rid of these images in the dialogs? The big red (!) is intended but not the inKeep editor over it.
This is the expected behavior -- a feature. When you give your application an icon, it will display it on top of the default warning/error icons. This reinforces with the user that the dialog that just popped up is from your application and not another or the OS.
For example, here how it looks using the default Java app icon:
To add a custom icon, you can do something like this:
ImageIcon icon = new ImageIcon(...);
JOptionPane.showMessageDialog(null, "", "", JOptionPane.ERROR_MESSAGE, icon);

Selenium Webdriver screenshots do not show driver errors

I'm creating screenshots in my testcases with selenium webdriver, and while these indeed show what is visible in my web application, it doesn't show popups created by the browser.
I have found that in IE in some cases, my app triggers a JS debug popup in IE. This is of course an error and breaks the rest of my test, but the screenshot does not show the error. I presume this is because it's an IE native popup, rather than one trigger by my application.
Is it possible to get this included in the screenshots? I was thinking of maybe creating the screenshot with Robot#createScreenCapture() but obviously that wouldn't show anything useful if the browser is minimized.
So, a few possible solutions:
- can you detect if an error message pops up in a browser
- is it possible to maximise/focus the browser while running?
- can you take screenshots from selenium with the popups showing?
Selenium will take a screenshot that represents the rendered DOM that the browser shows the end user by intercepting the rendered image that the browser is displaying and taking a copy of it.
It does not take a desktop screenshot so the screenshots shown will not show anything covering the browser window. JavaScript alerts are not part of the rendered DOM so you will not see these in Selenium screenshots.
Maybe because the driver is not with the Alert/Window active?
You could try something like this:
private void CheckForOtherWindows()
{
//Check for any other window open
if (driver.WindowHandles.Count > 0)
{
foreach (string window in driver.WindowHandles)
{
driver.SwitchTo().Window(window);
TakeScreenshot();
}
}
//Check for alert window
try
{
driver.SwitchTo().Alert();
TakeScreenshot();
}
catch
{
//Nothing
}
}
This is not tested, not sure if works. Just giving the idea. :)
Edit:
To maximize the window is easy:
driver.Manage().Window.Maximize();
Hope it helps.
You can use this as below:
FileUtils.copyFile(((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE), new File("E:\\ScreenShot\\screenshot.png"));

How to set alert instead of showing unhandled exception in J2ME?

I am designing an application which shows unhandled exception due to lot of reason. So I want my application to show alert in catch block instead.
You could do something like this:
private Alert alert;
private Display display;
// Obtain display with display = Display.getDisplay(this); in consturctor
catch(Exception e) {
alert = new Alert("Error occurred", "Message: " + e.getMessage(), null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, form);
}
Hope this helps.
I think you can just put the alert handling in the catch block:
catch(Exception e) {
// create new alert and
}
The problem I think this guy is having, is that the exceptions are appearing seemingly randomly, i.e. he won't know which piece of code is throwing it.
Some J2ME handsets (e.g. Nokias) have a habit of showing the exception name to the user in an alert, while others (e.g. Sony Ericssons) silently swallow them.
You need to identify all the points at which code can be executed (i.e. all the threads you're creating, and all the MIDP lifecycle methods that the framework could be calling for you), and wrap all of those in try/catch blocks, to ensure that no exceptions can be shown in this way.
That will probably slow your code down a lot though, so you should get to the bottom of what causes these exceptions to appear, and fix the problem!

Categories