Jump to content

Can anyone figure out the problem with this code?


Recommended Posts

Posted (edited)

Basically, if I add an icon to the JOptionPane, it doesn't recognize my option anymore. If I leave it without an image, it does recognize the option. It's obviously something with the code, but what is it? O_O

What it does is, I let the user select the type of log they want, and then perform an action based on that. Here's a snippet:

 

Code that works

	@Override
	public void onStart() throws InterruptedException {	
		
		String userOptions[] = { "Logs", "Oak logs", "Teak logs", "Mahogany logs" };
		String userChoice = "" + (String) JOptionPane.showInputDialog(null, "Choose a log type", "Log Selection Menu",
				JOptionPane.PLAIN_MESSAGE, null, userOptions, userOptions[1]);
	}

Code that doesn't work

	@Override
	public void onStart() throws InterruptedException {
		
		ImageIcon icon = null;
		try {
			icon = new ImageIcon(new URL(
					"https://vignette.wikia.nocookie.net/2007scape/images/6/65/Demon_butler_chathead.png/revision/latest?cb=20151103211007"));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String userOptions[] = { "Logs", "Oak logs", "Teak logs", "Mahogany logs" };
		String userChoice = "" + (String) JOptionPane.showInputDialog(null, "Choose a log type", "Log Selection Menu",
				JOptionPane.PLAIN_MESSAGE, icon, userOptions, userOptions[1]);
	}

 

Edited by Lol_marcus
Posted (edited)

It's not passing on the option to the rest of the script. I'm getting "userChoice is an invalid variable" if I try to do something like getBank().withdrawAll(userChoice); with the code that doesn't work. If I remove all the code to add my icon it works fine. :???:

 

The code itself works, it's just not passing the users choice for some odd reason.

Edited by Lol_marcus
Posted
On 6/6/2020 at 4:40 AM, Lol_marcus said:

It's not passing on the option to the rest of the script. I'm getting "userChoice is an invalid variable" if I try to do something like getBank().withdrawAll(userChoice); with the code that doesn't work. If I remove all the code to add my icon it works fine. :???:

 

The code itself works, it's just not passing the users choice for some odd reason.

If the code above is the same that you are using and you are trying to access the userChoice variable outside of the onStart() than it will show invalid variable, as you have only created that variable inside of the onStart(). Add the userChoice var to the class and set that value in the onStart(), you will than be able to use that variable.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...