Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How to read text file from a GUI

$
0
0

Quote

how do access reading a text file to Login ?



here's the code:


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;

public class LoginBank extends JFrame {
     private JButton back, lgnbb, clear, pcode;
     private JTextField txf1;
     private JPasswordField txf2;
     private JLabel ln,pd;

     public void lgBank (){


     	ln = new JLabel("Account No. : ");
		ln.setFont(new Font("Serif", Font.BOLD, 24));
		ln.setForeground(Color.BLACK);
		ln.setLocation(20,23);
		ln.setSize(200,30);

     	pd = new JLabel("Password: ");
		pd.setFont(new Font("Serif", Font.BOLD, 24));
		pd.setForeground(Color.BLACK);
		pd.setLocation(20,60);
		pd.setSize(200,30);

		txf1 = new JTextField(20);
		txf1.setBackground(new Color(204,204,255));
		txf1.setLocation(170,25);
		txf1.setFont(new Font("Serif", Font.PLAIN, 20));
		txf1.setToolTipText("Type your Account ID or Number in textbox.");
		txf1.setSize(200,25);
		txf1.setHorizontalAlignment(JTextField.LEFT);

		txf2 = new JPasswordField(20);
		txf2.setBackground(new Color(204,204,255));
		txf2.setLocation(170, 65);
		txf2.setEchoChar('*');
		txf2.setFont(new Font("Serif", Font.PLAIN, 20));
		txf2.setToolTipText("Type 8-16 characters.");
		txf2.setSize(200,25);
		txf2.setHorizontalAlignment(JTextField.LEFT);

		ImageIcon bcr = new ImageIcon(getClass().getResource("/images/bcr.jpg"));
		final JButton back = new JButton(bcr);
		back.setLocation(20,120);
		back.setSize(70,30);
		back.setPreferredSize(new Dimension(90,30));
		back.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
			dispose();
			}
		}
		);

		ImageIcon lll = new ImageIcon(getClass().getResource("/images/loginn.jpg"));
		final JButton lgnbb = new JButton(lll);
		lgnbb.setLocation(100, 120);
		lgnbb.setSize(70,40);
		lgnbb.setPreferredSize(new Dimension(70,40));
		lgnbb.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent ee){

			String acc,code;

						BufferedReader reader = null;
						String text= "";
                acc = txf1.getText();
                code = txf2.getText();
                if((txf1.getText().equals(""))||(txf2.getText().equals("")))
                	{
				JOptionPane.showMessageDialog(null,
					"Please Type!!",
					"Incomplete",
					JOptionPane.WARNING_MESSAGE);
			}else{


					try{
						File file = new File("Database.txt");
						reader = new BufferedReader(new FileReader(file));
						String []data = text.split("\t");
						String accno = data[0];
						int cd = Integer.parseInt(data[1]);

						if((txf1.getText().equals(accno))||
							(txf2.getText().equals(cd))){
								JOptionPane.showMessageDialog(null,
								"Login Success!!!",
								"Login-Successfully!!!",
								JOptionPane.INFORMATION_MESSAGE);

							while((text=reader.readLine())!=null){
                				WelcomeAcc exe = new WelcomeAcc();
								exe.wcAcc();
								dispose();
            				}




							}

						}
						catch (FileNotFoundException ex){
		          			ex.printStackTrace();
		      			} catch (IOException ex) {
		           			 ex.printStackTrace();
		       			} finally {
		        		    try {
		            		   	if (reader != null){
		                			reader.close();
		              		}
		           		} catch (IOException ex) {
		            		    ex.printStackTrace();
		           	}
		        }
		         }

		}
		});

		ImageIcon cle = new ImageIcon(getClass().getResource("/images/clear.jpg"));
		final JButton clear = new JButton(cle);
		clear.setLocation(190,120);
		clear.setSize(70,40);
		clear.setPreferredSize(new Dimension(90,50));
		clear.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent eee){
			txf1.setText("");
			txf2.setText("");
			}
		}
		);

		ImageIcon pco = new ImageIcon(getClass().getResource("/images/pcode.jpg"));
		final JButton pcode = new JButton(pco);
		pcode.setLocation(280,120);
		pcode.setSize(70,40);
		pcode.setPreferredSize(new Dimension(70,40));
		pcode.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent eeee){
			PinBank pbnk = new PinBank();
			pbnk.pinBnk();
			}
		}
		);

		Container pane = getContentPane();
		pane.setLayout(null);
		pane.add(lgnbb);
		pane.add(clear);
		pane.add(pcode);
		pane.add(back);
		pane.add(ln);
		pane.add(pd);
		pane.add(txf1);
		pane.add(txf2);

		setIconImage(new ImageIcon("icon1.jpg").getImage());
		setLayout(new BorderLayout());
		JLabel lgn = new JLabel(new ImageIcon(getClass().getResource("/images/lgn.jpg")));
		add(lgn);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setTitle("Login Account");
		setSize(400,220);
		setResizable(false);
		setVisible(true);
		setLocation(120, 300);

     }
}


Here's the code of WelcomeAcc.java:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class WelcomeAcc extends JFrame {
     private JButton back;

     public void wcAcc (){
		ImageIcon bcr = new ImageIcon(getClass().getResource("/images/bcr.jpg"));
		final JButton back = new JButton(bcr);
		back.setLocation(20,480);
		back.setSize(70,30);
		back.setPreferredSize(new Dimension(90,30));
		back.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
			dispose();
			}
		}
		);

		Container pane = getContentPane();
		pane.setLayout(null);
		pane.add(back);

		setIconImage(new ImageIcon("/images/icon.jpg").getImage());
		setLayout(new BorderLayout());
		JLabel www=new JLabel(new ImageIcon("/images/welcome.jpg"));
		add(www);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setTitle("Change Pin Code");
		setSize(650,550);
		setResizable(false);
		setVisible(true);
		setLocation(180, 5);
     }
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>