Clicking a button, opening a text field, inputting a name, saving said name.
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.
Re: Clicking a button, opening a text field, inputting a name, saving said name.
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.
Re: Clicking a button, opening a text field, inputting a name, saving said name.
Quote:
Originally Posted by
aussiemcgr
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.
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:
Code java:
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;
}
Re: Clicking a button, opening a text field, inputting a name, saving said name.
Quote:
Originally Posted by
Intensity`
Yes, I want the popup with the input field.
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.
Quote:
The location of the character files is permanent. And creating a new file.
Here's my current saveGame boolean:
Code java:
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;
}
What currently happens when you run this code? Is there something wrong that happens? Do you get an error?