java codes

java codes

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 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 CODES - Java Beginners
JAVA CODES  Layout manager.java 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. http://www.roseindia.net/java/example/java/swing/ 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 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
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 banking database using java
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 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 (adsbygoogle = window.adsbygoogle || []).push({});   Hi, Please check HTML examples and HTML5 Tutorials page
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
ModuleNotFoundError: No module named 'codes'
ModuleNotFoundError: No module named 'codes'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'codes' How to remove the ModuleNotFoundError: No module named 'codes'
ModuleNotFoundError: No module named 'codes'
ModuleNotFoundError: No module named 'codes'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'codes' How to remove the ModuleNotFoundError: No module named 'codes'
ModuleNotFoundError: No module named 'codes'
ModuleNotFoundError: No module named 'codes'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'codes' How to remove the ModuleNotFoundError: No module named 'codes'
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
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
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
ModuleNotFoundError: No module named 'mobile-codes'
ModuleNotFoundError: No module named 'mobile-codes'  Hi, My Python... 'mobile-codes' How to remove the ModuleNotFoundError: No module named 'mobile-codes' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'funny-codes'
ModuleNotFoundError: No module named 'funny-codes'  Hi, My Python... 'funny-codes' How to remove the ModuleNotFoundError: No module named 'funny-codes' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'coupon-codes'
ModuleNotFoundError: No module named 'coupon-codes'  Hi, My Python... 'coupon-codes' How to remove the ModuleNotFoundError: No module named 'coupon-codes' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'exit-codes'
ModuleNotFoundError: No module named 'exit-codes'  Hi, My Python... 'exit-codes' How to remove the ModuleNotFoundError: No module named 'exit-codes' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'iata_codes'
ModuleNotFoundError: No module named 'iata_codes'  Hi, My Python... 'iata_codes' How to remove the ModuleNotFoundError: No module named 'iata_codes' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'prefix-codes'
ModuleNotFoundError: No module named 'prefix-codes'  Hi, My Python... 'prefix-codes' How to remove the ModuleNotFoundError: No module named 'prefix-codes' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'rkm-codes'
ModuleNotFoundError: No module named 'rkm-codes'  Hi, My Python... 'rkm-codes' How to remove the ModuleNotFoundError: No module named 'rkm-codes' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'coupon-codes'
ModuleNotFoundError: No module named 'coupon-codes'  Hi, My Python... 'coupon-codes' How to remove the ModuleNotFoundError: No module named 'coupon-codes' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'exit-codes'
ModuleNotFoundError: No module named 'exit-codes'  Hi, My Python... 'exit-codes' How to remove the ModuleNotFoundError: No module named 'exit-codes' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'iata_codes'
ModuleNotFoundError: No module named 'iata_codes'  Hi, My Python... 'iata_codes' How to remove the ModuleNotFoundError: No module named 'iata_codes' error? Thanks   Hi, In your python environment you
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
ModuleNotFoundError: No module named 'iso-language-codes'
ModuleNotFoundError: No module named 'iso-language-codes'  Hi, My... named 'iso-language-codes' How to remove the ModuleNotFoundError: No module named 'iso-language-codes' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'py-polar-codes'
ModuleNotFoundError: No module named 'py-polar-codes'  Hi, My... named 'py-polar-codes' How to remove the ModuleNotFoundError: No module named 'py-polar-codes' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'boois_return_codes'
ModuleNotFoundError: No module named 'boois_return_codes'  Hi, My... named 'boois_return_codes' How to remove the ModuleNotFoundError: No module named 'boois_return_codes' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'cloudfront-edge-codes'
ModuleNotFoundError: No module named 'cloudfront-edge-codes'  Hi...: No module named 'cloudfront-edge-codes' How to remove the ModuleNotFoundError: No module named 'cloudfront-edge-codes' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-countries-with-calling-codes'
ModuleNotFoundError: No module named 'django-countries-with-calling-codes' ...: ModuleNotFoundError: No module named 'django-countries-with-calling-codes' How to remove...-codes' error? Thanks   Hi, In your python environment you have
ModuleNotFoundError: No module named 'django-script-codes'
ModuleNotFoundError: No module named 'django-script-codes'  Hi, My... named 'django-script-codes' How to remove the ModuleNotFoundError: No module named 'django-script-codes' error? Thanks   Hi
ModuleNotFoundError: No module named 'iso-language-codes'
ModuleNotFoundError: No module named 'iso-language-codes'  Hi, My... named 'iso-language-codes' How to remove the ModuleNotFoundError: No module named 'iso-language-codes' error? Thanks   Hi, In your
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
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

Ads