I want to create a pop up box that is activated when I click a "Check" button. What it does is ask the user if they are sure about what they requested. If yes, it does the request. If no, it goes back to it's normal state.
I know what I said is a bit ambiguous but I want to create various types of pop up boxes.
So I was wondering is there a website that has generic pop up boxes with the code given?
I just need a simple code which I can expand on.
I think JOptionPane is what you want.
showConfirmDialog: Asks a confirming
question, like yes/no/cancel.
showInputDialog: Prompt for some
input.
showMessageDialog: Tell the
user about something that has
happened.
showOptionDialog: The Grand
Unification of the above three.
A small example to get a Yes-No popup like you asked for would be:
if (JOptionPane.showConfirmDialog(null, "Are you sure about your request?", "Request",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
== JOptionPane.YES_OPTION)
{
//Do the request
}
else
{
//Go back to normal
}
This solution, however, only works if you are using Swing.
You shoudl check JDialog to create your custom message dialogs or you can use standard message dialogs in JOptionPane.
Related
I'm trying to use a while loop for email validation in JFrame (An email has to have "#" and "com" in it). So far I have:
while(!emailInput.getText().matches(".*[#com].*")) {
if (emailInput.getText().matches(".*[#com].*")) {
break;
} //from if
}
if((!emailInput.getText().matches(".*[#com].*"))) {
JOptionPane.showMessageDialog(null, "Your email does not seem to be valid. It should be in the form of myemail#myemail.com. Please try again!");
}
Of course, the while run will be an infinite one - once I tested the program by entering a random email (say, "test"), the condition will always be false. I dare not to put show the dialog (saying that the email doesn't seem right) in the while loop because it will also show the message infinite number of times.
But that is a little bit counterintuitive - since I want the user to keep entering the email until he gets it right (break in while loop).
I have tried to use only if statement but it seems that my if code only works once - so I'm counting on while loop. However, is there any way to fix this, even without using while loop? I am using this for password validation and I am having trouble as well. I am open to any suggestion.
As Sebastian suggested, the solution is to use a button, get rid of the while loop, and just use an if statement. The reason is that if the user clicks on it, the program would have checked the validation - and it checks the validation whenever the user clicks on the button. The process ends when the user enters a valid email and clicks on the button for the final time.
I am creating a log in form in Java. I have already completed the entire structure of the program, as well, I have designed it's purpose and perfected it's functionality. However, now I am focusing on styling the program. I'm proud of it as it is and it's fine if I don't add this feature, however I really would like to and cannot discover how.
In summary, when a username is typed into the login form, I have a void that runs after the JTextField loses focus. This void searches for possible invalid characters such as spaces. I can successfully change the color of the border on my JTextField, and other attributes I wish to change, however I also would like to have a small dialogue box to hover over the JTextField to say what specifically is wrong with what was typed. (e.g. "Your username cannot contain spaces!").
Ideally, it would be a rectangle that could simply be filled with text, and only appear when the username is deemed incorrect, and be able to disappear when it is fixed ( I can handle the appearance and disappearance most likely, I just need help with creating this box thing ).
Is there any such thing as like a "JHoverBox" or something that I could add to my JTextField?
Well, you can have, as Gene said, a label that's normally empty near the text field, but you can have it coloured like the background colour. This will make the user not able to see it, and when you detect something wrong, you can change the colour and add the text. Simple!
In my (pure) E4 App I want to force the User to enter something in a Text Field before he can move on.
Currently, if nothing has been entered into the Text Field and the focusLost Event is triggered, I reset focus to the Text Field.
Via a ModifyListener I check, if the entered String equals "" and if yes, a fake tooltip is displayed, telling the User to enter something into the Text Field.
The Problem is, if I have two Parts on a PartStack and the Text Field is on my first Part, the User is still able to trigger a Part switch and work on the other Part without having to enter something into the Text Field on the first Part first.
How is it possible to prohibit the User from switching between these Parts, as long as nothing is entered in the Text Field on the first Part?
I donĀ“t want to hide Part 2, the App should still look the same, the user should just not be able to do anything until something has been entered into the Text Field.
If you don't want to hide Part 2, then disable the widgets in that part instead. And display a message that asks the user to provide the necessary input back in Part 1. If the user does, hide that message and enable your widgets.
For example:
if (!isInputSatisfactory())
{
displayMessage("Invalid input");
myButton.setEnabled(false);
}
else
myButton.setEnabled(true);
I just programmed an interface about user registration. For delete user part, when i click delete button, the program will show a box asking "are you sure about deleting the user?" If i choose yes, then how can i program to show a showDialogMessage to tell the user the user has been deleted?
Just pitching here, but if you are using swing, look into JOptionPane especially the showMessageDialog methods.
JOptionPane.showConfirmDialog(Component parentComponent, Object message) This is for getting confirmation from user.
JOptionPane.showMessageDialog(Component parentComponent, Object message) this is for showing that the user has been deleted.
Since your question is incomplete, answering in the contrast of Swing
int i=JOptionPane.showConfirmDialog(null,"Are you sure about deleting the user?");
if(i==0) JOptionPane.showMessageDialog(null,"User deleted");
Confirmation box return 0 if chosen is yes, 1 if chosen is no and 2 if chosen is cancel
Go through JOptionPane you will get better idea.
I am a student at ISU. I was working on a homework assignment where I wanted to get text that was selected (highlighted) in a TextArea to appear in a JOptionPane dialog. I tried many of the methods for JOptionPane, but I could not get any of them to place the text the user selected in the input field of the dialog.
I guess that I could make a one element String array and pass this to the JOptionPane constructor listed.
JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options, Object initialValue)
This is not way I wanted to implement the JOptionPane. Please give me any help you can. Thanks and there is no hurry as the assignment has been handed in.
This tutorial shows how to use JOptionPane and how to get input from the user.
Read it to see if works for you or not.
How to make dialogs
I guess that I could make a one element String array and pass this to the JOptionPane constructor listed.
Sounds correct to me.
This is not way I wanted to implement the JOptionPane.
What's wrong with that? Or how do you want to implement it?
Take a look at that article perhaps you could manage to write something like:
JOptionPane.showMessageDialog(frame, getSelectedTextFrom( someTextArea ) , "Message");
Where the method:
String getSelectedTextFrom( JTextArea )
Will return... well the selected text from the text area... :)