login window - gui with file io

I am trying to make a login window that checks a flat file 'clients.dat' to verify the user name and password. I am not using sql and I don't want to use it. How do I add the file io to my gui?

here is my login window gui:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class mainGUI extends JFrame implements ActionListener

{

private static final long serialVersionUID = 1L;

// textfield labels

private JLabel useridLabel = new JLabel ( "User ID:" );

private JLabel passwordLabel = new JLabel ( "Last Name: " );

// textfields

private JTextField useridText = new JTextField( 20 );

private JPasswordField passwordText = new JPasswordField( 20 );

// message labels

private JLabel mainMessage = new JLabel( " Click Register To Create An Account" );

private JLabel spacer = new JLabel( "" );

// buttons

private JButton submitButton = new JButton("Submit");

private JButton registerButton = new JButton("Register");

private JButton cancelButton = new JButton("Cancel");

public static void main(String[] args)

{

mainGUI app = new mainGUI();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public mainGUI()

{

super("Welcome To The Electronic Bidding System");

Container content = getContentPane();

content.setBackground( Color.lightGray );

// Message

JPanel messageArea = new JPanel();

messageArea.setLayout( new BorderLayout() );

mainMessage.setForeground( new Color(0xff0000) );

messageArea.add( mainMessage, BorderLayout.CENTER );

messageArea.add( spacer, BorderLayout.SOUTH );

content.add( messageArea, BorderLayout.NORTH );

// Textfield area and messages

JPanel controlArea = new JPanel( new GridLayout(3, 2) );

controlArea.add( useridLabel );

controlArea.add( useridText );

controlArea.add( passwordLabel );

controlArea.add( passwordText );

content.add( controlArea, BorderLayout.EAST );

// Label Area

JPanel labelArea = new JPanel();

// Preferred height is irrelevant, since using WEST region

labelArea.setPreferredSize(new Dimension(100, 0));

labelArea.add( useridLabel );

labelArea.add( passwordLabel );

content.add( labelArea, BorderLayout.WEST );

// Button Area

JPanel buttonArea = new JPanel();

buttonArea.add( submitButton );

buttonArea.add( registerButton );

buttonArea.add( cancelButton );

submitButton.addActionListener( this );

cancelButton.addActionListener( this );

content.add( buttonArea, BorderLayout.SOUTH );

pack();

setVisible( true );

//setSize(650, 200);

setResizable( false );

//}

cancelButton.addActionListener( new ActionListener()

{

public void actionPerformed( ActionEvent event)

{

setDefaultCloseOperation( EXIT_ON_CLOSE );

dispose(); // remove main window

}

}

); // end cancelButton.addActionListener

registerButton.addActionListener( new ActionListener()

{

public void actionPerformed( ActionEvent event)

{

NURegister opennureg = new NURegister();

dispose(); // remove main window

}

}

); // end registerButton.addActionListener

submitButton.addActionListener( new ActionListener()

{

public void actionPerformed( ActionEvent event )

{

useridText.setBackground(Color.WHITE);

passwordText.setBackground(Color.WHITE);

// if userid is not digits, error, change color of field to red

if( !useridText.getText().matches("\\d+") )

{

useridText.setBackground(Color.RED);

}

// if firstname is not a string, error, change color of field to red

if( !passwordText.getSelectedText().matches("\\S+") )// password must be a string

{

passwordText.setBackground(Color.RED);

}

}

}

); // end submitButton.addActionListener

}

public void actionPerformed(ActionEvent arg0) {

// TODO Auto-generated method stub

}

}

[4216 byte] By [Ivorya] at [2007-10-2 20:21:02]
# 1
Use java.io.* package, specifically java.io.FileReader to read the *.dat file. ICE
icewalker2ga at 2007-7-13 23:03:28 > top of Java-index,Java Essentials,Java Programming...
# 2
Hello ICE, and thank you for your response. what is the code and where does it go in this file? I am very new to this Java programming. Thanks Ivory
Ivorya at 2007-7-13 23:03:28 > top of Java-index,Java Essentials,Java Programming...
# 3

Here's simpler way which is not really secure but useful for novice programming. This use the java.util.Properties object

Say you have some text file with user information (call it users.dat)

users = 2

#format: user#=username | password

user1=Frank | frank50

user2=Mary | mayU

Then using a Properties object you would access the file as such

public Properties userData;

public void loadUserInformation() {

userData = new Properties();

try {

userData.load( new FileInputStream( new File("users.dat") ) );

} catch(IOException ioe) {

System.out.println("Load error: " + ioe);

return;

}

}

When the user enters his username and password validation could be done as follows:

public boolean validateUser(String user, char[] pass) {

String userPass = userData.getProperty(user).trim();

if(userPass.equals( new String(pass) )) {

return true;

}

return false;

}

This is the general idea. Read more on the java.util.Properties class for a better understanding of the file formatting

ICE

icewalker2ga at 2007-7-13 23:03:28 > top of Java-index,Java Essentials,Java Programming...