Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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 Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "linking to main " Watch "linking to main " New topic

linking to main

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.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)) {
"Welcome to MegaBook Order Processing.");
} else {
"Invalid password. Pls try again.",
"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.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0); }
//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="" target="_blank" rel="nofollow"></a>
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

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 - Experts at Building Test Data
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
Priya Mahale

Joined: Sep 30, 2002
Posts: 4
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.");
MainMenu main = new MainMenu();
Hope this helps u
Ranch Hand

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

Joined: Sep 30, 2002
Posts: 4
I agree. Here's the link:
subject: linking to main
jQuery in Action, 3rd edition