Results 1 to 8 of 8
  1. #1
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    23
    Rep Power
    0

    Default Trying to write to external file by pressing button

    I'm trying to make a program to input a customer list to an external file. I want to have two buttons, 1 to be able to add a new customer and still print to the file, and 1 to be done and print the variable to the file. However, in order to do this, I need to to write to the file before closing the GUI. I need to do this within the buttons actionPerformed() methods. However, I need to throw an IOException for those methods and I cannot. How do I do?!

    Java Code:
    package customerlist;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    /**
     * Name: Ben Hughes
     * Date: November 11th, 2015
     * Purpose: To read customer information from an external file and allow the user to write new customer information to the file
     */
    public class CustomerList {
        public static void main(String[] args) throws IOException {
            getInput main = new getInput();        
            main.userInput();
        }    
    }
    Java Code:
    package customerlist;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import javax.swing.*;
    import java.util.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    
    /**
     *
     * @author 358512
     */
    public class getInput{
        int listCount = 0;
        String names = "";
        String cities = "";
        String addresses = "";
        String provinces= "";
        String postalCodes = "";
        
        public void userInput(){
            JFrame main = new JFrame();
            main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  
            final JTextField name = new JTextField(20);
            final JTextField city = new JTextField(10);
            final JTextField address = new JTextField(20);
            final JTextField province = new JTextField(10);
            final JTextField postalCode = new JTextField(10);
            JButton addNew = new JButton("Add Customer");
            JButton done = new JButton("Done");
            
            done.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    names = name.getText();
                    cities = city.getText();
                    addresses = address.getText();
                    provinces = province.getText();
                    postalCodes = postalCode.getText();
                    writeList();
                }    
            });
            
            addNew.addActionListener(new ActionListener(){
    
                public void actionPerformed(ActionEvent e){
                    names = name.getText();
                    cities = city.getText();
                    addresses = address.getText();
                    provinces = province.getText();
                    postalCodes = postalCode.getText();  
                    userInput();
                }
            });
            
    
            
            main.setLayout(new GridLayout(0,1));
            JPanel customerInput = new JPanel(new GridLayout(0,1));
            
            main.add(customerInput);
            customerInput.add(new JLabel("Name:"));
            customerInput.add(name);
            customerInput.add(new JLabel("City:"));
            customerInput.add(city);
            customerInput.add(new JLabel("Address:"));
            customerInput.add(address);
            customerInput.add(new JLabel("Province:"));
            customerInput.add(province);
            customerInput.add(new JLabel("Postal Code:"));
            customerInput.add(postalCode);
            customerInput.add(addNew);
            customerInput.add(done);
            
            customerInput.setVisible(true);
            
             
            
            JOptionPane.showMessageDialog(null, customerInput, "Customer List", JOptionPane.INFORMATION_MESSAGE);
        }
        
        public void writeList()throws IOException{
            File customerList = new File("H:\\customerList.txt");
            FileWriter fileWriter = new FileWriter(customerList, true);
            BufferedWriter buffer = new BufferedWriter(fileWriter);
            PrintWriter fileOut = new PrintWriter(buffer);
       
                fileOut.println("Name: " + names);
                fileOut.println("City: " + cities);
                fileOut.println("Address: " + addresses);
                fileOut.println("Province: " + provinces);
                fileOut.println("Postal Code: " + postalCodes);
                fileOut.println("------------------------------------\n\n");
                   
            fileOut.close();    
        }   
    }

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

    Default Re: Trying to write to external file by pressing button

    What do you mean you need to throw an exception and cannot? Please provide an example an keep it short.

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

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Trying to write to external file by pressing button

    What problems are you having? Does the code compile ok? If not, copy the full text of the error messages and paste it here.
    Does the program execute? What happens?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    23
    Rep Power
    0

    Default Re: Trying to write to external file by pressing button

    Quote Originally Posted by Norm View Post
    What problems are you having? Does the code compile ok? If not, copy the full text of the error messages and paste it here.
    Does the program execute? What happens?
    In the ActionListener for the done button, there is an error over the writeList() method, saying "unreported exception IOException; must be declared or thrown", however, when I try and add the "throws IOException" to the end of the ActionListener, it gives an error saying "ActionPerformed(ActionEvent) in <anonymous customerlist.getinput$1> cannot implement ActionPerformed(ActionEvent) in ActionListener. Overriden method does not throw IOException."

    Furthermore, and override annotation for the ActionListener does not fix the issue. What am I doing wrong?

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

    Default Re: Trying to write to external file by pressing button

    You can't add a "throws WhateverException" to an overridden method that doesn't throw it. So you have to surround it with a try {} catch{} block and handle the exception there.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

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

    Default Re: Trying to write to external file by pressing button

    Quote Originally Posted by SurfMan View Post
    You can't add a "throws WhateverException" to an overridden method that doesn't throw it. So you have to surround it with a try {} catch{} block and handle the exception there.
    Or, since writeList() is homegrown, remove the throws clause from that method. Then surround the appropriate code in writeList with a try catch block.

    Also, each time your "Add Customer" button is pressed, you actionListener creates a new JFrame (via userInput()), and adds a new listener. Are you really sure you want to do this?

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

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

    Default Re: Trying to write to external file by pressing button

    Quote Originally Posted by jim829 View Post
    Or, since writeList() is homegrown, remove the throws clause from that method. Then surround the appropriate code in writeList with a try catch block.
    And think what you want to happen in the case that the file is not found, or cannot be opened.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Trying to write to external file by pressing button

    Quote Originally Posted by jim829 View Post
    Or, since writeList() is homegrown, remove the throws clause from that method. Then surround the appropriate code in writeList with a try catch block.

    Also, each time your "Add Customer" button is pressed, you actionListener creates a new JFrame (via userInput()), and adds a new listener. Are you really sure you want to do this?

    Regards,
    Jim
    I would say you catch it and handle it as soon as you can. Don't let it bubble up to an ActionListener.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Write to external file from textfield
    By zlloyd1 in forum New To Java
    Replies: 4
    Last Post: 01-28-2013, 07:25 AM
  2. pressing a button making a instance in another class
    By SpicyElectricity in forum New To Java
    Replies: 3
    Last Post: 02-18-2012, 10:48 PM
  3. Draw shapes by pressing a button
    By SWEngineer in forum AWT / Swing
    Replies: 16
    Last Post: 05-14-2010, 05:26 AM
  4. called external program does not automatically write file
    By nickvandewiele in forum New To Java
    Replies: 6
    Last Post: 02-25-2010, 02:38 PM
  5. Replies: 5
    Last Post: 01-17-2008, 09:01 PM

Posting Permissions

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