This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

linking to main

 
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
Mac OS X IntelliJ IDE Chrome
  • 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
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic