Results 1 to 4 of 4
  1. #1
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

    Default Problem with JTextField

    I know swing decently well, but im having a weird problem with a JTextField.
    Here's the code:

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Frame
    {
    	public static void main(String[] args)
    	{
    		new Frame();
    	}
    	
    	JFrame checkerFrame;
    	JButton logon;
    	JTextField passwordField;
    	
    	String password = "tree";
    	
    	public Frame()
    	{
    		checkerFrame = new JFrame("Password");
    		checkerFrame.setSize(200,200);
    		checkerFrame.setResizable(false);
    		checkerFrame.setLayout(null);
    		checkerFrame.setLocationRelativeTo(null);
    		checkerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		checkerFrame.setVisible(true);
    		
    		logon = new JButton("Enter");
    		logon.setBounds(65,140,75,25);
    		
    		checkerFrame.add(logon);
    		
    		passwordField = new JTextField();
    		passwordField.setBounds(25,25,150,25);
    		
    		checkerFrame.add(passwordField);
    		
    		logon.addActionListener(new Handler());
    	}
    	
    	private class Handler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if(e.getSource() == logon)
    			{
    				if(passwordField.getText() == password)
    				{
    					checkerFrame.dispose();
    				}
    				else
    				{
    					JOptionPane.showMessageDialog(checkerFrame,"Incorrect Password. Please Try Again.");
    				}
    			}
    		}
    	}
    }
    It should dispose of the frame if the text in passwordField is the same as password (in this case "tree"), but even if it's the same it runs the else statement. Do i do something wrong? I really don't know what's wrong.

  2. #2
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

    Default Re: Problem with JTextField

    Oh, wow. Nevermind, stupid mistake on my part. I got it.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with JTextField

    Yeah, you've got problems in that code
    • Using == to compare Strings
    • Using Strings for passwords period
    • Using null layout, absolute positioning and setSize(...) rather than preferredSizes and layout managers
    • Giving your class the same name as a core Java class, one similar to what you are using.
    • ...


    You'll want to read the Java tutorials and especially the Swing tutorials to fix these things, especially the layouts.

  4. #4
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

Similar Threads

  1. problem with JTextfield..
    By Shahnawaz in forum New To Java
    Replies: 2
    Last Post: 10-04-2011, 09:16 AM
  2. Replies: 0
    Last Post: 08-30-2011, 10:18 AM
  3. Problem with JTextField.. please help!!
    By Asvin in forum New To Java
    Replies: 21
    Last Post: 12-18-2010, 04:49 PM
  4. Problem's with JTextField
    By DC% in forum AWT / Swing
    Replies: 4
    Last Post: 03-10-2009, 06:33 PM
  5. Got problem with JtextField
    By hungleon88 in forum AWT / Swing
    Replies: 4
    Last Post: 12-06-2008, 04:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •