I'm tired and I can't figure this out. What I want is to have it where you click a button, it opens a text field, lets you input a name, and saves the name into a new character file inside the characters folder. I'm lost with this.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I'm tired and I can't figure this out. What I want is to have it where you click a button, it opens a text field, lets you input a name, and saves the name into a new character file inside the characters folder. I'm lost with this.
I'm lost with the phrase: "opens a text field". What do you mean by this? A text field cannot be "opened".
Are you asking about a button making a pop-up that contains a text field to input into? If so, explore the world of JOptionPane for your simplest solution: How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
As for the IO (Input/Output) stuff you mentioned above (the new character file and character folder), you will have to show us how you are doing it now. Do you want to prompt the user for a location for that file, or is the location permanent? Are you creating a new file or adding to a file?
More explanation is needed for a better solution.
NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:
When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.
Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/
Yes, I want the popup with the input field.
The location of the character files is permanent. And creating a new file.
Here's my current saveGame boolean:
public static boolean saveGame() { BufferedWriter characterfile = null; try { characterfile = new BufferedWriter(new FileWriter("./characters/"+Engine.playerName+".txt")); if (Engine.playerName == null) return false; characterfile.write("character-username = ", 0, 21); characterfile.write(Engine.playerName, 0, Engine.playerName.length()); characterfile.newLine(); } catch(IOException ioexception) { System.out.println(Engine.playerName+": error writing file."); return false; } return true; }
Making a JOptionPane is very easy. Here is the API for future reference: JOptionPane (Java Platform SE 7 b120)
To make a JOptionPane with a text field, you want to use the showInputDialog methods. The API shows 6 different methods to create this, but I will show the most basic. All of the showInputDialog methods returns a String. The returned String is the value that was in the text field when the window was closed. The most basic showInputDialog method asks for just a message parameter. This message is what you want the user to read in the window that opens. Creating a pop-up for input is show below:
Usually, you want to error check to confirm that the user pressed OK instead of X-ing out, but that is more detailed things that can be read about in the tutorials I posted earlier.String value = JOptionPane.showInputDialog("Input a name:");
What currently happens when you run this code? Is there something wrong that happens? Do you get an error?The location of the character files is permanent. And creating a new file.
Here's my current saveGame boolean:
public static boolean saveGame() { BufferedWriter characterfile = null; try { characterfile = new BufferedWriter(new FileWriter("./characters/"+Engine.playerName+".txt")); if (Engine.playerName == null) return false; characterfile.write("character-username = ", 0, 21); characterfile.write(Engine.playerName, 0, Engine.playerName.length()); characterfile.newLine(); } catch(IOException ioexception) { System.out.println(Engine.playerName+": error writing file."); return false; } return true; }
NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:
When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.
Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/