I have one JFrame where I enter parameters of a matrix, after those parameters being entered, the user is supposed to click a button to start a simulation. The problem is, the button is supposed to be clicked twice instead of once in order to open the JOption pane where a message is written. I just can't figure out why this happens. This is the function that is called from the simulation button's action performed function:
private void setMatrixParameters(){
if(tfTouristNumber.getText().equals("") || tfRowNumber.getText().equals("") || tfColumnNumber.getText().equals("") || tfMinimal.getText().equals("")){
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame, "All fields must be filled", "Error!", JOptionPane.ERROR_MESSAGE);
}
//check the matrix dimensions
else if(min + touristNumber > Integer.parseInt(tfRowNumber.getText()) * Integer.parseInt(tfColumnNumber.getText())){
int dimension= min + touristNumber;
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame, "Matrix dimensions too small. Need to be at least:" + dimension, "Enlarge matrix!", JOptionPane.ERROR_MESSAGE);
}
else{
this.touristNumber = Integer.parseInt(tfTouristNumber.getText());
int row = Integer.parseInt(tfRowNumber.getText());
int column = Integer.parseInt(tfColumnNumber.getText());
this.matrix = new Object[row][column];
this.min = Integer.parseInt(tfMinimal.getText());
}
}
Your problem in this case is that you are not checking the text field values in the second if-condition. The following should do the trick.
private void setMatrixParameters(){
if(tfTouristNumber.getText().equals("") || tfRowNumber.getText().equals("") || tfColumnNumber.getText().equals("") || tfMinimal.getText().equals("")){
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame, "All fields must be filled", "Error!", JOptionPane.ERROR_MESSAGE);
return;
}
// Read values here. (Should probably add a try-catch block around this, in case it can't be parsed as number)
int inTouristNumber = Integer.parseInt(tfTouristNumber.getText());
int inRow = Integer.parseInt(tfRowNumber.getText());
int inColumn = Integer.parseInt(tfColumnNumber.getText());
int inMin = Integer.parseInt(tfMinimal.getText());
//check the matrix dimensions
if(inMin + inTouristNumber > inRow * inColumn){
int dimension= inMin + inTouristNumber;
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame, "Matrix dimensions too small. Need to be at least:" + dimension, "Enlarge matrix!", JOptionPane.ERROR_MESSAGE);
}
else{
this.touristNumber = inTouristNumber;
this.matrix = new Object[inRow][inColumn];
this.min = inMin;
}
}
I'm having a hard time figuring out where I should put my ers++ or ers+=1. I've tried doing a for loop and do-while, it worked and reached the limit of 3, but it should also let the user input again and check the inputs again.
if (ers < 3) {
if (a.equals(SignupPage.ee) && b.equals(SignupPage.dd) && !(a.isEmpty()) && !(b.isEmpty())) {
System.out.println(a+" "+b);
tf = true;
JOptionPane.showMessageDialog(null, "Welcome!", "Payment Portal", JOptionPane.INFORMATION_MESSAGE);
ElectricityPage ep = new ElectricityPage();
ep.setVisible(true);
} else if (a.equals("Admin") && b.equals("javaness") && !(a.isEmpty()) && !(b.isEmpty())) {
SignupPage.aa = Admin.Fname[0];
SignupPage.aa = Admin.Lname[0];
ers+=1;
ElectricityPage ep = new ElectricityPage();
ep.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "something is wrong", "Payment Portal", JOptionPane.ERROR_MESSAGE);
//ers=ers+1;
}
System.out.println(ers);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnCancel) {
System.exit(0);
}
if (e.getSource() == btnLogin) {
int i = 0;
while (loggedIn == false) {
if (i < TeacherData.length) {
if (txtUsername.getText() == TeacherData[i].getUsername() && String.valueOf(txtPassword.getPassword()) == TeacherData[i].getPassword()) {
loggedIn = true;
System.out.println("Login Successful");
}
else {
System.out.println("Invalid Username or Password.");
i++;
}
}
}
}
}
My login panel is coded to look like this http://imgur.com/TizgJeX.
Upon pressing the "login", the button the console prints out "Invalid Username or Password." 10 times (there are 10 teachers in teacherData).
Before anybody asks I have already made sure the getUsername() and getPassword() methods work. txtUsername is the name of the JTextField and txtPassword is the name of the JPasswordField. I do not receive any errors.
change this line:
if (txtUsername.getText() == TeacherData[i].getUsername() && String.valueOf(txtPassword.getPassword()) == TeacherData[i].getPassword()) {
to this
String password = String.valueOf(txtPassword.getPassword());
if (txtUsername.getText().equals(TeacherData[i].getUsername()) && password.equals(TeacherData[i].getPassword())){
with == you compare the object references and not the values.
As result of this you will never login successfully.
btw. the String.valueOf is not necessary.
getPassword returns a char[] array.
You have to convert it to String with: String.valueOf(txtPassword.getPassword())
Everything runs perfectly but the String am1 = (String)JOptionPane.showInputDialog has a random default "-1" showing.
private void am1ActionPerformed(java.awt.event.ActionEvent evt) {
getinfo();//Set the random question and answer
am1.setEnabled(false);
Object[] options = {"Answer", "Cancel"};
int n = JOptionPane.showOptionDialog(null,
JeopardyGUI.question1_1,//Reference the question set
"",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE,
null, //do not use a custom Icon
options, //the titles of buttons
options[0]); //default button title
if(n == JOptionPane.YES_OPTION){
String am1 = (String)JOptionPane.showInputDialog("",JOptionPane.PLAIN_MESSAGE);
if(am1.equalsIgnoreCase(JeopardyGUI.answer1_1)){
Jscore += 100;
JOptionPane.showMessageDialog(null, JeopardyGUI.answer1_1 );
}
else if(!am1.equalsIgnoreCase(JeopardyGUI.answer1_1)){
Jscore += -100;
JOptionPane.showMessageDialog(null, JeopardyGUI.answer1_1 );
}
// else
// am1.setEnabled(true);
}
if(n == JOptionPane.NO_OPTION){
//am1.setVisible(false);
am1.setEnabled(true);
}
}
You are using:
public static String showInputDialog(Object message, Object initialSelectionValue)
JOptionPane.PLAIN_MESSAGE is your initialSelectionValue in this case. It's an int which is equal to -1 I'm guessing. What you actually want is probably:
JOptionPane.showInputDialog("Actual message", "");
Also, be careful:
String am1 = ...
Is hiding am1 the class member which is some Component it seems.
May I also suggest rewriting the handling logic as:
if(am1 != null && am1.equalsIgnoreCase(JeopardyGUI.answer1_1)){
Jscore += 100;
else Jscore += -100;
JOptionPane.showMessageDialog(null, JeopardyGUI.answer1_1 );
You need use the other static method, it don't receive a default value:
JOptionPane.showInputDialog("")
Check JOptionPane API here.
I had created a JOptionPane of type showInputDialog. When it opens it, it shows me two buttons: OK and Cancel. I would like to handle the action when I push on Cancel button, but I don't know how to reach it. How can I get it?
For example:
int n = JOptionPane.showConfirmDialog(
frame, "Would you like green eggs and ham?",
"An Inane Question",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
} else if (n == JOptionPane.NO_OPTION) {
} else {
}
Alternatively with showOptionDialog:
Object[] options = {"Yes, please", "No way!"};
int n = JOptionPane.showOptionDialog(frame,
"Would you like green eggs and ham?",
"A Silly Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (n == JOptionPane.YES_OPTION) {
} else if (n == JOptionPane.NO_OPTION) {
} else {
}
See How to Make Dialogs for more details.
EDIT: showInputDialog
String response = JOptionPane.showInputDialog(owner, "Input:", "");
if ((response != null) && (response.length() > 0)) {
}
This is an old question, and I am an Java newbie so there might be better solutions, but I wanted to know the same, and maybe it can help others, what I did was to check if the response was null.
If the user clicks "cancel", the response will be null. If they click "ok" without entering any text the response will be the empty string.
This worked for me:
//inputdialog
JOptionPane inpOption = new JOptionPane();
//Shows a inputdialog
String strDialogResponse = inpOption.showInputDialog("Enter a number: ");
//if OK is pushed then (if not strDialogResponse is null)
if (strDialogResponse != null){
(Code to do something if the user push OK)
}
//If cancel button is pressed
else{
(Code to do something if the user push Cancel)
}
The showMessageDialog, shouldn't show two buttons, so something is amiss with either your code or your interpretation of it. Regardless, if you want to give the user an choice and want to detect that choice, don't use a showMessageDialog but rather a showConfirmDialog, and get the int returned and test it to see if it is JOptoinPane.OK_OPTION.
You could do it like this:
String val = JOptionPane.showInputDialog("Value: ");
if(val == null){
// nothing goes here if yo don't want any action when canceled, or
// redirect it to a cancel page if needed
}else{
//insert your code if ok pressed
// JOptionPane return an String, as you was talking about int
int value = Integer.ParseInt(val);
}
showInputDialog return NULL if you click cancel and String object even if it's empty.
So all you need to do is test if it's Null and if it's not empty.
package Joptionpane;
import javax.swing.JOptionPane;
public class Cancle_on_JOptionPane {
public static void main(String[] args) {
String s;
int i;
for (i=0;i<100;i++){
s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?");
try {
if (s.equals("")) {
JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=2;
} else {
JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=100;
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=100;
}
}
}
}
This may be your answer:
package Joptionpane;
import javax.swing.JOptionPane;
public class Cancle_on_JOptionPane {
public static void main(String[] args) {
String s;
int i;
for(i=0;i<100;i++){
s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?");
try{
if(s.equals("")) {
JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=2;
}
else {
JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=100;
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=100;
}
}
}
}