Results 1 to 5 of 5
  1. #1
    aadem is offline Member
    Join Date
    Mar 2011
    Posts
    22
    Rep Power
    0

    Default Requesting a review of my LoginBox!

    Hello everyone!

    I had an idea to create a simple login box that will take a user name and password from someone, and check a text file to see if the name and pw exist on any line in the file.

    Right now the window is coded and the only 2 things the program does so far, is when ran it checks to see if a registrar.txt file exists, if it doesnt it creates it with a title on the first line.

    The second thing it does is when the login button is clicked, it takes whatever text is in the username textfield and the pw textfield and appends them to their own line in the registrar.txt file

    My questions are:
    Even though this code works, when i look at it it looks off because every time i click login it creates a FileWriter and a BufferedWriter with the same names as it did the last time, yet it only works if i ONLY .close() the buffered writer. If i try to .close() the filewriter which makes sense to me, it throws an IOException. To cut it short, when and why should i close() a bufferedwriter/bufferedreader or anything else, and did i do it right already?


    Last question: Anyone see anything they would have done differently? Any suggestions welcome and sorry for the terribly long thread

    Java Code:
    /*
     * Swing loginbox for getting username and password
     * written by: Aaron DeMarco
     */
    
    
    package gutilities.getUserInfo;
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    import java.awt.*;
    
    class loginBox {
    
        private static void createAndShowGUI() {
    	File registrar = new File("registrar.txt");
    	boolean regExists = false;
    	regExists = registrar.exists();
    	try{
                if (regExists != true) {
    		FileWriter fOut = new FileWriter(registrar);
    		BufferedWriter fileOut = new BufferedWriter(fOut);
    		String registrarBan = "-----Registrar Log-----\n";
    		fileOut.write(registrarBan);
    		fileOut.close();
    		System.out.println("Address Book Created");
    	    } 
    	    
    	    else {
    	    System.out.println("Registrar exists, loading");
    	 	}
    	} catch (IOException err) {
    	    System.out.println("Error creating file");
    	}
    
    	
    	JFrame mainFr = new JFrame();
    	mainFr.setTitle("User Login");
    	mainFr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		int windowWidth = 380;
    		int windowHeight = 200;
    	Point centP = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
    		
    	Container cPane = mainFr.getContentPane();
    	
    	JButton lginB = new JButton("Login");
    	JButton cancB = new JButton("Cancel");
    	JButton regB = new JButton("Register");
    	JPanel buttonPanel = new JPanel();
    	FlowLayout bpLayout = new FlowLayout();
    	buttonPanel.setLayout(bpLayout);
    	bpLayout.setHgap(35);
    	buttonPanel.add(lginB);
    	buttonPanel.add(cancB);
    	buttonPanel.add(regB);
    	cPane.add(buttonPanel, BorderLayout.PAGE_END);
    
    	JLabel banLab = new JLabel("Enter login/pass or click register");
    	JPanel banPanel = new JPanel();
    	banPanel.add(banLab);
    	cPane.add(banPanel, BorderLayout.PAGE_START);
    
    	JLabel unameLab = new JLabel("Username");
    	JLabel pwLab = new JLabel("Password");
    	final JTextField unameTxt = new JTextField("",12);
    	final JTextField pwTxt = new JTextField("",12);
    	JPanel fieldPane = new JPanel();
    	fieldPane.setLayout(new GridBagLayout());
    	GridBagConstraints c = new GridBagConstraints();
    	c.gridx = 0;
    	c.gridy = 0;
    	c.anchor = GridBagConstraints.CENTER;
    	c.insets = new Insets(10,10,10,30);
    	fieldPane.add(unameLab, c);
    	c.gridx = 1;
    	c.gridy = 0;
    	c.anchor = GridBagConstraints.CENTER;
    	c.insets = new Insets(10,30,10,10);
    	fieldPane.add(unameTxt, c);
    	c.gridx = 0;
    	c.gridy = 1;
    	c.anchor = GridBagConstraints.CENTER;
    	c.insets = new Insets(10,10,10,30);
    	fieldPane.add(pwLab, c);
    	c.gridx = 1;
    	c.gridy = 1;
    	c.anchor = GridBagConstraints.CENTER;
    	c.insets = new Insets(10,30,10,10);
    	fieldPane.add(pwTxt, c);
    	cPane.add(fieldPane, BorderLayout.CENTER);
    
    	//Add action listeners
    	lginB.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    		    System.out.println("Logging In");
    		    try{
    		    String unameGetText = unameTxt.getText();
    		    String pwGetText = pwTxt.getText();
    		    FileWriter logOut = new FileWriter(new File("registrar.txt"), true);
    		    BufferedWriter logBuff = new BufferedWriter(logOut);
    		    logBuff.write("\n" + unameGetText + "\n" + pwGetText + "\n");
    		    logBuff.close();
    		    } catch (IOException er) {
    			System.out.println("Error accessing file");
    		    }
    		}
    	    });
    	    
    		    
    	//Place and size Window
    	mainFr.setBounds(centP.x - windowWidth / 2, centP.y - windowHeight / 2, windowWidth, windowHeight);
    	mainFr.setVisible(true);
        }
    
    
    
        public static void main(String[] argv) {
    	javax.swing.SwingUtilities.invokeLater(new Runnable() {
    		public void run() {
    		    createAndShowGUI();
    		}
    	    });
        }
    
     }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    if it makes sense to close the filewriter, close it, and then debug your code. if you make diversions every time you have a bug how will you finish this?

  3. #3
    aadem is offline Member
    Join Date
    Mar 2011
    Posts
    22
    Rep Power
    0

    Default

    ahhh wise words ozzy. I shall heed your advice. thanks :)

  4. #4
    aadem is offline Member
    Join Date
    Mar 2011
    Posts
    22
    Rep Power
    0

    Default

    my other thread actually answered this question :) so ill mark this as solved

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

Similar Threads

  1. Help getting my program to continue requesting info.
    By antdilla22 in forum New To Java
    Replies: 4
    Last Post: 09-13-2009, 10:44 PM
  2. please review the following code
    By ajbj in forum New To Java
    Replies: 3
    Last Post: 08-25-2009, 08:37 AM
  3. Replies: 1
    Last Post: 10-20-2008, 12:58 PM
  4. Replies: 1
    Last Post: 06-23-2008, 06:46 AM
  5. Java Review
    By toad in forum New To Java
    Replies: 5
    Last Post: 12-08-2007, 01:28 AM

Posting Permissions

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