Results 1 to 10 of 10
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By SurfMan

Thread: How to save content from a JTextField to a txt file via a buton's actionLisytener?

  1. #1
    Titus_85 is offline Member
    Join Date
    Apr 2016
    Posts
    11
    Rep Power
    0

    Post How to save content from a JTextField to a txt file via a buton's actionLisytener?

    Hello.So I want to save content from a JTextField to a txt file when I press the button.
    Here is my code:

    Java Code:
    public class Test7 {
    
    	public JFrame mainFrame;
    	public JPanel panel_1;
    	public JLabel label_2;
    	public JButton buton_1, buton_2, buton_3, buton_4, buton_5;
    	public JTextField textF_1;
    
    	
    
    	public static void main(String[] args) {
    		Test7 obj_1 = new Test7();
    		
    		FileOutputStream fop = null;
    		File file;
    
    	}
    
    	public Test7() {
    		mainFrame = new JFrame("Fereastra_1");
    		mainFrame.setSize(600, 600);
    		mainFrame.setLayout(null);
    
    		panel_1 = new JPanel();
    		buton_1 = new JButton("buton");
    		textF_1 = new JTextField();
    
    		buton_1.addActionListener(new CustomActionListener());
    
    		mainFrame.add(buton_1);
    		mainFrame.setVisible(true);
    		mainFrame.add(panel_1);
    		mainFrame.add(textF_1);
    
    		panel_1.setBackground(Color.green);
    		panel_1.requestFocusInWindow();
    
    		panel_1.setBounds(1, 1, 500, 500);
    		buton_1.setBounds(450, 30, 130, 40);
    		textF_1.setBounds(250, 30, 130, 40);
    
    		mainFrame.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent windowEvent) {
    				System.exit(0);
    			}
    
    		});
    
    	}
    
    }
    
    class CustomActionListener implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
         .............................Please Help..........................
    
           }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to save content from a JTextField to a txt file via a buton's actionLisytener

    Your action listener needs access to the JTextField you want to read.
    So I would add a constructor to the CustomActionListener (and rename it to something more meaningful while I'm at it) that takes a JTextField, and store that as an instance variable.

    That means you can then read the value in the text field when the actionPerformed is called.
    Titus_85 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Titus_85 is offline Member
    Join Date
    Apr 2016
    Posts
    11
    Rep Power
    0

    Default Re: How to save content from a JTextField to a txt file via a buton's actionLisytener

    Right but could you please write the code for how should I do that?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to save content from a JTextField to a txt file via a buton's actionLisytener

    Quote Originally Posted by Titus_85 View Post
    Right but could you please write the code for how should I do that?
    Just try it yourself. Check out the tutorials in my signature. Look for how to use buttons and how to write an action listener.
    But I recommend you write a program simply to practice using the above. Then integrate into your main program later.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Titus_85 is offline Member
    Join Date
    Apr 2016
    Posts
    11
    Rep Power
    0

    Default Re: How to save content from a JTextField to a txt file via a buton's actionLisytener

    Great.I've done that but now I have another problem.Every time the text from the JTextField will overwrite the previous text.How can I have it write on the next line.Thank you!


    String f = textField.getText();

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to save content from a JTextField to a txt file via a buton's actionLisytener

    I don't believe text fields support more than one row (which is the nature of a field). The easiest way to add content is to grab the existing text, concatenate the new text and rewrite. You can also get the default document and set the insertion point. For more than one row, use JTextAreas or JTextPanes.

    Once again, these are all mentioned in the tutorials. Please read about them first, try some stuff, and then ask questions.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: How to save content from a JTextField to a txt file via a buton's actionLisytener

    Quote Originally Posted by Titus_85 View Post
    Great.I've done that but now I have another problem.Every time the text from the JTextField will overwrite the previous text.How can I have it write on the next line.Thank you!


    String f = textField.getText();

    There is a FileWriter and FileOutputStream constructor that accepts a boolean (true/false) for appending. If you're writing just text, I'd go for the FileWriter.
    FileWriter (Java Platform SE 8 )
    FileOutputStream (Java Platform SE 8 )
    Titus_85 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to save content from a JTextField to a txt file via a buton's actionLisytener

    Apparently I mis-understood what the OP was asking.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: How to save content from a JTextField to a txt file via a buton's actionLisytener

    Quote Originally Posted by jim829 View Post
    Apparently I mis-understood what the OP was asking.

    Regards,
    Jim
    Don't blame yourself. It's a vague comment if anything. I am also assuming this is about writing into the file. I guess we know for sure when the OP returns with a "Thanks, it works now!" message :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  10. #10
    Titus_85 is offline Member
    Join Date
    Apr 2016
    Posts
    11
    Rep Power
    0

    Default Re: How to save content from a JTextField to a txt file via a buton's actionLisytener

    That's what I meant.I need to write multiple lines in the .txt file. Thanks SurfMan !

Similar Threads

  1. Replies: 1
    Last Post: 03-17-2014, 02:03 AM
  2. Replies: 1
    Last Post: 05-04-2012, 03:09 PM
  3. Register content of jTextField
    By jeata in forum New To Java
    Replies: 3
    Last Post: 11-19-2010, 09:22 PM
  4. Replies: 7
    Last Post: 08-17-2010, 06:00 PM
  5. Clean the content of the JTextField
    By elizabeth in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2007, 08:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •