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.
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.
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 was able to figure out getting input from the user and displaying the final product by myself, but I would like to add a "busy" message that displays at a certain point and then closes at a certain point in the code.
Hopefully it'll be easier to explain by showing it in my code. It's for a sorting algorithm and the sorting times obviously will vary.
public static void main(String[] args) throws Exception{
//Code that doesn't matter for this question...
String userInput = JOptionPane.showInputDialog("Please enter the amount of input you would like to test (integer): ");
// Code that doesn't matter for this question....
//
// Open dialogue box here that says, "Busy sorting" and stays open
//
long startTime = System.currentTimeMillis();
sort(shorts);
long endTime = System.currentTimeMillis();
//
// The dialogue box closes here without the user having to press anything
//
// This displays the final output.
JOptionPane.showMessageDialog(null, error + "The runtime for " + input + " short integers is " + (endTime-startTime) + " milliseconds.", "Final Runtime", JOptionPane.PLAIN_MESSAGE);
}
Hopefully the question is understandable.
What you will need is to use the SwingWorker class to run your long task on a separate thread from the GUI. While this is running displaying a JProgressBar or something similar would be ideal.
http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html
The JFrame is probably what you are looking for.
Ah yes, and keep in mind the important remark on threading in Swing by jzd. You'll want to read up on that too.
Read the section from the Swing tutorrial on How to Use Progress Bars for some examples.
Or maybe you would like to try using a Disabled Glass Pane.
I will probably not use JOptionPane. Since while displaying the busy message, the program will not be executing. You better create a more elaborate swing application with a text field and a button. When the user click on the button, the system display a busy message on the bottom of the windows.
As I mentioned in my question, you should check out the swing tutorial. It's really good, and really easy to follow.
JOptionPane is part of Java Swing, though, as others have said, it's probably not the best approach for displaying a busy state. As others have said, a progress bar might be a good option, depending on your needs, but you'll see other options when you go through the tutorial.
Good Luck!
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.
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... :)