please help me

hello.

i am having trouble logging on to the video library system that i just developed. the LogOn program works to a certain extent (program shown below). when i try to log onto my system time. a JOptionPane.ERROR_MESSAGE message appears on screen. when i press OK inside the message, the message disappears but the same one appears again. i press OK again + the JOptionPane messages stop appearing again. when i press ok after entering my login details in the text + password fields, a main menu (either an administrator menu or a user menu) should be displayed on screen. how can i get this program to do what it should be doing?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass LogOnextends JFrame{

JPanel pnlBody, pnlFooter;

JLabel unLabel =new JLabel("Username: ");

JLabel pwLabel =new JLabel("Password: ");

JTextField jtfUN =new JTextField(20);

JPasswordField jtfPW =new JPasswordField(20);

JButton jbOK =new JButton("OK");

JButton jbCancel =new JButton("Cancel");

Container contentpane;

public LogOn(){

super("Welcome to Home Entertainment");

contentpane = getContentPane();

contentpane.setLayout(new BorderLayout());

pnlBody =new JPanel();

pnlFooter =new JPanel();

pnlBody.add(unLabel);

pnlBody.add(jtfUN);

pnlBody.add(pwLabel);

pnlBody.add(jtfPW);

pnlFooter.add(jbOK);

pnlFooter.add(jbCancel);

contentpane.add(pnlBody,BorderLayout.NORTH);

contentpane.add(pnlFooter,BorderLayout.CENTER);

pack();

setVisible(true);

jbOK.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

String username;

String password;

String[] userUsernameArray ={"Ann Smyth","John Murphy"};

String[] userPasswordArray ={"1","2"};

String[] adminUsernameArray ={"Administrator"};

String[] adminPasswordArray ={"0"};

username = jtfUN.getText().trim();

password =new String(jtfPW.getPassword());

if(username.equals(userUsernameArray) && password.equals(userPasswordArray)){

setVisible(false);

UserMainMenu umm =new UserMainMenu();

umm.setVisible(true);

}

else{

JOptionPane.showMessageDialog(null,"Error\n\nYou have entered an incorrect username and/or password\nPlease try again", null, JOptionPane.ERROR_MESSAGE);

}

if(username.equals(adminUsernameArray) && password.equals(adminPasswordArray)){

setVisible(false);

AdminMainMenu amm =new AdminMainMenu();

amm.setVisible(true);

}

else{

JOptionPane.showMessageDialog(null,"Error\n\nYou have entered an incorrect username and/or password\nPlease try again", null, JOptionPane.ERROR_MESSAGE);

}

}

});

jbCancel.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

setVisible(false);

}

});

}

}

[5286 byte] By [james-mcfaddena] at [2007-11-27 10:56:28]
# 1

Your two if clauses in the ActionListener are both always executed. You want an if-then-else structure.

Or you want to define a User class, which contains the type of user (admin or regular), and just run through the list of Users to find a match, and then load the appropriate menu based on their access.

BTW, please make your examples self-contained and executable. This one had no main() method, and two references to classes that were not included (and not necessary) in this example.

mbmerrilla at 2007-7-29 12:03:08 > top of Java-index,Desktop,Core GUI APIs...
# 2

hello mbmerrill.

there is only one main() method (in the HomeEntertainment class) used in my video library project (slightly modified LogOn class is shown below along with the other 3 classes). the slight modification that i made to the LogOn class was that i put in an if, an else-if and an if instead of 2 if's and 2 else's. when i try to log on to my system this time, i only get one problem thrown my way. when i enter my login details and press OK, i get one error message instead of a menu. how can i get rid of this problem?

thanks for your help.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

public class HomeEntertainment extends JPanel implements ActionListener{

private JTabbedPane jtp = new JTabbedPane();

private JPanel cP1 = new JPanel();

private JPanel cP2 = new JPanel();

private JPanel cP3 = new JPanel();

private JPanel bP1 = new JPanel();

private JPanel bP2 = new JPanel();

private JPanel bP3 = new JPanel();

private JButton jbAdministratorLogOn = new JButton("Log On");

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

private JButton jbExistingUserLogOn = new JButton("Log On");

private JButton jbAdministratorExitTheSystem = new JButton("Exit the system");

private JButton jbNewUserExitTheSystem = new JButton("Exit the system");

private JButton jbExistingUserExitTheSystem = new JButton("Exit the system");

public HomeEntertainment(){

cP1.setLayout(new BorderLayout());

cP2.setLayout(new BorderLayout());

cP3.setLayout(new BorderLayout());

bP1.setBorder(new TitledBorder("Make a choice"));

bP2.setBorder(new TitledBorder("Make a choice"));

bP3.setBorder(new TitledBorder("Make a choice"));

bP1.add(jbAdministratorLogOn);

bP2.add(jbNewUserRegister);

bP3.add(jbExistingUserLogOn);

bP1.add(jbAdministratorExitTheSystem);

bP2.add(jbNewUserExitTheSystem);

bP3.add(jbExistingUserExitTheSystem);

cP1.add(bP1, BorderLayout.SOUTH);

cP2.add(bP2, BorderLayout.SOUTH);

cP3.add(bP3, BorderLayout.SOUTH);

jtp.addTab("Administrator", cP1);

jtp.addTab("New User", cP2);

jtp.addTab("Existing User", cP3);

JFrame jf = new JFrame("Home Entertainment");

jf.getContentPane().add(jtp, BorderLayout.CENTER);

jf.setSize(500, 500);

jf.setVisible(true);

jbAdministratorLogOn.addActionListener(this);

jbNewUserRegister.addActionListener(this);

jbExistingUserLogOn.addActionListener(this);

jbAdministratorExitTheSystem.addActionListener(this);

jbNewUserExitTheSystem.addActionListener(this);

jbExistingUserExitTheSystem.addActionListener(this);

}

public void actionPerformed(ActionEvent e){

if(e.getSource() == jbAdministratorLogOn){

LogOn logOn = new LogOn();

logOn.setVisible(true);

}

if(e.getSource() == jbNewUserRegister){

RegistrationForm r = new RegistrationForm();

r.setVisible(true);

}

if(e.getSource() == jbExistingUserLogOn){

LogOn logOn = new LogOn();

logOn.setVisible(true);

}

if(e.getSource() == jbAdministratorExitTheSystem){

System.exit(0);

}

if(e.getSource() == jbNewUserExitTheSystem){

System.exit(0);

}

if(e.getSource() == jbExistingUserExitTheSystem){

System.exit(0);

}

}

public static void main(String[] args){

new HomeEntertainment();

}

}

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class LogOn extends JFrame{

JPanel pnlBody, pnlFooter;

JLabel unLabel = new JLabel("Username: ");

JLabel pwLabel = new JLabel("Password: ");

JTextField jtfUN = new JTextField(20);

JPasswordField jtfPW = new JPasswordField(20);

JButton jbOK = new JButton("OK");

JButton jbCancel = new JButton("Cancel");

Container contentpane;

public LogOn(){

super("Welcome to Home Entertainment");

contentpane = getContentPane();

contentpane.setLayout(new BorderLayout());

pnlBody = new JPanel();

pnlFooter = new JPanel();

pnlBody.add(unLabel);

pnlBody.add(jtfUN);

pnlBody.add(pwLabel);

pnlBody.add(jtfPW);

pnlFooter.add(jbOK);

pnlFooter.add(jbCancel);

contentpane.add(pnlBody,BorderLayout.NORTH);

contentpane.add(pnlFooter,BorderLayout.CENTER);

pack();

setVisible(true);

jbOK.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

String username;

String password;

String[] userUsernameArray = {"Ann Smyth", "John Murphy"};

String[] userPasswordArray = {"1", "2"};

String[] adminUsernameArray = {"Administrator"};

String[] adminPasswordArray = {"0"};

username = jtfUN.getText().trim();

password = new String(jtfPW.getPassword());

if(username.equals(userUsernameArray) && password.equals(userPasswordArray)){

setVisible(false);

UserMainMenu umm = new UserMainMenu();

umm.setVisible(true);

}

else if(username.equals(adminUsernameArray) && password.equals(adminPasswordArray)){

setVisible(false);

AdminMainMenu amm = new AdminMainMenu();

amm.setVisible(true);

}

else{

JOptionPane.showMessageDialog(null, "Error\n\nYou have entered an incorrect username and/or password\nPlease try again", null, JOptionPane.ERROR_MESSAGE);

}

}

});

jbCancel.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

setVisible(false);

}

});

}

}

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class AdminMainMenu extends JFrame{

JPanel pnlBody;

JButton btnOrderSystem = new JButton("Order System");

JButton btnMaintenance = new JButton("Maintenance");

JButton btnAdminLogOff = new JButton("Log Off");

Container contentpane;

public AdminMainMenu(){

super("Main Menu");

contentpane = getContentPane();

contentpane.setLayout(new BorderLayout());

pnlBody = new JPanel();

pnlBody.add(btnOrderSystem);

pnlBody.add(btnMaintenance);

pnlBody.add(btnAdminLogOff);

contentpane.add(pnlBody,BorderLayout.CENTER);

pack();

setVisible(true);

btnOrderSystem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

setVisible(false);

//OrderSystem os = new OrderSystem();

//os.setVisible(true);

}

});

btnMaintenance.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

setVisible(false);

//Maintenance m = new Maintenance();

//m.setVisible(true);

}

});

btnAdminLogOff.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

int result;

result = JOptionPane.showConfirmDialog(null, "Are you sure you want to log off?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

if(result == JOptionPane.YES_OPTION){

setVisible(false);

}

}

});

}

}

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class UserMainMenu extends JFrame{

JPanel pnlBody;

JButton btnUserProductMenu = new JButton("Product Menu");

JButton btnUserMemberMenu = new JButton("Member Menu");

JButton btnUserRentalMenu = new JButton("Rental Menu");

JButton btnUserLogOff = new JButton("Log Off");

Container contentpane;

public UserMainMenu(){

super("Main Menu");

contentpane = getContentPane();

contentpane.setLayout(new BorderLayout());

pnlBody = new JPanel();

pnlBody.add(btnUserProductMenu);

pnlBody.add(btnUserMemberMenu);

pnlBody.add(btnUserRentalMenu);

pnlBody.add(btnUserLogOff);

contentpane.add(pnlBody,BorderLayout.CENTER);

pack();

setVisible(true);

btnUserProductMenu.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

//UserProductMenu upm = new UserProductMenu();

//upm.setVisible(true);

}

});

btnUserMemberMenu.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

//UserMemberMenu umm = new UserMemberMenu();

//umm.setVisible(true);

}

});

btnUserRentalMenu.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

//UserRentalMenu urm = new UserRentalMenu();

//urm.setVisible(true);

}

});

btnUserLogOff.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

int result;

result = JOptionPane.showConfirmDialog(null, "Are you sure you want to log off?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

if(result == JOptionPane.YES_OPTION){

setVisible(false);

}

}

});

}

}

james-mcfaddena at 2007-7-29 12:03:08 > top of Java-index,Desktop,Core GUI APIs...
# 3

> hello mbmerrill.

> there is only one main() method (in the

> HomeEntertainment class) used in my video library

> project (slightly modified LogOn class is shown below

> along with the other 3 classes).

I understand that. I'm saying that in your shorter (and thus preferable) version of the code that you posted at the beginning of this thread, you could have easily included a main() method that kicked off the example.

The first example was preferable because it showed the problem, and was shorter and less complex; it didn't focus on the business problem you're trying to solve, but instead highlighted the Java problem you were having.

> the slight

> modification that i made to the LogOn class was that

> i put in an if, an else-if and an if instead of 2

> if's and 2 else's. when i try to log on to my system

> this time, i only get one problem thrown my way. when

> i enter my login details and press OK, i get one

> error message instead of a menu. how can i get rid of

> this problem?

Your conditionals are comparing String to String[].

mbmerrilla at 2007-7-29 12:03:08 > top of Java-index,Desktop,Core GUI APIs...