This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

linking to main

 
sae0203
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;
}
}
 
Gregg Bolinger
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.
 
Priya Mahale
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic