aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes linking to main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "linking to main " Watch "linking to main " New topic
Author

linking to main

sae0203
Ranch Hand

Joined: Sep 19, 2002
Posts: 34
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;
}
}


SAE <br /> <br /><a href="http://www.findsingapore.net/forum" target="_blank" rel="nofollow">http://www.findsingapore.net/forum</a>
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

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?


GenRocket - A Test Data Generation Platform
sae0203
Ranch Hand

Joined: Sep 19, 2002
Posts: 34
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.
Priya Mahale
Greenhorn

Joined: Sep 30, 2002
Posts: 4
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

Joined: Sep 19, 2002
Posts: 34
thanks Mahale... your help is greatly
apprenticed
Priya Mahale
Greenhorn

Joined: Sep 30, 2002
Posts: 4
 
 
subject: linking to main
 
Similar Threads
Help!!!!!! GridBag Layout constraints
Switching between two forms
mouseClicked event for the "Cancel button"
Applet Login via database
Password verification program