• Post Reply Bookmark Topic Watch Topic
  • New Topic

linking to main  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i link the ok button after the validate of the password to the main menu?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Password {
public static void main(String[] argv) {
final JFrame f = new JFrame("Password");

//set label to give instruction to user
JLabel label = new JLabel("Enter the password: ");
JPasswordField passwordField = new JPasswordField(10);
passwordField.setEchoChar('*');

passwordField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPasswordField input = (JPasswordField)e.getSource();

//an array to store user inputs
char[] password = input.getPassword();

//display corresponding messages upon password input
if (isPasswordCorrect(password)) {
JOptionPane.showMessageDialog(f,
"Welcome to MegaBook Order Processing.");
} else {
JOptionPane.showMessageDialog(f,
"Invalid password. Pls try again.",
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
});
//set panel properties
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
contentPane.add(label, BorderLayout.NORTH);
contentPane.add(passwordField, BorderLayout.CENTER);
f.setContentPane(contentPane);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0); }
});
f.pack();
f.setVisible(true);
}
//check validity of password
private static boolean isPasswordCorrect(char[] input) {
char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' };
if (input.length != correctPassword.length)
return false;
for (int i = 0; i < input.length; i++)
if (input[i] != correctPassword[i])
return false;
return true;
}
}
 
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What? What "main menu" are you referring to? Are you talking about a JMenuBar? Are you talking about a JDialog with JButtons that act as your "main menu"? Could you please clarify your question?
 
sae0203
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's a JDialogBox with button that can be link to the main page... not main menu.... sorry {~.~}
the system is like this, when we first run it, a input textfield will appear for user to key in the password, it will do a checking.... if the password is correct, a message dialog will appear and with it there is a "OK" button. we need the button to bring us to the main page upon clicking it. so how can be link it?
let's say the main menu is named MainMenu.java.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If u want to show a different frame after the user clicks the ok button of the msg dialog then u can do as follows:
if (isPasswordCorrect(password))
{
JOptionPane.showMessageDialog(f,"Welcome to MegaBook Order Processing.");
f.hide();
MainMenu main = new MainMenu();
main.setSize(300,200);
main.setVisible(true);
}
Hope this helps u
 
sae0203
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Mahale... your help is greatly
apprenticed
 
Priya Mahale
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!