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.

Results 1 to 4 of 4

Thread: Clicking a button, opening a text field, inputting a name, saving said name.

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    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/

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Clicking a button, opening a text field, inputting a name, saving said name.

    Quote Originally Posted by aussiemcgr View Post
    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:

    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; 
     
    	}

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Clicking a button, opening a text field, inputting a name, saving said name.

    Quote Originally Posted by Intensity` View Post
    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:
    String value = JOptionPane.showInputDialog("Input a name:");
    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.

    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; 
     
    	}
    What currently happens when you run this code? Is there something wrong that happens? Do you get an error?
    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/

Similar Threads

  1. need help inputting values into an array
    By pds8475 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 09:47 PM
  2. password field, with focused "Enter" button
    By chronoz13 in forum Java Swing Tutorials
    Replies: 1
    Last Post: June 13th, 2010, 05:00 PM
  3. unable to go to next page...while clicking submit button
    By javaking in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: April 29th, 2010, 02:55 AM
  4. inputting of text
    By Subhasis Banerjee in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: October 30th, 2009, 12:53 PM
  5. How to upload a file by clicking a link instead of button?
    By raghuprasad in forum Java Theory & Questions
    Replies: 2
    Last Post: May 3rd, 2009, 05:21 AM