java codes



Share on Google+Share on Google+

java codes
2 Answer(s)      9 years ago
Posted in : Java Beginners
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);
JLabel label = new JLabel("Enter the password: ");
JComponent buttonPane = createButtonPanel();
//Lay out everything.
JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));


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


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",
//Zero out the possible password, for security.
Arrays.fill(input, '0');
} else { //The user has asked for help.
"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.

return isCorrect;

protected void resetFocus() {
private static void createAndShowGUI() {
JFrame frame = new JFrame("password filed example");
final JPasswordDemo newContentPane = new JPasswordDemo(frame);
newContentPane.setOpaque(true); //content panes must be opaque
frame.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {

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

Visit for more information.




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
JAVA CODES - Java Beginners
JAVA CODES  Layout code
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  how to use the jpasswordfield? what is the code?  ... of The Java Tutorial."); } }   private boolean isPasswordCorrect... information. Thanks
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: Here
Java Example Codes and Tutorials in 2017
Java Example Codes and Tutorials in 2017  Hi, Which is the best tutorials to learn Java programming language in 2014? Thanks   HI, Check tutorial at Java tutorials. All Java tutorials at Java Tutorials and examples
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
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
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
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
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
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
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
ANSI Color Codes with Python
ANSI Color Codes with Python   Use ANSI codes to play with colors and styles. Create a program that will spit out a user inputted string in a color, style, or background color that they can choose. To do this, you'll need
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.( at search.initComponents( at search.( 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


Advertisement null