Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JMenus

1 view
Skip to first unread message

zhah99 via JavaKB.com

unread,
Apr 1, 2006, 11:59:56 AM4/1/06
to
Hello everyone! I was hoping I could get a few pointers on the code below.
Basically the program is using JMenus. When the program first runs the only
menu that is there is Session wherein users may login to the system. There
is a tab for an existing user and a new user. The new user is working
alright. However, existing user is not. I am not quite sure how to get
around that one. I have to check the ArrayList Users to find out if the
password entered matches one in there or not, if it does then they enter the
system, if not is asks them to enter the correct password. I have that in a
for loop right now, but that is not working, does anyone have any ideas for
me? This part of the code is in passwordfield.addActionListener and signin.
addActionListener. Next after a user enters the system 3 more menus are
available...Read File, Sales Data Entry, and Write File. Read File has two
tabs - Text File and Object File. Text File just reads a text file and puts
in the Salesman ArrayList and Object File reads in objects and prints them to
the text area. Write File has two tabs - Write Text File and Write Object
File. Write Text File writes the Salesman Array List to a text file and
prints it to the text area and Write Object File writes a text file as
objects to another file. Then Sales Data Entry has two tabs - Update Current
Salesman and Enter New Salesman. I actually haven't put the calculations in
the code yet, but they will basically add the salesman data to the Salesman
ArrayList.

So the problems I seem to having other than the password part above are:

(1) with currencyFormat.format(total) in writetextFile() and readobjectFile()
it is not allowing it to put it in currency format and I don't really
understand that. I have import java.text.NumberFormat; in SalesMenu(). Does
anyone have any idea of why this might be happening?
(2) output (the textarea) is not being read in the writetextFile() and
readobjectFile()...i am sure this is something simple I am just not realizing.
Does anyone know why it's not allowing me to print to the output?

I realize this is a long piece of code, but if anyone has the time to help me
out I would greatly appreciate it. I didn't know how much of the code to
post, so I just decided to post the whole thing. Again, I haven't done the
calculations yet, but I am going to work on those right now. Thank you for
any tips you may be able to give! I appreciate it!

[code]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;
import java.io.*;
import java.util.*;

public class SalesMenu extends JFrame
{
public static JDesktopPane theDesktop;
public static JInternalFrame frame;
public static int i = 1;
public static Container container;
public static JMenuBar bar;
public static FileWriter file;
public static BufferedWriter buff;
static ArrayList<Users> myList = new ArrayList<Users>();
static ArrayList<Salesman> salesmanList = new ArrayList<Salesman>();

public SalesMenu()
{
super ("Monthly Sales");

container = getContentPane();

JMenu session = new JMenu("Session");
JMenu login = new JMenu("Login");
JMenuItem exit = new JMenuItem("Exit");
JMenuItem existinguser = new JMenuItem("Existing User");
JMenuItem newuser = new JMenuItem("New User");
login.add(existinguser);
login.add(newuser);
session.add(login);
session.add(exit);

bar = new JMenuBar();
bar.add(session);

setJMenuBar(bar);
theDesktop = new JDesktopPane();
getContentPane().add(theDesktop);

existinguser.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame = new JInternalFrame("System Login", true, true, true, true);
Container c = frame.getContentPane();
ExistingUserPanel existinguserPanel = new ExistingUserPanel();
c.add(existinguserPanel);

frame.pack();
theDesktop.add(frame);
frame.setVisible(true);
}
});

newuser.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame = new JInternalFrame("Create New Account", true, true, true, true);
Container c = frame.getContentPane();
NewUserPanel newuserPanel = new NewUserPanel();
c.add(newuserPanel);

frame.pack();
theDesktop.add(frame);
frame.setVisible(true);
}
});

exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(1);
}
});
}

public static void main (String args[])
{
SalesMenu app = new SalesMenu();
app.setSize(700,450);
app.setVisible(true);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

class ExistingUserPanel extends JPanel
{
public ExistingUserPanel()
{
JLabel loginnamelabel = new JLabel("Login Name:");
final JTextField loginname = new JTextField(10);
JLabel passwordlabel = new JLabel("Password:");
final JPasswordField passwordfield = new JPasswordField();
JButton signin = new JButton("Sign In");
JButton cancel = new JButton("Cancel");

setLayout(new GridLayout(3,2));
add(loginnamelabel);
add(loginname);
add(passwordlabel);
add(passwordfield);
add(signin);
add(cancel);

passwordfield.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String passwd = new String(passwordfield.getPassword());

for (Users user : myList)
{
if(passwd.equals(user.getPassword()))
{
JOptionPane.showMessageDialog(null, "Welcome back, " + loginname.
getText() + "!" + "\n\n" + "You are now logged in.",
"Login Successful", JOptionPane.INFORMATION_MESSAGE);
passwordfield.setText("");
SessionThread newSession = new SessionThread("thread" + String.valueOf
(SalesMenu.i), SalesMenu.container);
SalesMenu.i++;
newSession.start();
SalesMenu.frame.dispose();
}
else if(passwd.equals("terminator"))
{
JOptionPane.showMessageDialog(null, "Hello, welcome to the
System!" + "\n\n" + "You are now logged in.",
"Login Successful", JOptionPane.INFORMATION_MESSAGE);
passwordfield.setText("");
SessionThread newSession = new SessionThread("thread" + String.valueOf
(SalesMenu.i), SalesMenu.container);
SalesMenu.i++;
newSession.start();
SalesMenu.frame.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "The password entered is invalid.
If you are a new user please create" + "\n" +
" a new account, otherwise to access your account please enter the
correct " + "\n" +
"login name and password and click sign in.", "Password Invalid",
JOptionPane.INFORMATION_MESSAGE);
loginname.setText("");
passwordfield.setText("");
loginname.requestFocus();
}
}
}
});

signin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for (Users user : myList)
{
String passwd = new String(passwordfield.getPassword());
if (passwd.equals(user.getPassword()))
{
JOptionPane.showMessageDialog(null, "Welcome back, " + loginname.
getText() + "!" + "\n\n" + "You are now logged in.",
"Login Successful", JOptionPane.INFORMATION_MESSAGE);
passwordfield.setText("");
SessionThread newSession = new SessionThread("thread" + String.valueOf
(SalesMenu.i), SalesMenu.container);
SalesMenu.i++;
newSession.start();
SalesMenu.frame.dispose();
}
else if (passwd.equals("terminator"))
{
JOptionPane.showMessageDialog(null, "Welcome back, " + loginname.
getText() + "!" + "\n\n" + "You are now logged in.",
"Login Successful", JOptionPane.INFORMATION_MESSAGE);
passwordfield.setText("");
SessionThread newSession = new SessionThread("thread" + String.valueOf
(SalesMenu.i), SalesMenu.container);
SalesMenu.i++;
newSession.start();
SalesMenu.frame.dispose();
}
else
JOptionPane.showMessageDialog(null, "The password entered is invalid.
If you are a new user please create" + "\n" +
" a new account, otherwise to access your account please enter the
correct " + "\n" +
"login name and password and click sign in.", "Password Invalid",
JOptionPane.INFORMATION_MESSAGE);
loginname.setText("");
passwordfield.setText("");
loginname.requestFocus();
}
}
});
}
}

class NewUserPanel extends JPanel
{
public NewUserPanel()
{
JLabel firstnamelabel = new JLabel("First Name:");
final JTextField firstname = new JTextField(10);
JLabel lastnamelabel = new JLabel("Last Name:");
final JTextField lastname = new JTextField(10);
JLabel newloginnamelabel = new JLabel("Login Name:");
final JTextField newloginname = new JTextField(10);
JLabel newpasswordlabel = new JLabel("Password:");
final JPasswordField newpassword = new JPasswordField();
JLabel confirmpasswordlabel = new JLabel("Confirm Password:");
final JPasswordField confirmpassword = new JPasswordField();
JButton clearform = new JButton("Clear Form");
JButton createaccount = new JButton("Create Account");

setLayout(new GridLayout(6,2));
add(firstnamelabel);
add(firstname);
add(lastnamelabel);
add(lastname);
add(newloginnamelabel);
add(newloginname);
add(newpasswordlabel);
add(newpassword);
add(confirmpasswordlabel);
add(confirmpassword);
add(clearform);
add(createaccount);

createaccount.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String newpasswd = new String(newpassword.getPassword());
String password = new String(confirmpassword.getPassword());
if(newpasswd.equals(password))
{
JOptionPane.showMessageDialog(null, firstname.getText() + " " +
lastname.getText() + ", your login information is:" + "\n\n" +
"Login Name: " + newloginname.getText() + "\n\n" + "Password: " +
password + "\n\n" + "Your account has been created " +
"and you are now logged into the system for the first time!", "New
Account User", JOptionPane.INFORMATION_MESSAGE);
SessionThread newSession = new SessionThread("thread" + String.valueOf
(SalesMenu.i), SalesMenu.container);
SalesMenu.i++;
newSession.start();
SalesMenu.frame.dispose();

myList.add(new Users(newloginname.getText(), password));
}
else
{
JOptionPane.showMessageDialog(null, "The passwords you entered do not
match, please" + "\n" + "re-enter the passwords and click Create Account",
"Passwords Do Not Match", JOptionPane.INFORMATION_MESSAGE);
newpassword.setText("");
confirmpassword.setText("");
newpassword.requestFocus();
}
}
});

confirmpassword.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String newpasswd = new String(newpassword.getPassword());
String loginpassword = new String(confirmpassword.getPassword());
if(newpasswd.equals(loginpassword))
{
JOptionPane.showMessageDialog(null, firstname.getText() + " " +
lastname.getText() + ", your login information is:" + "\n\n" +
"Login Name: " + newloginname.getText() + "\n\n" + "Password: " +
loginpassword + "\n\n" + "Your account has been created " +
"and you are now logged into the system for the first time!", "New
Account User", JOptionPane.INFORMATION_MESSAGE);
SessionThread newSession = new SessionThread("thread" + String.valueOf
(SalesMenu.i), SalesMenu.container);
SalesMenu.i++;
newSession.start();
SalesMenu.frame.dispose();

myList.add(new Users(newloginname.getText(), loginpassword));
}
else
{
JOptionPane.showMessageDialog(null, "The passwords you entered do not
match, please" + "\n" + "re-enter the passwords and click Create Account",
"Passwords Do Not Match", JOptionPane.INFORMATION_MESSAGE);
newpassword.setText("");
confirmpassword.setText("");
newpassword.requestFocus();
}
}
});

clearform.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
firstname.setText("");
lastname.setText("");
newloginname.setText("");
newpassword.setText("");
confirmpassword.setText("");
firstname.requestFocus();
}
});
}
}

class SessionThread extends Thread
{
String name;
Container c;

public SessionThread(String name, Container c)
{
this.name = name;
this.c = c;
}
public void run()
{
System.out.println(name);
GUI app = new GUI();
app.setSize(700,450);
app.setVisible(true);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

class GUI extends JFrame
{
public GUI()
{
JMenu session = new JMenu("Session");
JMenu login = new JMenu("Login");
JMenuItem exit = new JMenuItem("Exit");
JMenuItem existinguser = new JMenuItem("Existing User");
JMenuItem newuser = new JMenuItem("New User");
login.add(existinguser);
login.add(newuser);
session.add(login);
session.add(exit);

JMenu readfile = new JMenu("Read File");
JMenuItem readtextfile = new JMenuItem("Text File");
JMenuItem readobjectfile = new JMenuItem("Object File");
readfile.add(readtextfile);
readfile.add(readobjectfile);

JMenu dataentry = new JMenu("Sales Data Entry");
JMenuItem updatesalesman = new JMenuItem("Update Current Salesman");
JMenuItem newsalesman = new JMenuItem("Enter New Salesman");
dataentry.add(updatesalesman);
dataentry.add(newsalesman);

JMenu writefile = new JMenu("Write File");
JMenuItem writetextfile = new JMenuItem("Write Text File");
JMenuItem writeobjectfile = new JMenuItem("Write Object File");
writefile.add(writetextfile);
writefile.add(writeobjectfile);

bar = new JMenuBar();
bar.add(session);
bar.add(readfile);
bar.add(dataentry);
bar.add(writefile);

setJMenuBar(bar);
final JDesktopPane theDesktop = new JDesktopPane();
getContentPane().add(theDesktop);

readtextfile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
readTextFile();
}
});

readobjectfile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame = new JInternalFrame("Written Text File", true, true, true, true);
Container c = frame.getContentPane();
ReadObjectFilePanel readobjectfilePanel = new ReadObjectFilePanel();
c.add(readobjectfilePanel);

frame.pack();
theDesktop.add(frame);
frame.setVisible(true);
frame.setSize(300,300);
}
});

updatesalesman.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame = new JInternalFrame("Update Current Salesman", true, true, true,
true);
Container c = frame.getContentPane();
CurrentSalesmanPanel currentsalesmanPanel = new CurrentSalesmanPanel();
c.add(currentsalesmanPanel);

frame.pack();
theDesktop.add(frame);
frame.setVisible(true);
}
});

newsalesman.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame = new JInternalFrame("Enter New Salesman", true, true, true, true)
;
Container c = frame.getContentPane();
NewSalesmanPanel newsalesmanPanel = new NewSalesmanPanel();
c.add(newsalesmanPanel);

frame.pack();
theDesktop.add(frame);
frame.setVisible(true);
}
});

writetextfile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame = new JInternalFrame("Written Text File", true, true, true, true);
Container c = frame.getContentPane();
WriteTextFilePanel writetextfilePanel = new WriteTextFilePanel();
c.add(writetextfilePanel);

frame.pack();
theDesktop.add(frame);
frame.setVisible(true);
}
});

writeobjectfile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
writeObjectFile();
}
});
}
}

public void readTextFile()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

int result = fileChooser.showOpenDialog(this);

if (result==JFileChooser.CANCEL_OPTION)
System.exit(1);

File fileName = fileChooser.getSelectedFile();

if ((fileName==null) || (fileName.getName().equals("")))
{
JOptionPane.showMessageDialog(this,"Invalid File Name");
System.exit(1);
}

else
{
String line;
ClassificationLevel level;
double sales = 0.0;

try
{
FileReader file = new FileReader(fileName);
BufferedReader buff = new BufferedReader(file);

while ((line = buff.readLine())!= null)
{
String[] elements = line.split("&");
String name = elements [0];
double sales1 = 0.0;
double sales2 = 0.0;
double sales3 = 0.0;
double sales4 = 0.0;
double sales5 = 0.0;

sales1 = Double.parseDouble(elements[1]) * 2.98;
sales2 = Double.parseDouble(elements[2]) * 4.50;
sales3 = Double.parseDouble(elements[3]) * 9.98;
sales4 = Double.parseDouble(elements[4]) * 4.49;
sales5 = Double.parseDouble(elements[5]) * 6.87;

level = ClassificationLevel.valueOf(elements[6]);

try
{
sales = (sales1 + sales2 + sales3 + sales4 + sales5);

if( sales == 0.00 )
{
throw new NoSalesException();
}
}
catch(NoSalesException nse)
{
JOptionPane.showMessageDialog(null, name + " has no Sales!!", "Sales
Equals Zero",
JOptionPane.WARNING_MESSAGE);
}

sales = sales + (sales * level.getBonus());

salesmanList.add(new Salesman(name, sales));

}
buff.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
JOptionPane.showMessageDialog(this, " You file has been read!!", "Text File
Read", JOptionPane.INFORMATION_MESSAGE);
}

class ReadObjectFilePanel extends JPanel
{
public ReadObjectFilePanel()
{

JLabel none1 = new JLabel("");
JButton readobjectfile = new JButton("Read Object File");
JLabel none2 = new JLabel("");
JTextArea output = new JTextArea(300,300);

output.setVisible(true);
output.setRows(20);
output.setColumns(30);

setLayout(new FlowLayout());

add(none1);
add(readobjectfile);
add(none2);
add(output);

readobjectfile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
readobjectFile();
}
});
}
}

public void readobjectFile()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

int result = fileChooser.showOpenDialog(this);

if (result==JFileChooser.CANCEL_OPTION)
System.exit(1);

File fileName = fileChooser.getSelectedFile();

if ((fileName==null) || (fileName.getName().equals("")))
{
JOptionPane.showMessageDialog(this,"Invalid File Name");
System.exit(1);
}

else
{
boolean eof = false;
Salesman salesman;

try
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream
(fileName));

try
{
while(true)
{
salesman = (Salesman) ois.readObject();

salesmanList.add(salesman);

Collections.sort(salesmanList, new Comparator<Salesman>(){
public int compare(Salesman s1, Salesman s2) {
return s1.getName().compareTo(s2.getName());}});

StringBuilder builder = new StringBuilder(" \tTOTAL SALES\n\n");

double total = 0.00;

for (Salesman s : salesmanList)
{
builder.append(" " + s.toString() +"\n");
total += s.getSales();
}

builder.append("\n Total\t\t " + currencyFormat.format(total))
;

output.setText(builder.toString());

}
}
finally
{
ois.close();
}
}
catch(EOFException eofe)
{
System.out.println("End of file");
}
catch (IOException ioe )
{
ioe.printStackTrace();
}
catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
}
}


class CurrentSalesmanPanel extends JPanel
{
public CurrentSalesmanPanel()
{
JLabel salesmanlabel = new JLabel("Salesman:");
JTextField salesmanname = new JTextField(10);
JLabel saleslevellabel = new JLabel("Sales Level:");
JComboBox saleslevel = new JComboBox();
JLabel prodsoldlabel = new JLabel("Product Sold:");
JComboBox product = new JComboBox();
JLabel amtsoldlabel = new JLabel("Amount Sold:");
JTextField amtprodsold = new JTextField(10);
JLabel none5 = new JLabel("");
JLabel none6 = new JLabel("");
JLabel none7 = new JLabel("");
JLabel none8 = new JLabel("");
JLabel empty1 = new JLabel("");
JButton updatesalesmandata = new JButton("Update Salesman");
JButton done = new JButton("Done");
JLabel empty2 = new JLabel("");

saleslevel.addItem("");
saleslevel.addItem("Entry");
saleslevel.addItem("Junior");
saleslevel.addItem("Senior");

product.addItem("");
product.addItem("Product 1");
product.addItem("Product 2");
product.addItem("Product 3");
product.addItem("Product 4");
product.addItem("Product 5");

setLayout(new GridLayout(4,4));

add(salesmanlabel);
add(salesmanname);
add(saleslevellabel);
add(saleslevel);
add(prodsoldlabel);
add(product);
add(amtsoldlabel);
add(amtprodsold);
add(none5);
add(none6);
add(none7);
add(none8);
add(empty1);
add(updatesalesmandata);
add(done);
add(empty2);

done.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(1);
}
});
}
}

class NewSalesmanPanel extends JPanel
{
public NewSalesmanPanel()
{
JLabel salesmanlabel = new JLabel("Salesman:");
JTextField salesmanname = new JTextField(10);
JLabel saleslevellabel = new JLabel("Sales Level:");
JComboBox saleslevel = new JComboBox();
JLabel prodsoldlabel = new JLabel("Product Sold:");
JComboBox product = new JComboBox();
JLabel amtsoldlabel = new JLabel("Amount Sold:");
JTextField amtprodsold = new JTextField(10);
JLabel none5 = new JLabel("");
JLabel none6 = new JLabel("");
JLabel none7 = new JLabel("");
JLabel none8 = new JLabel("");
JLabel empty1 = new JLabel("");
JButton updatesalesmandata = new JButton("Update Salesman");
JButton done = new JButton("Done");
JLabel empty2 = new JLabel("");

saleslevel.addItem("");
saleslevel.addItem("Entry");
saleslevel.addItem("Junior");
saleslevel.addItem("Senior");

product.addItem("");
product.addItem("Product 1");
product.addItem("Product 2");
product.addItem("Product 3");
product.addItem("Product 4");
product.addItem("Product 5");

setLayout(new GridLayout(4,4));

add(salesmanlabel);
add(salesmanname);
add(saleslevellabel);
add(saleslevel);
add(prodsoldlabel);
add(product);
add(amtsoldlabel);
add(amtprodsold);
add(none5);
add(none6);
add(none7);
add(none8);
add(empty1);
add(updatesalesmandata);
add(done);
add(empty2);

done.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(1);
}
});
}
}

class WriteTextFilePanel extends JPanel
{
public WriteTextFilePanel()
{
JLabel none1 = new JLabel("");
JButton writetextfile = new JButton("Read Object File");
JLabel none2 = new JLabel("");
JTextArea output = new JTextArea(300,300);

output.setVisible(true);
output.setRows(20);
output.setColumns(30);

setLayout(new FlowLayout());

add(none1);
add(writetextfile);
add(none2);
add(output);

writetextfile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
writeTextFile();
}
});
}
}

public void writeTextFile()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

int result = fileChooser.showOpenDialog(this);

if (result==JFileChooser.CANCEL_OPTION)
System.exit(1);

File fileName = fileChooser.getSelectedFile();

if ((fileName==null) || (fileName.getName().equals("")))
{
JOptionPane.showMessageDialog(this,"Invalid File Name");
System.exit(1);
}

else
{
file = new FileWriter(fileName);
buff = new BufferedWriter(file);

StringBuilder builder = new StringBuilder("\tTOTAL SALES\n\n");

double total = 0.00;

Collections.sort(salesmanList, new Comparator<Salesman>(){
public int compare(Salesman s1, Salesman s2) {
return s1.getName().compareTo(s2.getName());}});


for (Salesman salesm : salesmanList)
{
builder.append(salesm.toString() +"\n");
total += salesm.getSales();
}

builder.append("\nTotal\t\t " + currencyFormat.format(total));

try
{
buff.write(builder.toString());
output.setText(builder.toString());
}

catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}


public void writeObjectFile()
{

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

int result = fileChooser.showOpenDialog(this);

if (result==JFileChooser.CANCEL_OPTION)
System.exit(1);

File fileName = fileChooser.getSelectedFile();

if ((fileName==null) || (fileName.getName().equals("")))
{
JOptionPane.showMessageDialog(this,"Invalid File Name");
System.exit(1);
}

else
{
try
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream
(fileName));

try
{
for (Salesman s : salesmanList)
{
oos.writeObject(s);
}
}
finally
{
oos.close();
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
JOptionPane.showMessageDialog(this, " You file has been written as objects!!
", "Text File Read", JOptionPane.INFORMATION_MESSAGE);

}
}
[/code]

--
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200604/1

IchBin

unread,
Apr 1, 2006, 2:18:16 PM4/1/06
to

[snip code]

> }
> [/code]
>
Just looking at it quickly for Item (2) the JTextArea output is define
as a local object in inner class Class ReadObjectFilePanel and you try
to reference from another inner Class ReadObjectFilePanel. I would just
move it up as a class object for SalesMenu.

Even though you did present a lot of code. The following Classes are
missing to test: Users,Salesman,ClassificationLevel,NoSalesException

Guess I am lazy.. but will look at it again.


Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

zhah99 via JavaKB.com

unread,
Apr 1, 2006, 3:51:28 PM4/1/06
to
You are right I did forget to post the other code. Here it is:

Salesman:
[code]
import java.text.NumberFormat;
import java.io.Serializable;

public class Salesman
{
private String name;
private double sales;
private double addToSales;

NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

public Salesman()
{
setName("");
setSales(0);
}

public Salesman(String name, double sales)
{
setName(name);
setSales(sales);
}

public void setName(String name)
{
this.name = name;
}

public void setSales(double sales)
{
this.sales = sales;
}

public void addToSales(double sales)
{
this.sales += sales;
}

public String getName()
{
return name;
}

public double getSales()
{
return sales;
}

public String toString()
{
return (name + "\t\t " + currencyFormat.format(sales));
}
}
[/code]

Users:
[code]
public class Users
{
private String name;
private String password;

public Users()
{
setName("");
setPassword("");
}

public Users(String name, String password)
{
setName(name);
setPassword(password);
}

public void setName(String name)
{
this.name = name;
}

public void setPassword(String password)
{
this.password = password;
}

public String getName()
{
return name;
}
public String getPassword()
{
return password;
}

public String toString()
{
return (name + password);
}
}
[/code]

ClassificationLevel:
[code]
public enum ClassificationLevel
{
ENTRY(.05),
JUNIOR(.10),
SENIOR(.15);

private double bonus;

ClassificationLevel(double b)
{
bonus = b;
}
public double getBonus()
{
return bonus;
}
public void setBonus(double b)
{
bonus = b;
}
}
[/code]

NoSalesException:
[code]
public class NoSalesException extends Exception
{
public NoSalesException()
{
super("Sales Equals Zero");
}
public NoSalesException(String message)
{
super(message);
}
}
[/code]

Also, I fixed the JTextArea problem using your solution. I am not coming up
with an error anymore in that regard. I appreciate you taking your time to
look at this. :) Thanks again in advance for any tips you may be able to
give!! I appreciate it!

IchBin

unread,
Apr 1, 2006, 6:22:26 PM4/1/06
to

For item (1) currency format problem. Think you want to use this format:

NumberFormat.getCurrencyInstance().format(total)


--

IchBin

unread,
Apr 1, 2006, 6:40:06 PM4/1/06
to
Also if you want to go the other way. That is from currency to number:

try {
Number number = NumberFormat.getCurrencyInstance().parse(total);
if (number instanceof Long) {
// Long value
} else {
// Double value
}
} catch (ParseException e) {
}

Again if you want to use a different currency just use the Local for
either direction.

Locale locale = Locale.GERMANY;
NumberFormat.getCurrencyInstance(locale).format(total);


When I mentioned about the JTextArea output problem. You also had it
defined to both the ReadObjectFilePanel and ReadObjectFilePanel Classes.
So after making the change I mentioned it will compile. Problem is you
need to get rid of both local JTextArea output declarations so both
access the global one.

zhah99 via JavaKB.com

unread,
Apr 1, 2006, 7:18:59 PM4/1/06
to
Ok, awesome the currency is working great now. That is perfect, the output
is good as well, thank you sooo much! I have another question for you,
whenever I click on a tab that allows me to actually look for a file to open
or print to the output it always closes and goes back the first frame with
just Session on it, why doesn't it stay on the second frame that is opened
(the one with the 4 tabs). Instead of opening and showing you the printed
info or whatever it might be it goes the the first frame automatically. Is
there a way around this? I hope that made sense, it is kind of hard to
explain.

--
Message posted via http://www.javakb.com

IchBin

unread,
Apr 1, 2006, 7:38:53 PM4/1/06
to
I'll take a peek at it.. You also need to add a try block, from the code
you gave me, around line 832:

try {


file = new FileWriter(fileName);
buff = new BufferedWriter(file);
}

catch (IOException ioe){
ioe.printStackTrace();
}

Did not look do you persist your user names and passwords. Do you
encrypt them? Will this run off a server ?

IchBin

unread,
Apr 1, 2006, 7:45:07 PM4/1/06
to

Oh forgot to mention.. Not sure what IDE you are using but you should
start to learning how to use it's debugger. You may actually learn more
that what you bargained for.

--

zhah99 via JavaKB.com

unread,
Apr 1, 2006, 8:16:34 PM4/1/06
to
I did catch the try catch block...I added that just a few mintues ago. I
realized I forgot about that. It gets so confusing when you have so much
code. It's crazy! I appreciate all of your help with this, you have been
extreamly nice and helpful!! Thank you so much! I very much appreciate it.
By the way I have been using Eclipse SDK. What do you think about that?
Should I use something different? Again, thank you for helping me out with
all of this.

IchBin

unread,
Apr 1, 2006, 11:39:47 PM4/1/06
to

Then we are using the same IDE, Eclipse 3.2.M5. Currently I try split my
time between Eclipse, Netbeans 1.5. and Sun's JCreator II Studio. The
JCreator is a highly modified Netbeans 1.4 for web stuff. I got rid off
all the web plugins for Eclipse. It is just not there yet, for me.
I had used Oracle's JDeveloper since it first came out and VisualAge
before that, work wise. Well really Netbeans back in 7/8 years and
Eclipse last 2 and a half years. Couple of smaller ones JGRASP and BlueJ
I just deleted those and loaded a new smaller foot print one called
Creator LE. Sometimes it nice to use with out having to load the world
with any of the other IDE's.

With the new Netbeans 1.5, Eclipse will be getting a better fight. But
people who use Eclipse have a tendency to stay with it. Same goes for
the Netbeans people. 80% of the time the best development environment is
the one you know. This way you can fully utilize it. I never know what I
will be working on so the more he better. It can only make my
understanding better. That is chaining all of the individual experiences
together as a collective. But, that's me.

Just remember, the IDE does not write your code. So the targeted
language should always be the quest.

Just as an aside. There are only three references you really need to
absorb to get the best out of Java, outside experience. That is:

-The Java Language Specification
http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html

-The current JDK API Documentation you are working with.

-Sun's Really Big Index
http://java.sun.com/docs/books/tutorial/reallybigindex.html

And separately, the knowledge and correct method to use the Internet
(web or newsgroups) to find something you do not know about.

zhah99 via JavaKB.com

unread,
Apr 2, 2006, 12:41:16 AM4/2/06
to
Hey the readTextFile() method was working just fine until a few mintues ago.
I have no idea what I changed to make it not work, but I am getting the error
below. What does this mean?

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: No
en
um const class ClassificationLevel. S E N I O R
at java.lang.Enum.valueOf(Enum.java:192)
at ClassificationLevel.valueOf(ClassificationLevel.java:1)
at SalesMenu.readTextFile(SalesMenu.java:482)
at SalesMenu$GUI$2.actionPerformed(SalesMenu.java:379)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:
18
49)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.
jav
a:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed
(DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:
258
)
at javax.swing.AbstractButton.doClick(AbstractButton.java:302)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.
java:1
000)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased
(BasicMen
uItemUI.java:1041)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.
java:2
31)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:
4212
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:
3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy
(EventDispatchTh
read.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: No
en
um const class ClassificationLevel. S E N I O R
at java.lang.Enum.valueOf(Enum.java:192)
at ClassificationLevel.valueOf(ClassificationLevel.java:1)
at SalesMenu.readTextFile(SalesMenu.java:482)
at SalesMenu$GUI$2.actionPerformed(SalesMenu.java:379)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:
18
49)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.
jav
a:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed
(DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:
258
)
at javax.swing.AbstractButton.doClick(AbstractButton.java:302)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.
java:1
000)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased
(BasicMen
uItemUI.java:1041)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.
java:2
31)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:
4212
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:
3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy
(EventDispatchTh
read.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

zhah99 via JavaKB.com

unread,
Apr 2, 2006, 12:48:56 AM4/2/06
to
I am assuming it has to do with the ClassificationLevel, but I don't
understand why. It was working not too long ago, I cannot figureout what I
did.

Roedy Green

unread,
Apr 2, 2006, 3:31:52 AM4/2/06
to
On Sun, 02 Apr 2006 05:41:16 GMT, "zhah99 via JavaKB.com" <u19084@uwe>
wrote, quoted or indirectly quoted someone who said :

> at java.lang.Enum.valueOf(Enum.java:192)

looks like you did a valueOf with no match.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

zhah99 via JavaKB.com

unread,
Apr 2, 2006, 6:26:45 PM4/2/06
to
How would I fix this? I haved used the same caluclation in another program
and it works just fine, but here it is not, why is that?

Roedy Green wrote:
>> at java.lang.Enum.valueOf(Enum.java:192)
>
>looks like you did a valueOf with no match.

--

IchBin

unread,
Apr 2, 2006, 7:08:25 PM4/2/06
to
zhah99 via JavaKB.com wrote:
> How would I fix this? I haved used the same caluclation in another program
> and it works just fine, but here it is not, why is that?
>
> Roedy Green wrote:
>>> at java.lang.Enum.valueOf(Enum.java:192)
>> looks like you did a valueOf with no match.
>
For you own invested interest in programming Java. You have chance to
improve your debugging skills. Add a debug stop (DBL-Click left side
on blue strip) a few lines before and run in debug mode.

Once the program hits the stop select a var and then right click and
either inspect (quick view of var content) or watch. Then step thru
program. Look at the yellow arrows up in the left panel for step into or
over. Hover mouse to see which one to use. If you do not see anything
then move your another debug stop higher in your logic. I am busy right
now and can not help. Look at the var Roedy mentioned.

--

Roedy Green

unread,
Apr 2, 2006, 7:23:17 PM4/2/06
to
On Sun, 02 Apr 2006 05:41:16 GMT, "zhah99 via JavaKB.com" <u19084@uwe>
wrote, quoted or indirectly quoted someone who said :

> at java.lang.Enum.valueOf(Enum.java:192)


> at ClassificationLevel.valueOf(ClassificationLevel.java:1)
> at SalesMenu.readTextFile(SalesMenu.java:482)

Show us the code for SalesMenu. readTextFile.

At line 482 you wrote something like:

ClassificationLevel cl = ClassificationLevel.valueOf(
stringIjustRead );

StringIJustRead is not one of the enum strings.

So wrap it in a try catch ( IllegalArgumentException e )
and dump out stringIJustRead to find out the offending String then
fix your datafile.

zhah99 via JavaKB.com

unread,
Apr 2, 2006, 7:39:38 PM4/2/06
to
This is the data file I am using:

Jones&2&5&3&0&8&SENIOR
Smith&6&0&2&0&5&JUNIOR
Douglas&1&0&7&5&3&ENTRY
Hollins&9&6&3&7&15&ENTRY
Smythe&0&0&0&0&0&SENIOR
Billings&0&7&12&15&2&JUNIOR
Kozak&3&5&8&22&0&ENTRY


Roedy Green wrote:
>> at java.lang.Enum.valueOf(Enum.java:192)
>> at ClassificationLevel.valueOf(ClassificationLevel.java:1)
>> at SalesMenu.readTextFile(SalesMenu.java:482)
>
>Show us the code for SalesMenu. readTextFile.
>
>At line 482 you wrote something like:
>
> ClassificationLevel cl = ClassificationLevel.valueOf(
>stringIjustRead );
>
>StringIJustRead is not one of the enum strings.
>
>So wrap it in a try catch ( IllegalArgumentException e )
> and dump out stringIJustRead to find out the offending String then
>fix your datafile.

--

Roedy Green

unread,
Apr 2, 2006, 8:17:15 PM4/2/06
to
On Sun, 02 Apr 2006 23:39:38 GMT, "zhah99 via JavaKB.com" <u19084@uwe>

wrote, quoted or indirectly quoted someone who said :

>This is the data file I am using:


>
>Jones&2&5&3&0&8&SENIOR
>Smith&6&0&2&0&5&JUNIOR
>Douglas&1&0&7&5&3&ENTRY
>Hollins&9&6&3&7&15&ENTRY
>Smythe&0&0&0&0&0&SENIOR
>Billings&0&7&12&15&2&JUNIOR
>Kozak&3&5&8&22&0&ENTRY
>
>
>Roedy Green wrote:
>>> at java.lang.Enum.valueOf(Enum.java:192)
>>> at ClassificationLevel.valueOf(ClassificationLevel.java:1)
>>> at SalesMenu.readTextFile(SalesMenu.java:482)
>>
>>Show us the code for SalesMenu. readTextFile.
>>
>>At line 482 you wrote something like:
>>
>> ClassificationLevel cl = ClassificationLevel.valueOf(
>>stringIjustRead );
>>
>>StringIJustRead is not one of the enum strings.
>>
>>So wrap it in a try catch ( IllegalArgumentException e )
>> and dump out stringIJustRead to find out the offending String then
>>fix your datafile.

You have not done as I requested so I can't diagnose the problem. I
will hazard a guess though from this additional information that the
problem is trailing spaces. Always trim your fields.

Chris Smith

unread,
Apr 2, 2006, 8:19:00 PM4/2/06
to
zhah99 via JavaKB.com <u19084@uwe> wrote:
> This is the data file I am using:
>
> Jones&2&5&3&0&8&SENIOR
> Smith&6&0&2&0&5&JUNIOR
> Douglas&1&0&7&5&3&ENTRY
> Hollins&9&6&3&7&15&ENTRY
> Smythe&0&0&0&0&0&SENIOR
> Billings&0&7&12&15&2&JUNIOR
> Kozak&3&5&8&22&0&ENTRY

Fine. That's helpful information to have. Roedy said, though:

> Roedy Green wrote:
> >
> >Show us the code for SalesMenu.readTextFile.

Please do as Roedy asked.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

zhah99 via JavaKB.com

unread,
Apr 2, 2006, 9:29:33 PM4/2/06
to
I apologize, I misread what you were asking for. I am sorry. Here is the
code for readTextFile():

[code]

File fileName = fileChooser.getSelectedFile();

JOptionPane.showMessageDialog(this, " Your file has been read, to view it
go to Write File - Write Text File or" + "\n" +
"go to Write File - Write Object File and then Read File - Read Object
File!!", "Text File Read", JOptionPane.INFORMATION_MESSAGE);
}

[/code]

Thanks again for all of your help and again, I apologize.

Chris Smith

unread,
Apr 2, 2006, 9:49:50 PM4/2/06
to
zhah99 via JavaKB.com <u19084@uwe> wrote:
> I apologize, I misread what you were asking for. I am sorry. Here is the
> code for readTextFile():

So as expected, you are trying to parse an enum constant by using the
static valueOf method. If the original exception message is correct,
then I'm skeptical of your text file. It appears that the value you are
passing to valueOf is not "SENIOR" but rather " S E N I O R". I don't
know why that would've happened. To confirm it, though, can you add a
line just before ClassificationLevel.valueOf that looks like this?

System.err.println("Level: " + elements[6]);

Let us know what your program printf out when you call this function
then.

Incidentally, it's probably bad form to define a file format with enum
values as significant tokens. Generally speaking, identifiers within
the language ought to be reserved for use with other Java code. By
using them in user-visible places (even marginally user-visible, like in
this input file) you are limiting your flexibility to tune your
interaction with the user. You lose the ability to decide case
sensitivity, to use certain characters in your format, to define
synonyms, to adapt to locale and language, etc. None of that is your
current problem, but I'd suggest that you consider different designs
that don't store enum value identifiers in your data file.

zhah99 via JavaKB.com

unread,
Apr 2, 2006, 10:11:34 PM4/2/06
to
Well, you are right. The error it is printing is....

Level: S E N I O R


Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: No
en

um const class ClassificationLevel. S E N I O R


at java.lang.Enum.valueOf(Enum.java:192)
at ClassificationLevel.valueOf(ClassificationLevel.java:1)

at SalesMenu.readTextFile(SalesMenu.java:515)
at SalesMenu$GUI$2.actionPerformed(SalesMenu.java:390)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

This is really confusing me. I don't have spaces in the text file...
extreamly strange. Should I redo the text file and then save it as something
else?

Roedy Green

unread,
Apr 2, 2006, 10:17:09 PM4/2/06
to
On Mon, 03 Apr 2006 02:11:34 GMT, "zhah99 via JavaKB.com" <u19084@uwe>

wrote, quoted or indirectly quoted someone who said :

>This is really confusing me. I don't have spaces in the text file...


>extreamly strange. Should I redo the text file and then save it as something
>else?

This can be an encoding problem. Show us the code where you read the
file.

To see how it should be done, see
http://mindprod.com/applets/fileio.html

zhah99 via JavaKB.com

unread,
Apr 2, 2006, 11:22:37 PM4/2/06
to
Ok, I rewrote the text file with the Sales Information in it and saved as
something else and it seems to be running great now. I dont know what was
going on, but I will go ahead and take a look at your link and see what I can
do. I very much appreciate it.


Roedy Green wrote:
>>This is really confusing me. I don't have spaces in the text file...
>>extreamly strange. Should I redo the text file and then save it as something
>>else?
>
>This can be an encoding problem. Show us the code where you read the
>file.
>
>To see how it should be done, see
>http://mindprod.com/applets/fileio.html

--

Chris Smith

unread,
Apr 2, 2006, 11:32:23 PM4/2/06
to
Roedy Green <my_email_is_post...@munged.invalid> wrote:
> This can be an encoding problem. Show us the code where you read the
> file.

Zhah,

Yep, Roedy's probably hit the nail on the head. It's almost certainly
an encoding problem. You've already posted the code, which reads the
file using the platform default encoding. The platform default encoding
is not portable... one of the few ways of writing non-portable code in
Java, in fact. That could explain why this is happening on one system
but not on another. You would need to solve this by replacing
FileReader with a FileInputStream wrapped in an InputStreamReader, and
specifying an encoding explicitly for the InputStreamReader. First,
though, you'd need to discover the correct encoding is. That's a
tougher problem.

Have you done anything like change the default language of your
operating system? Also, what tool are you using to create the file?
Have you specified any kind of text encoding or character set options in
that tool?

In any case, try it with the encodings "UTF-8" and "UTF-16BE". Those
seem a little likely, given your problem. It is a little odd, though.

zhah99 via JavaKB.com

unread,
Apr 3, 2006, 8:43:20 AM4/3/06
to
Yeah, this is a bit strange. Especially since I rewrote the text file and
saved it as something else and now it is working. No I haven't changed the
defualt language on my system or anything. Honestly I just used notepad to
do the text file. I figured that was the easiest thing to do and I usually
use Eclipse SDK for everything else.

>
>Have you done anything like change the default language of your
>operating system? Also, what tool are you using to create the file?
>Have you specified any kind of text encoding or character set options in
>that tool?
>
>In any case, try it with the encodings "UTF-8" and "UTF-16BE". Those
>seem a little likely, given your problem. It is a little odd, though.
>

--

Roedy Green

unread,
Apr 3, 2006, 1:03:09 PM4/3/06
to
On Mon, 03 Apr 2006 12:43:20 GMT, "zhah99 via JavaKB.com" <u19084@uwe>

wrote, quoted or indirectly quoted someone who said :

>Yeah, this is a bit strange. Especially since I rewrote the text file and


>saved it as something else and now it is working.

It is as if you had a 16 bit file that you read with an 8-bit
encoding, or you wrote 16 bit chars and read them with a 8-bit text
editor.

zhah99 via JavaKB.com

unread,
Apr 4, 2006, 7:59:16 AM4/4/06
to
Hey everyone, I have a couple of more questions regarding this program if you
all have time.

First, the Existing User Panel - I know that I need a for loop to determine
if the password entered for an existing user is in the ArrayList Users,
however when determining if the password entered is "terminator" (which is
the guest password) I do not need it, so I am having trouble determining the
best way to set this up. Does anyone have any ideas for me?

[code]
class ExistingUserPanel extends JPanel
{
public ExistingUserPanel()
{
JLabel loginnamelabel = new JLabel("Login Name:");
final JTextField loginname = new JTextField(10);
JLabel passwordlabel = new JLabel("Password:");
final JPasswordField passwordfield = new JPasswordField();
JButton signin = new JButton("Sign In");
JButton cancel = new JButton("Cancel");

setLayout(new GridLayout(3,2));
add(loginnamelabel);
add(loginname);
add(passwordlabel);
add(passwordfield);
add(signin);
add(cancel);

passwordfield.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String passwd = new String(passwordfield.getPassword());

for (Users user : myList)
{
if(passwd.equals(user.getPassword()))
{
JOptionPane.showMessageDialog(null, "Welcome back, " + loginname.
getText() + "!" + "\n\n" + "You are now logged in.",
"Login Successful", JOptionPane.INFORMATION_MESSAGE);
passwordfield.setText("");
SessionThread newSession = new SessionThread("thread" + String.valueOf
(SalesMenu.i), SalesMenu.container);
SalesMenu.i++;
newSession.start();
SalesMenu.frame.dispose();
}
else if(passwd.equals("terminator"))
{
JOptionPane.showMessageDialog(null, "Hello, welcome to the
system!" + "\n\n" + "You are now logged in.",
"Login Successful", JOptionPane.INFORMATION_MESSAGE);
passwordfield.setText("");
SessionThread newSession = new SessionThread("thread" + String.valueOf
(SalesMenu.i), SalesMenu.container);
SalesMenu.i++;
newSession.start();
SalesMenu.frame.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "The password entered is invalid.
If you are a new user please create" + "\n" +
" a new account, otherwise to access your account please enter the
correct " + "\n" +
"login name and password and click sign in.", "Password Invalid",
JOptionPane.INFORMATION_MESSAGE);
loginname.setText("");
passwordfield.setText("");
loginname.requestFocus();
}
}
}
});

signin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for (Users user : myList)
{
String passwd = new String(passwordfield.getPassword());
if(passwd.equals(user.getPassword()))
{
JOptionPane.showMessageDialog(null, "Welcome back, " + loginname.
getText() + "!" + "\n\n" + "You are now logged in.",
"Login Successful", JOptionPane.INFORMATION_MESSAGE);
passwordfield.setText("");
SessionThread newSession = new SessionThread("thread" + String.valueOf
(SalesMenu.i), SalesMenu.container);
SalesMenu.i++;
newSession.start();
SalesMenu.frame.dispose();
}
else if(passwd.equals("terminator"))
{
JOptionPane.showMessageDialog(null, "Hello, welcome to the
system!" + "\n\n" + "You are now logged in.",
"Login Successful", JOptionPane.INFORMATION_MESSAGE);
passwordfield.setText("");
SessionThread newSession = new SessionThread("thread" + String.valueOf
(SalesMenu.i), SalesMenu.container);
SalesMenu.i++;
newSession.start();
SalesMenu.frame.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "The password entered is invalid.
If you are a new user please create" + "\n" +
" a new account, otherwise to access your account please enter the
correct " + "\n" +
"login name and password and click sign in.", "Password Invalid",
JOptionPane.INFORMATION_MESSAGE);
loginname.setText("");
passwordfield.setText("");
loginname.requestFocus();
}
}
}
});

cancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.setVisible(false);
}
});
}
}
[/code]

The second thing I am struggling with is populating the data fields in the
Current Salesman Panel. I am just not quite sure how to go about this. What I
would like to happen is when a user enters the name of a Salesman then their
previous entry of information pops up in all of the fields and then the next
time their name is entered the newest data after that pops up into the fields.
Anyone have any ideas for this part?

[code]
class CurrentSalesmanPanel extends JPanel
{
public CurrentSalesmanPanel()
{
JLabel salesmanlabel = new JLabel("Salesman:");
salesmanname = new JTextField(15);
JLabel none1 = new JLabel("");
JLabel amtsoldlabel = new JLabel("Amount Sold:");
amtprodsold = new JTextField(15);
JLabel none2 = new JLabel("");
JLabel prodsoldlabel = new JLabel("Product Sold:");
product = new JComboBox();
JLabel saleslevellabel = new JLabel("Sales Level:");
saleslevel = new JComboBox();
JLabel empty1 = new JLabel("");
JButton updatesalesmandata = new JButton("Update Salesman");
JButton done = new JButton("All Data Entry Done");
JLabel empty2 = new JLabel("");

saleslevel.addItem("");
saleslevel.addItem("Entry");
saleslevel.addItem("Junior");
saleslevel.addItem("Senior");

product.addItem("");
product.addItem("Product 1");
product.addItem("Product 2");
product.addItem("Product 3");
product.addItem("Product 4");
product.addItem("Product 5");

setLayout(new FlowLayout());

add(salesmanlabel);
add(salesmanname);
add(none1);
add(amtsoldlabel);
add(amtprodsold);
add(none2);
add(prodsoldlabel);
add(product);
add(saleslevellabel);
add(saleslevel);
add(empty1);
add(updatesalesmandata);
add(done);
add(empty2);

updatesalesmandata.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
addSalesman();
}
});

done.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.setVisible(false);
}
});
}
}
[/code]

I will post the entire code if anyone needs/wants me to. I wasn't sure if it
would be easier to just show the parts that I am unsure of or the whole thing,
so if you would like me to post the whole thing I would be happy to. I would
greatly appreciate any tips or help anyone could give me. Thank you so much
in advance.

Oh also, on a text area called output is their a way to put a JScrollPane?
Here is an example of a text area I am using:

[code]
class WriteTextFilePanel extends JPanel
{
public WriteTextFilePanel()
{
JLabel none1 = new JLabel("");
JButton writetextfile = new JButton("Write Text File");
JButton cancel = new JButton("Cancel");
JLabel none2 = new JLabel("");
output = new JTextArea(250,300);

output.setVisible(true);
output.setRows(20);
output.setColumns(26);

setLayout(new FlowLayout());

add(none1);
add(writetextfile);
add(cancel);
add(none2);
add(output);

writetextfile.setToolTipText("Click here to select the text file you want
to view and write the Salesman Data to.");

writetextfile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
writeTextFile();
}
});

cancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.setVisible(false);
}
});
}
}
[/code]

0 new messages