Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT Help Required
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Kshirod
Help Required
2 Answer(s)      6 years and 2 months ago
Posted in : Swing AWT



Advertisement
View Answers

August 26, 2008 at 7:30 PM


Hi friend,

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;


public class LoginPassword extends JPanel implements ActionListener {
private static String OK = "ok";
private static String HELP = "help";

private JFrame controllingFrame; //needed for dialogs
private JPasswordField passwordField;

public LoginPassword(JFrame f) {
//Use the default FlowLayout.
controllingFrame = f;

//Create everything.
passwordField = new JPasswordField(15);
passwordField.setActionCommand(OK);
passwordField.addActionListener(this);

JLabel label = new JLabel("Enter the password: ");
label.setLabelFor(passwordField);

JComponent buttonPane = createButtonPanel();

//Lay out everything.
JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
textPane.add(label);
textPane.add(passwordField);
add(textPane);
add(buttonPane);
}

protected JComponent createButtonPanel() {
JPanel p = new JPanel(new GridLayout(0,1));
JButton okButton = new JButton("OK");
JButton helpButton = new JButton("Help");

okButton.setActionCommand(OK);
helpButton.setActionCommand(HELP);
okButton.addActionListener(this);
helpButton.addActionListener(this);

p.add(okButton);
p.add(helpButton);

return p;
}

public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();

if (OK.equals(cmd)) { //Process the password.
char[] input = passwordField.getPassword();
if (isPasswordCorrect(input)) {
JOptionPane.showMessageDialog(controllingFrame,
"Success! You typed the right password.");
} else {
JOptionPane.showMessageDialog(controllingFrame,"Invalid password. Try again.","Error Message",
JOptionPane.ERROR_MESSAGE);
}

//Zero out the possible password, for security.
Arrays.fill(input, '0');

passwordField.selectAll();
resetFocus();
} else { //The user has asked for help.
JOptionPane.showMessageDialog(controllingFrame,
"You can get the password by searching this example's\n"
+ "source code for the string \"correctPassword\".\n"
+ "Or look at the section How to Use Password Fields in\n"
+ "the components section of The Java Tutorial.");
}
}


August 26, 2008 at 7:33 PM


private static boolean isPasswordCorrect(char[] input) {
boolean isCorrect = true;
char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' };

if (input.length != correctPassword.length) {
isCorrect = false;
} else {
isCorrect = Arrays.equals (input, correctPassword);
}

//Zero out the password.
Arrays.fill(correctPassword,'0');

return isCorrect;
}

protected void resetFocus() {
passwordField.requestFocusInWindow();
}

private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("password example in java");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final LoginPassword newContentPane = new LoginPassword(frame);
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
newContentPane.resetFocus();
}
});
frame.setSize(500,200);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
-------------------------------

Read for more information.

http://www.roseindia.net/java/example/java/swing/

Thanks.

Amardeep


Related Tutorials/Questions & Answers:
help required in coding
help required in coding  hi..... I want to display output in graphical format . Can u tell me how it can be done? using for project java swings. plzzzzzz helpp
Help Required - Swing AWT
Help Required  How to bring password field in JDialogueBox??? ... static String HELP = "help"; private JFrame controllingFrame; //needed..."); JButton helpButton = new JButton("Help
Advertisements
Editor help required. - Java Server Faces Questions
Editor help required.  I am having problem with editor used to design JSP page in JSF applications. I want to open JSP page with Web Page Editor... are they? Please Help me.... Thanks In advance
Required help about the concept of JSP page reloading
Required help about the concept of JSP page reloading  Hi, We have one application with Websphere portlet Factory generated JSP as front end... set. Your inputs are highly appreciated. It will help us to have better insight
Required help about the concept of JSP page reloading
Required help about the concept of JSP page reloading  Hi, We have one application with Websphere portlet Factory generated JSP as front end... char set. Your inputs are highly appreciated. It will help us to have better
HELP! Design Review & Risk Management topics (in Java projects) required - Development process
HELP! Design Review & Risk Management topics (in Java projects) required  Hi, I have to take 2 one hour sessions on the following topics... topics/examples/case studies for the same? Any help will be much appreciated
code required.!
Java Script phone number entered by person should be in numeric only. code required!  i am using javascript coding on jsp page. i want that the phone number entered by person should be in numeric only..can i get code
code required
page Will you plz help me out by providing me with the code
code required in servlet
code required in servlet  hello... you provided me the answer with javascript.. i want to check which button is clicked in the servlet.. i have only... help
E BOOK REQUIRED
E BOOK REQUIRED  Wicket in Action pdf required Urgently. Thnx
property datasource required - Spring
property datasource required  Hi I am using java springs I am using mysql as backend I am getting one error when i compile the application.property datasource required. This is my dispatcher-servlet.xml
Urgently required SEO
Freelance SEO       Position Vacant: Urgently required SEO Job Description ...; Website: http://www.roseindia.net  Reference ID: Urgently required
steps required to execute a query in JDBC
steps required to execute a query in JDBC  What are the steps required to execute a query in JDBC
Date Format required to change in java
Date Format required to change in java  can we change the format of date like sql as per our requirements???If yes then please give the detail solution
Hibernate required jar files - Hibernate
Hibernate required jar files  Hi, What are the jar files are required for hibernate and Springs application. I want to run complete application springs and hibernate. Where it will be available? How can i use it i.e.
jQuery required validation
jQuery required Validation: jQuery "required" is the method...;/option>. Getting Started with jQuery required : In this example there are the following steps which are used to make jQuery required. This example  Makes
Spring @Required Annotation
Spring @required Annotation In this tutorial you will see about spring @required annotation with an example. The @Required when written on top of setStudent... that org.springframework.beans.factory.BeanInitializationException: Property 'student' is required for bean 'college
Tools required to build Simple Cart
Tools required to build Simple Cart       Shopping cart application is written in Java and so... tools are required--- Java Development Kit (JDK), 1.4 or later. Apache Ant
help
help   how i can send a pitcture on url in java
help
help  pls help me to get the code of a java program a program to perform different shapes in a menu using javaapplet
sample JSP&Servlet application required? - JSP-Servlet
sample JSP&Servlet application required?  hey all iam new... tutorial or a project that integrate both jsp&servlets any help?  Hi Friend... will be helpful for you. Thanks  thanks for your help i need more
how to show required fields indicate as * in struts2
how to show required fields indicate as * in struts2  how to show required fields indicate as * in struts2
help
help  i need help with this code. write a java code for a method named addSevenToKthElement that takes an integer array, and an integer k as its arguments and returns the kth element plus 7. any help would be greatly
HELP
HELP  I need this code to open in a new web browser. but no matter what i do it wont please help?? Directive 055 Storage Requirements for the Upstream Petroleum Industry
Help...
Help...  Write a class that displays your first name vertically down the screen ?? where each letter uses up to 5 rows by 5 columns of a character input by you with a blank line between each letter
Help!
Help!  thanks for ur code, I'd like to ask a question: after press "c" or "C", timer starts and after every delay ms, it'll print "This line is printed only once." or just print once. I'm writting a timer but it seems to perform
Why binding attribute is required in jsf frame work
Why binding attribute is required in jsf frame work  Why binding attribute is required in jsf frame work. And what use of binding attribute
Help
Help  Hi; Can anyone help me to solve this errors. I use J2ME(wirlesee Toolkit 2.5.2. for CLDC Project "SlideImageMIDlet" loaded Project settings saved Building "SlideImageMIDlet" C:\Documents and Settings\???? ?????\j2mewtk
help
to the user) please tell me how could i design that. Please help me asap Thanks a lot
help
based on the percentage of cheating. please help me
help!!!!!!!!!!!!!!!!!!
help!!!!!!!!!!!!!!!!!!  import java.awt.; import java.sql.*; import javax.swing.*; import java.awt.event.* public class NewJFrame extends javax.swing.JFrame { JTextField1
Advertisements
 

 

 

DMCA.com