alex
java codes
2 Answer(s)      6 years and 7 months ago
Posted in : Java Beginners
Advertisement
View Answers

October 3, 2008 at 5:11 PM


Hi friend,

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


public class JPasswordDemo extends JPanel implements ActionListener{
private String OK = "ok";
private String HELP = "help";
private JFrame frame;
private JPasswordField passwordField;

public JPasswordDemo(JFrame f) {
frame = f;
//Create everything.
passwordField = new JPasswordField(10);
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(frame, "Success! You typed the right password.");
}
else {
JOptionPane.showMessageDialog(frame, "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(frame,
"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.");
}
}


October 3, 2008 at 5:13 PM


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

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() {
JFrame frame = new JFrame("password filed example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPasswordDemo newContentPane = new JPasswordDemo(frame);
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
newContentPane.resetFocus();
}
});
frame.setSize(300,150);
frame.setVisible(true);
}

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

Visit for more information.

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

Thanks

Amardeep









Related Tutorials/Questions & Answers:
java codes
java codes  hi .. i need a login code in java with restriction that with 3 error attempts then the program will terminate .. thanks
java codes
java codes  why is every application allowed to use classes System and String without first importing the item
Advertisements
JAVA CODES - Java Beginners
JAVA CODES  Layout manager.java code
java codes - Java Beginners
java codes  how to use the jpasswordfield? what is the code?  ... of The Java Tutorial."); } }   private boolean isPasswordCorrect... information. http://www.roseindia.net/java/example/java/swing/ Thanks
Java Codes - Java Beginners
Java Codes  Please, can someone solve this? A prompt asks the executive secretary to input the time for the first interview, and then a loop continues to prompt for input of subsequent interview, and then a loop continues
Java codes - Java Beginners
Java codes  Ex#1. Write a java programe that declares 25 characters... on the monitor. Ex#3. Write a java program that declares an array containing... numbers in a array. Ex#4. Write a java program that declares an array
java sorting codes - Java Beginners
java sorting codes  I want javasorting codes. please be kind enogh and send me the codes emmediately///  Hi Friend, Please visit the following link: http://www.roseindia.net/java/beginners/arrayexamples/ Here
java image converting to byte codes - Java Beginners
java image converting to byte codes  i want to convert an image to human unreadable format which is uploaded by client to server.How can i do
show codes of this
show codes of this  search for the number of occurrence of "and" and "the" in the following sentence by writing a codes. the student like the best teacher at the end of the lessons and others
Eclipse (Problem with combining 2 codes together) - Java Beginners
Eclipse (Problem with combining 2 codes together)  hi, I have 2 pieces of code and I don't know how to combine together. Hope someone can help me combine it together. And I also want to add in if (inputs.equals
Eclipse (Problem with combining 2 codes together)(Cont) - Java Beginners
Eclipse (Problem with combining 2 codes together)(Cont)  Hi, I have a problem with this code (below), I enter the username and password correctly then I want to input an integer either 1 or 2 but if I enter wrongly, e.g. 3
codes for banking databases
codes for banking databases  sir plz give me codes for creating a banking database using java
codes for banking databases
codes for banking databases  sir PLZ give me codes for banking database using java
HTML codes
HTML codes  Hi, I am trying to find HTML codes to learn HTML. Can any one html me? Thanks   Hi, Please check HTML examples and HTML5 Tutorials page. Thanks
codes for banking databases
codes for banking databases  sir plz give me codes for creating a banking database using netbeans as back end and java as front end
Java Example Codes and Tutorials
Java Tutorials - Java Example Codes and Tutorials Java is great programming... the name to Java and modified the language to take advantage of the burgeoning World Wide Web. Java is an object-oriented language, and this is very similar to C
Request for codes - JSP-Servlet
Request for codes  Sir , I am an engineering student i am interested in learning JAVA also i need some example code for creating Registration form codes for creating web based application using JSP sir plz send me which
codes for displaying in calendar
codes for displaying in calendar  can i get jsp codes for displaying comments, when the pointer is placed over the particular date in calendar
files uploding and downloading codes
files uploding and downloading codes  any one know JSP codes for upload files,download files and delete files from a created virtual memory
Needed jsp codes
Needed jsp codes   jsp code for employee payroll.producing a payslip of every employees monthly calculating the gross pay of a particular employee considering the income tax and producing a report which can be printed out
jsp and ejb codes - EJB
jsp and ejb codes  give me the codes to write small shopping cart project.I only want to acess from server.i want to show the client the search place... i all want to develop..please send me the codes in using jsp and ejb.because iI
Can you correct this codes?
Can you correct this codes?  Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 at searchcon.(searchcon.java:39) at search.initComponents(search.java:41) at search.(search.java:7) at payroll
No complete codes for Simple Form Controlle Example
No complete codes for Simple Form Controlle Example  No complete codes
HTML color codes
HTML Color Codes In this tutorial we will show you how to get the different color codes for designing your HTML pages. You will be able to find the  hexadecimal color code by color name. We have also provided the tool to create
HTML Color Codes
:5px 0px 5px 5px; } HTML Web color codes In this page we have created a list of most used HTML Web color codes. We have shown the color... color codes: Name/String Code Name Code
what are the codes for uploading and downloading files in and from database?
what are the codes for uploading and downloading files in and from database?  We are designing a student's website where they can get and post study materials. We actually need the code to upload and download files in and from
what is wrong with my JSP codes for updating a form?
what is wrong with my JSP codes for updating a form?  Hi experts, I have tried to emulate a code from a textbook but it doesn't work in my program.... Name - String. Here's snippet of my codes:- <% Connection conn = null
Lambda Expression in Java 8
Lambda Expression in Java 8: Learn the power of Lambda Expression In this section we will learn about the Lambda Expression which is introduced in the Java... in the Java 8. This is the biggest upgrade introduced in the Java so far
can u help me to get a calendar with color codes on days ???
can u help me to get a calendar with color codes on days ???  hi roseindia, Can you help me to get the code, I need a calendar with color codes.i,e Sunday,Saturday dates display in blue color and my calendar should takes
Problem in Spring 3 MVC and Hibernate 3 Example tutorial source codes
Problem in Spring 3 MVC and Hibernate 3 Example tutorial source codes  I referred your tutorial "Spring 3 MVC and Hibernate 3 Example" and downloaded your source codes. I created a build.xml and built it using ant. And copied
Iterator in java, Iterator Java Examples
The Iterator is an java interface, it can be used to iterate the java collection objects. In this Java iterator tutorial you will learn how to define of Java Iterator interface