Joptionpane.showMessageDialog truncates title if its longer than message - java

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

Related

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.

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);

How to display Messagebox with Retry option in JAVA?

Scenario:
Pickup some data from an Excel file.
Search for it in a Text file.
If the data is not found, display a popup message box with a 'Retry' option.
User opens the Excel file and changes the value.
Click the 'Retry' button.
The Line which threw the error earlier should get executed again.
I need to know, how to display a message box with a 'Retry' option, clicking on which shall execute the line of code again.
Just place the code that pops up the box in a loop and continue the loop if the retry button was clicked. The response code from the jOptionPane tells you what button was popped.
this will give you an option pane with 4 buttons java C++ VB COBOL
String[] choices = {"Java", "C++", "<acronym title="vBulletin">VB</acronym>", "COBOL"};
int response = JOptionPane.showOptionDialog(
null
, "Which is your favourite programming language?"
, "Language Poll"
, JOptionPane.YES_NO_OPTION
, JOptionPane.PLAIN_MESSAGE
, null
, choices
, "None of your business"
);

While I was coding java using JOptionPane I couldn't understand below line

JOptionPane.showInputDialog(Component compt, Object o, String string, int i, Icon icon);
in the above line I didn't understood any parameters and also I provided string in the place of object
and it stills works. Can anybody explain correct procedure.
According to java docs
parentComponent - determines the Frame in which the dialog is displayed; if null,
or if the parentComponent has no Frame, a default Frame is used
message - the Object to display
title - the title string for the dialog
messageType - the type of message to be displayed: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
icon - an icon to display in the dialog that helps the user identify the kind of message that is being displayed

if statement not returning results

I'm sending a username and password to a website for authentication purposes, after all is said and done and I've retrieved the results from the server, I've placed the results in a variable called 'response' To this point everything is working correctly
response = sb.toString();
Toast.makeText(this,"Returned Value: "+ response,0).show();
The value seen in the above Toast is the value being returned by the php script. I've used both a valid user and an invalid user and the Toast displayed above shows the correct value (i.e. "Good Login" or "Login Failed") returned by the server. I want to test for those results so I can start the appropriate activity so I've put in some test "if" statements
if("Good Login".equals(response)){
Toast.makeText(this, "Registered User" + mUsername, 0).show();
}
if("Login Failed".equals(response)){
Toast.makeText(this, "Sorry You're Not A Registered Subscriber",0).show();
}
I'm getting nothing from either one.
I've also tried
if(response.equals("Good Login")){
Toast.makeText(this, "Registered User" + mUsername, 0).show();
}
if(response.equals("Login Failed")){
Toast.makeText(this, "Sorry You're Not A Registered Subscriber",0).show();
}
With the same results. Not sure what else to test for. Is there a better way to test for success or failure?
Thanks
Debug (or print) the exact value of the response variable.
It is likely that there are whitespaces, so you may need to have response = response.trim()
I would return an integer error code rather than some string to check the error response.
Make sure you are returning the correct case, otherwise use equalsIgnoreCase
The Java string equals function is fully case/spacing sensitive compare.
So if:response = "Good Login " or if it contains extra-spaces or non-printing characters then it will fail the test.
It would be a good idea to strip all whitespace, even the internal ones. Here's a SO question about doing just that. Also use String.equalsCaseInsesitive() when doing that actual comparison.

Categories