I have a series of of JOptionPanes pop up to get info from the user. However, if the user presses cancel, the machine gets an error (which is prevented). I'm trying to catch this and force the user to make a choice and press OK. MY code doesn't seem do do this. Here are the two ways that I tried:

//Obtians student information from the user
            Object[] possibilities = {"AP Computer Science", "IB Computer Science", "Algebra II"};
            StudentClass = (String)JOptionPane.showInputDialog(All, "What class is \nthe new student in?", "Info", JOptionPane.PLAIN_MESSAGE, null, possibilities, "IB Compter Science");
            newStudent= (String)JOptionPane.showInputDialog(All, "Please enter the full name\nof the new student.", "Info", JOptionPane.PLAIN_MESSAGE);
            newGrade= (String)JOptionPane.showInputDialog(All, "Please enter the grade\nof the new student.", "Info", JOptionPane.PLAIN_MESSAGE);
            
            while (StudentClass.equals(null))
            {
               StudentClass = (String)JOptionPane.showInputDialog(All, "What class is the student whose\ngrades are to be changed in?", "Info", JOptionPane.PLAIN_MESSAGE, null, possibilities, "IB Compter Science"); 
            }

and

//Obtians student information from the user
            Object[] possibilities = {"AP Computer Science", "IB Computer Science", "Algebra II"};
            StudentClass = (String)JOptionPane.showInputDialog(All, "What class is \nthe new student in?", "Info", JOptionPane.PLAIN_MESSAGE, null, possibilities, "IB Compter Science");
            newStudent= (String)JOptionPane.showInputDialog(All, "Please enter the full name\nof the new student.", "Info", JOptionPane.PLAIN_MESSAGE);
            newGrade= (String)JOptionPane.showInputDialog(All, "Please enter the grade\nof the new student.", "Info", JOptionPane.PLAIN_MESSAGE);
            
            while (StudentClass.equals("null"))
            {
               StudentClass = (String)JOptionPane.showInputDialog(All, "What class is the student whose\ngrades are to be changed in?", "Info", JOptionPane.PLAIN_MESSAGE, null, possibilities, "IB Compter Science"); 
            }

can some one show me the proper way to do this? Thanks!

In case you do not want your user to press "Cancel" in a JOptionPane dialog, you can actually get rid of that button altogether using the JOptionPane.showOptionDialog()

<EDIT> Cleaned some of my errors.

Ok Ill look into that. I just dont understand why it doesnt work because when i made this simple program:

public class optiontest
{
    public static void main(String[]args)
    {
        Object[] possibilities = {"AP Computer Science", "IB Computer Science", "Algebra II"};
        String StudentClass = (String)JOptionPane.showInputDialog(null, "What class is \nthe new student in?", "Info", JOptionPane.PLAIN_MESSAGE, null, possibilities, "IB Compter Science");
        System.out.println(StudentClass);
    }
}

with the proper imports, and pressed cancel, the System printed out null

If you use the showInputDialog() method, it either returns the String that was entered in the text field or else "NULL" if cancel was pressed, So in case you wish a User to force some input and not press Cancel, you could simple put it in a do-while loop like this:-

String StudentClass = null;
do {
  StudentClass = (String)JOptionPane.showInputDialog(null, "What class is " + 
      "\nthe new student in?", "Info", JOptionPane.PLAIN_MESSAGE, 
      null, possibilities, "IB Compter Science");
   System.out.println(StudentClass);
}while(StudentClass == null);

However if you use showOptionDialog method, and pass the "possibilities" array for the "options", you would get three buttons with their corresponding headings as specified in the Array.

Ok thanks, the do-while loop was what I needed!

This avoids a nullPointerException

public void get(String text)
	{
		boolean loop;
		do{
			loop = false;
			try
			{
				input = JOptionPane.showInputDialog(null,text,title,JOptionPane.PLAIN_MESSAGE).toLowerCase();
			}
			catch(Exception e)
			{
				message("Don't hit that CANCEL BUTTON!");
			}
		}while(loop);
	}

This avoids a nullPointerException

public void get(String text)
	{
		boolean loop;
		do{
			loop = false;
			try
			{
				input = JOptionPane.showInputDialog(null,text,title,JOptionPane.PLAIN_MESSAGE).toLowerCase();
			}
			catch(Exception e)
			{
				message("Don't hit that CANCEL BUTTON!");
			}
		}while(loop);
	}

there isn't need to revive a thread that was marked "solved" over a year ago

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.