Hey, so I'm trying to make a text-based game using mostly Swing GUI; the code I have so far is below. When I use JOptionPanes I modify the icon, title, and suchlike on them, so I have about two lines of code for every JOptionPane. I created methods for them so I wouldn't have to retype the specs each time. The one for the MessageDialog works fine, but the InputDialog doesn't seem to recognize my String as a String; it gives an incompatible type error. It is reading my String as simply and Object and I don't know why. Any ideas?

import javax.swing.*;
import java.util.*;

public class Game2
	public static Icon sword=new ImageIcon();

	public static void message(String m) //using these methods because I have specs for each JOptionPane that I don't
	{									 //want to type every time I use one.  This one works fine.
		JOptionPane.showMessageDialog(null, m, "Game", JOptionPane.INFORMATION_MESSAGE, sword);
	public static String input(String i) //this one, however, gives an "incompatible types" error
		String userInput;
		userInput=JOptionPane.showInputDialog(null, i, "Game", JOptionPane.INFORMATION_MESSAGE, sword, null, null);
		return userInput;
	public static void main(String[] args)
		message("Hello!  Welcome to my game!");
		input("Please enter your name.");

Please post full text of error message here. It makes it a lot faster and can have more and correct info about the problem.

Read the API doc for the method you are using. What does it return?

Here's the full error message I'm getting:

--------------------Configuration: <Default>--------------------
C:\Documents and Settings\mekks\My Documents\Computer Science\Java Programs\Game2.java:16: incompatible types
found : java.lang.Object
required: java.lang.String
userInput=JOptionPane.showInputDialog(null, i, "Game", //error is here
1 error

Process completed.

As for reading the API, how do you do that? I'm unfamiliar with it.

Application Programming Interface documentation.
"This document is the API specification for version 6 of the Java™ Platform, Standard Edition."

Where do you get documentation for how to use classes? This is the bible!

Haha right, sorry, I wasn't familiar with the name but I use it all the time (it is the Bible!). I believe I am doing exactly what it says with regard to using the method, though, and it still recognizes my String as an Object...I am at a loss.

You have typed this:

userInput=JOptionPane.showInputDialog(null, i, "Game", JOptionPane.INFORMATION_MESSAGE, sword, null, null);

According to the API doc, this is the method signature for showInputDialog that you are using:

public static Object showInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue) throws HeadlessException

You could try explicitly casting to a String?

I think you need "Type Cast".

userInput=(String)JOptionPane.showInputDialog(null, i, "Game", JOptionPane.INFORMATION_MESSAGE, sword, null, null);

Great, yeah, the type casting worked. Thanks!

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.