Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-12-2012, 06:48 PM   PM User | #1
trantommyd
New Coder

 
Join Date: Mar 2012
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
trantommyd is an unknown quantity at this point
JPasswordField Problem

I'm trying to use JPasswordField, but I'm having a problem doing so correctly.

Code:
String correctPW = "password"; 
			pw.getText()
			if(pw.getText() == correctPW)
			{
				System.out.println("Hello, " + textName.getText() + " from " + jcbo.getSelectedItem() + ", that password was correct.");
			}
			else
			{
				System.out.println("Hello, " + textName.getText() + " from " + jcbo.getSelectedItem() + ", that password was incorrect.");
			}
I've tried several different ways to compare the text entered into the pw JTextField with the correct password (which is "password"), but every way I've tried it so far, it tells me that I've entered the incorrect password everytime. Does is have something to do with the way getText() returns the data? Like, do I need to parse that data or something before comparing with a the String variable correctPassword?
trantommyd is offline   Reply With Quote
Old 06-12-2012, 07:49 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You cannot compare strings with equality. Use .equals() method of the String object.
Fou-Lu is offline   Reply With Quote
Old 06-12-2012, 09:39 PM   PM User | #3
trantommyd
New Coder

 
Join Date: Mar 2012
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
trantommyd is an unknown quantity at this point
Ah I knew it was something small like that. Thanks for your help.
trantommyd is offline   Reply With Quote
Old 06-14-2012, 10:35 PM   PM User | #4
trantommyd
New Coder

 
Join Date: Mar 2012
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
trantommyd is an unknown quantity at this point
Note: asgt1.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

What does this mean? I've never seen it before and still lets me run the program.

Here's my entire code if you need to look thru it.
Code:
import java.util.Scanner;
import java.io.*;
import javax.swing.JOptionPane;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class asgt1 extends JFrame
{
	//create all the components
	private JButton bEnter = new JButton("Enter");
	private JTextField textName = new JTextField(15);
	private JRadioButton rbBoy = new JRadioButton("Boy");
	private JRadioButton rbGirl = new JRadioButton("Girl");
	private JCheckBox cbAge = new JCheckBox("Over 18");
	private String[] states = {"Alabama", "Kentucky", "Louisiana", "Texas", "Wisconsin"};
	private JComboBox jcbo = new JComboBox(states);
	private JPasswordField pw = new JPasswordField(15);
	private Date date = new Date();
	private ImageIcon icon = new ImageIcon("Users/TommyTran/MyDocuments/School/Programming/CS2/us.gif");
	
	//a static variable that will keep count of the number of objects created and a static method that will
	// return that amount when called.
	static int numberOfObjects = 0;
	static int getNumberOfObjects()
	{
		return numberOfObjects;
	}
	
	public asgt1()
	{
		//add components to the panel for displaying	
		JPanel p1 = new JPanel(new GridLayout(6, 2, 10, 10));
		p1.add(new JLabel(date.toString()));
		p1.add(new JLabel());
		p1.add(new JLabel("Name"));
		p1.add(textName);
		p1.add(rbBoy);
		p1.add(rbGirl);
		p1.add(cbAge);
		p1.add(jcbo);
		p1.add(new JLabel("Password: "));
		p1.add(pw);
		p1.add(new JLabel(icon));
		p1.add(bEnter);

		//create an action listener for the enter button. it should check the pw.
		bEnter.addActionListener(new EnterListener());
		
		//make the radio button group
		ButtonGroup rbg = new ButtonGroup();
		rbg.add(rbBoy);
		rbg.add(rbGirl);

		//set layout and add the panel to the frame
		setLayout(new BorderLayout(2,2));
		add(p1, BorderLayout.NORTH);
	}
	
	//this is the superclass to square
	public abstract class GeometricObject
	{
		private boolean filled;
		private String color = "black";
		private java.util.Date dateCreated;
		
		protected GeometricObject()
		{
			dateCreated = new java.util.Date();
			numberOfObjects++;
		}
		
		protected GeometricObject(String color, boolean filled)
		{
			dateCreated = new java.util.Date();
			this.color = color;
			this.filled = filled;
		}
	}
	
	//this is a subclass of GeometricObject
	public class square extends GeometricObject
	{
		private double side;
		
		//a no-arg constructor
		public square()
		{
		numberOfObjects++;
		}
		
		//a constructor that specifies the side and the color and filled using the keyword super
		public square(double side, String color, boolean filled)
		{
		super(color, filled);
		this.side = side;
		numberOfObjects++;
		}
	}
	
	public static void main(String[] args)
	{
		//make the GUI frame 
		asgt1 frame = new asgt1();
		frame.setTitle("Info Sheet");
		frame.setSize(400,400);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	//This is the code that checks for the correct password
	class EnterListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			String correctPW = "password"; 
			String attempt = pw.getText();
			if(correctPW.equals(attempt))
			{
				JOptionPane.showMessageDialog(null, "Hello, " + textName.getText() + " from " + jcbo.getSelectedItem() + ", that password was correct.");
			}
			else
			{
				JOptionPane.showMessageDialog(null, "Hello, " + textName.getText() + " from " + jcbo.getSelectedItem() + ", that password was incorrect.");
			}
		}
	}
}

Last edited by trantommyd; 06-14-2012 at 11:46 PM..
trantommyd is offline   Reply With Quote
Old 06-15-2012, 12:16 AM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Deprecated means that the method has been replaced with one by a different signature, and whilst currently supported it will disappear in a future version of java. Its an advance warning to choose a new handling over an old one to keep the current code as up to date as possible.
This one is caused by getText on the pw object (near the bottom of the code). JPasswordField has deprecated the getText method in favour of getPassword() which returns a char[] instead of a String. You can either ignore it, or you can make use of the char[] instead.
Fou-Lu is offline   Reply With Quote
Old 06-15-2012, 05:17 PM   PM User | #6
trantommyd
New Coder

 
Join Date: Mar 2012
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
trantommyd is an unknown quantity at this point
EDIT. I figured it out. Thanks.

I'm getting the errors Cannot Find Class FigurePanel and Cannot Find Variable FigurePanel?
I don't understand why this error is happening. As far as I know, I've included all of the code that should allow me to draw and add shapes to the panel p2.

I've highlighted the parts of the code that I think are pertinent to my question, but feel free to point out any other errors in my code that you spot.

Code:
import java.util.Scanner;
import java.io.*;
import javax.swing.JOptionPane;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class asgt1 extends JFrame
{
	//create all the components
	private JButton bEnter = new JButton("Enter");
	private JTextField textName = new JTextField(15);
	private JRadioButton rbBoy = new JRadioButton("Boy");
	private JRadioButton rbGirl = new JRadioButton("Girl");
	private JCheckBox cbAge = new JCheckBox("Over 18");
	private String[] states = {"Alabama", "Kentucky", "Louisiana", "Texas", "Wisconsin"};
	private JComboBox jcbo = new JComboBox(states);
	private JPasswordField pw = new JPasswordField(15);
	private Date date = new Date();
	private ImageIcon icon = new ImageIcon("Users/TommyTran/MyDocuments/School/Programming/CS2/us.gif");
	static int numberOfContacts = 0;
		
	public asgt1()
	{
		//add components to the panel for displaying	
		JPanel p1 = new JPanel(new GridLayout(6, 2, 10, 10));
		p1.add(new JLabel(date.toString()));
		p1.add(new JLabel());
		p1.add(new JLabel("Name"));
		p1.add(textName);
		p1.add(rbBoy);
		p1.add(rbGirl);
		p1.add(cbAge);
		p1.add(jcbo);
		p1.add(new JLabel("Password: "));
		p1.add(pw);
		p1.add(new JLabel(icon));
		p1.add(bEnter);
		

		//make and fill another panel with a shape, filled shape, a line, and a string 
		JPanel p2 = new JPanel(new GridLayout(1, 1, 5, 5));
		p2.add(new FigurePanel(FigurePanel.RECTANGLE));
		p2.add(new FigurePanel(FigurePanel.RECTANGLE, true));
		p2.add(new FigurePanel(FigurePanel.LINE));

		

		//create an action listener for the enter button. it should check the pw.
		bEnter.addActionListener(new EnterListener());
		
		//make the radio button group
		ButtonGroup rbg = new ButtonGroup();
		rbg.add(rbBoy);
		rbg.add(rbGirl);

		//set layout and add the panels to the frame
		setLayout(new BorderLayout(2,2));
		add(p1, BorderLayout.NORTH);
		add(p2, BorderLayout.SOUTH);
	}
	public static void main(String[] args)
	{
		//make the GUI frame 
		asgt1 frame = new asgt1();
		frame.setTitle("Info Sheet");
		frame.setSize(400,400);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	//This is the code that checks for the correct password
	class EnterListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			String correctPW = "password"; 
			String attempt = pw.getText();
			if(correctPW.equals(attempt))
			{
				JOptionPane.showMessageDialog(null, "Hello, " + textName.getText() + " from " + jcbo.getSelectedItem() + ", that password was correct.");
				int ask = JOptionPane.showConfirmDialog(null, "Would you like to make a contact?", "Choose one", JOptionPane.YES_NO_OPTION);
				
				if(ask == JOptionPane.YES_OPTION)
				{
					Human timmy = new Contact(true, "Timmy");
					JOptionPane.showMessageDialog(null, "You have created Timmy.");
				}
			}
			else
			{
				JOptionPane.showMessageDialog(null, "Hello, " + textName.getText() + " from " + jcbo.getSelectedItem() + ", that password was incorrect.");
			}
		}
	}
	
	//a superclass to contact
	public class Human
	{
		//the String variable name is protected, so only the subclass can access this variable
		protected String name;
	
		public Human()
		{
		}
	
		public Human(String name)
		{
			this.name = name;
		}
		
		public void setName(String name)
		{
			this.name = name;
		}
	}
	
	//a subclass to Human. this class is final, meaning it cannot be extended from.
	public final class Contact extends Human
	{
		private boolean overEighteen;
		
		public Contact()
		{
		numberOfContacts++;
		}
		
		public Contact(boolean overEighteen, String name)
		{
			//using the keyword super to call the superclass constructor
			super(name);
			this.overEighteen = overEighteen;
			numberOfContacts++;
		}
		
		public String getName()
		{
			return name;
		}
		//example of overriding a method from the superclass
		public void setName(String name)
		{
			name = "Wrong";
		}
		
		//method of overloading a method from the superclass
		public void setName(int name)
		{
			name = name;
		}
		public void printInfo(boolean overEighteen)
		{
			if(overEighteen)
			{
				JOptionPane.showMessageDialog(null, "This person is over 18.");
			}
			else
			{
				JOptionPane.showMessageDialog(null, "This person is not over 18.");
			}
		}
	}
	
	//a static method that returns the number of contacts that have been created
	static int getNumberOfContacts()
	{
		return numberOfContacts;
	}
}

Last edited by trantommyd; 06-15-2012 at 08:01 PM..
trantommyd is offline   Reply With Quote
Old 06-15-2012, 10:54 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
FigurePanel isn't a part of core api, so it has to be a custom class. Is it a part of the same package (default) as asgt1 is? If not, it has to be imported first.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:58 AM.


Advertisement
Log in to turn off these ads.