5 Replies - 479 Views - Last Post: 25 June 2009 - 06:04 PM Rate Topic: -----

#1 Solris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-November 08

Need help with this application i wrote

Posted 24 June 2009 - 12:19 PM

Hi all.


need help with this code i wrote. it executes but not like i want it too. A folder is suppost install on the desktop before it ask the user to log in, but in my case its not installing it till after. The other promblem is once the user logs in the folder on the desktop should open, displaying the contents in the folder, but thats not happening. can any one tell me what im doing wrong and show me how to correct it. I have been working this issue for the last two day with no luck.

i tried having the file created before before the doMyAction is performed, but it gives tons of errors.

Hope someone can help


package javaapplication32;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class BoxX {
private static JTextField user;
private static JPasswordField password;
public static void main(String args[]) {
JFrame f = new JFrame("BoxX");

Container content = f.getContentPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
content.setLayout(new GridLayout(3, 1));

JButton mybtn = new JButton("Log in");
mybtn.addActionListener(
new ActionListener()




{
public void actionPerformed(ActionEvent e) {
doMyAction();
}
}
);
user = new JTextField(15);
password = new JPasswordField(15);
panel1.add(new JLabel("Username"));
panel1.add(user);
panel2.add(new JLabel("Password"));
panel2.add(password);
panel3.add(mybtn);

content.add(panel1);
content.add(panel2);
content.add(panel3);
f.setSize(300, 200);
f.setVisible(true);
}

public static void doMyAction() {

if (user.getText().equals ("Solris") &&
password.getText().equals ("hi123"))  {
JOptionPane.showMessageDialog (null, "You may enter");
}


File data = new File("C:/Users/Solris/Desktop/data");
		data.mkdir();

		try {
			FileWriter d = new FileWriter(data);
		} catch (IOException e) {
			e.printStackTrace();

		}


File f = null;
Scanner scan = null;
try{
   f = new File("path");
  scan = new Scanner(f);
}
catch(Exception e){
   System.out.println("Exception");
   System.exit(0);
}

  





JOptionPane.showMessageDialog(null, "You May Not Enter", "NOT GOOD", JOptionPane.PLAIN_MESSAGE); //display incorrect password message

}


}


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with this application i wrote

#2 jpete7683  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 80
  • Joined: 18-April 09

Re: Need help with this application i wrote

Posted 24 June 2009 - 06:54 PM

Its happening because you put it under actionPerformed(ActionEvent e) which is going to wait for an event to happen before it executes any of the code. You should create another method that does the creation of the Directory and call it in Main. See below:
public static void createDir(){
	File data = new File("<directory>");
	data.mkdir();
	try {
		FileWriter d = new FileWriter(data);
	} catch (IOException e) {
		e.printStackTrace();
	}
	File f = null;
	Scanner scan = null;
	try{
		f = new File("path");
		scan = new Scanner(f);
	}
	catch(Exception e){
		System.out.println("Exception");
		System.exit(0);
}
}

public static void doAuthentication() {
		if (user.getText().equals ("Solris") &&
				password.getText().equals ("hi123"))  {
				JOptionPane.showMessageDialog (null, "You may enter");
				}
JOptionPane.showMessageDialog(null, "You May Not Enter", "NOT GOOD", JOptionPane.PLAIN_MESSAGE); //display incorrect password message
}



So as you can see above I made the doAuthentication which is what you call in your ActionEvent and you can call the createDir in your main which will create the directory as soon as the application is launched

This post has been edited by jpete7683: 24 June 2009 - 07:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 jpete7683  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 80
  • Joined: 18-April 09

Re: Need help with this application i wrote

Posted 24 June 2009 - 07:07 PM

Also for the directory opening after authentication you can use the link provided below to help you write that code. It's pretty straight forward and easy.

http://java.sun.com/...ilechooser.html

This post has been edited by jpete7683: 24 June 2009 - 07:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 Solris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-November 08

Re: Need help with this application i wrote

Posted 25 June 2009 - 09:32 AM

Hey Jpete,





Thx for ur relpy to my issue. I tried using the example you gave to open the file, but it isnt working, it won't open the file. what else can i do?


File f = null;
	Scanner scan = null;
	try{
		f = new File("path");
		scan = new Scanner(f);
	}
	catch(Exception e){
		System.out.println("Exception");
		System.exit(0);
}


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Need help with this application i wrote

Posted 25 June 2009 - 01:41 PM

What errors are you getting? You need to be specific if you expect us to help you.
Was This Post Helpful? 0
  • +
  • -

#6 jpete7683  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 80
  • Joined: 18-April 09

Re: Need help with this application i wrote

Posted 25 June 2009 - 06:04 PM

View PostSolris, on 25 Jun, 2009 - 08:32 AM, said:

Hey Jpete,





Thx for ur relpy to my issue. I tried using the example you gave to open the file, but it isnt working, it won't open the file. what else can i do?


File f = null;
	Scanner scan = null;
	try{
		f = new File("path");
		scan = new Scanner(f);
	}
	catch(Exception e){
		System.out.println("Exception");
		System.exit(0);
}



What are you trying to open? You are looking for a file called "path". You actually need to put a path in there to the file you are wanting to open or create a file called "path" in your current working directory. Your exception probably says unable to find file.
f = new File("path");



Exception Message:
java.io.FileNotFoundException:  (No such file or directory)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:137)
	at java.io.FileInputStream.<init>(FileInputStream.java:96)
	at java.io.FileReader.<init>(FileReader.java:58)
	at Testwow.readFile(Testwow.java:25)
	at Testwow.<init>(Testwow.java:13)
	at Testwow.main(Testwow.java:45)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at Testwow.main(Testwow.java:46)


This post has been edited by jpete7683: 25 June 2009 - 06:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1