Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Login Page error

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Login Page error

    I have this code for creating a login page.I want that as soon the username and password are verified, control should be tranfered to another page which is Main.java in this case.But as soon as I verify the username and password Main.java is not executed.Actually nothing happens after verifying the code.Can someone tell me where I am going wrong?


    package pck1;
     
     
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    //import org.apache.log4j.chainsaw.Main;
     
    import java.awt.Color;
    //import pck1.Main.*;
     
     
     
    public class no extends JFrame{
     
     
     
     
     
        private JLabel jLabel1;
        private JLabel jLabel2;
        private JTextField jTextField1;
        private JPasswordField jPasswordField1;
        private JButton jButton1;
        private JPanel contentPane;
     
        String username;
       char[] password;
     
     
       public no()
        {
            super();
            create();
            this.setVisible(true);
        }
     
     
        private void create()
        {
            jLabel1 = new JLabel();
            jLabel2 = new JLabel();
            jTextField1 = new JTextField();
            jPasswordField1 = new JPasswordField();
            jButton1 = new JButton();
            contentPane = (JPanel)this.getContentPane();
     
     
     
     
     
            // jLabel1
             jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
             jLabel1.setText("username:");
     
     
            jLabel1.setForeground(new Color(0, 0, 255));
            jLabel1.setText("username:");
     
            // jLabel2
     
            jLabel2.setHorizontalAlignment(SwingConstants.LEFT);
            jLabel2.setText("password:");
     
            jLabel2.setForeground(new Color(0, 0, 255));
            jLabel2.setText("password:");
     
            // jTextField1
     
            jTextField1.setForeground(new Color(0, 0, 255));
            jTextField1.setSelectedTextColor(new Color(0, 0, 255));
            jTextField1.setToolTipText("Enter your username");
     
            jTextField1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    jTextField1_actionPerformed(e);
                }
     
            });
     
            // jPasswordField1
     
            jPasswordField1.setForeground(new Color(0, 0, 255));
            jPasswordField1.setToolTipText("Enter your password");
            jPasswordField1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
     
                    jPasswordField1_actionPerformed(e);
            }
     
            });
     
            // jButton1
     
           // jButton1.setBackground(new Color(204, 204, 204));
           // jButton1.setForeground(new Color(0, 0, 255));
            jButton1.setText("Login");
            jButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    jButton1_actionPerformed(e);
                }
     
            });
     
            // contentPane
     
            contentPane.setLayout(null);
            contentPane.setBorder(BorderFactory.createEtchedBorder());
            contentPane.setBackground(new Color(204, 204, 204));
            addComponent(contentPane, jLabel1, 5,10,106,18);
            addComponent(contentPane, jLabel2, 5,47,97,18);
            addComponent(contentPane, jTextField1, 110,10,183,22);
       addComponent(contentPane, jPasswordField1, 110,45,183,22);
            addComponent(contentPane, jButton1, 150,75,83,28);
     
            // login
     
            this.setTitle("Login to Enter DWST");
            this.setLocation(new Point(76, 182));
            this.setSize(new Dimension(335, 141));
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setResizable(false);
        }
     
        /** Add Component Without a Layout Manager (Absolute Positioning) */
        private void addComponent(Container container,Component c,int x,int y,int width,int height)
        {
            c.setBounds(x,y,width,height);
            container.add(c);
        }
     
     
        private void jTextField1_actionPerformed(ActionEvent e)
        {
              username=jTextField1.getText();
     
        }
     
        private void jPasswordField1_actionPerformed(ActionEvent e)
    {
        	password=jPasswordField1.getPassword();
     
        }
     
        @SuppressWarnings("deprecation")
    	public void jButton1_actionPerformed(ActionEvent e)
        {
            System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
            String username = new String(jTextField1.getText());
            String password = new String(jPasswordField1.getPassword());
     
            if(username.equals("") || password.equals("")) // If password and username is empty 
                    {
                        jButton1.setEnabled(false);
                    JLabel errorFields = new JLabel("You must enter a username and password to login.");
                    JOptionPane.showMessageDialog(null,errorFields);
                    jTextField1.setText("");
                    jPasswordField1.setText("");
                    jButton1.setEnabled(true);
                    this.setVisible(true);
                    }
                    else
                    {
            JLabel optionLabel = new JLabel("You entered  "+username+"  as your username.Is this correct?");
    int confirm =JOptionPane.showConfirmDialog(null,optionLabel);
     
    switch(confirm){       // Switch > Case
     
    	case JOptionPane.YES_OPTION:  // Attempt to Login user
            jButton1.setEnabled(false); 
            if(username.equals("abhinav") && password.equals("abhinav"))
            {
           Main obj1= new Main();
            obj1.setVisible(true);
            }
            break;
     
        case JOptionPane.NO_OPTION:   
                jButton1.setEnabled(false);
                jTextField1.setText("");
                jPasswordField1.setText("");
                jButton1.setEnabled(true);
                break;
     
        case JOptionPane.CANCEL_OPTION:  
                jButton1.setEnabled(false);
                jTextField1.setText("");
                jPasswordField1.setText("");
                jButton1.setEnabled(true);
                break;
     
            }   // End Switch > Case
     
     
           }
           }
     
    public static void main(String[] args)
    {
    /*JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    try
    {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    }
    catch (Exception ex)
    {
        System.out.println("Failed loading L&F: ");
        System.out.println(ex);
        }*/
        new no();
    }
     
     
     
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Login Page error

    Since I do not have your Main class I replaced it with a simple print statement. When I ran your code the output from the print statement appeared. Thus I can conclude the problem lies in your Main class.

    I have several issues with your code:

    Your indentation is all over the place like a mad womans knitting which makes it a nightmare to read.

    String username = new String(jTextField1.getText());
    Why create a new String? The getText method returns a String so just assign it to the variable.

    Why have you declared instance variables for username and password and then in the actionPerformed method you use local variables with the same names and totally ignore the instance variables?

    Why have actionListeners for the textfields at all?

    Use meaningful names for variables, jLabel1 and jLabel2 etc means nothing to someone trying to read your code.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Login Page error

    Quote Originally Posted by Junky View Post
    Since I do not have your Main class I replaced it with a simple print statement. When I ran your code the output from the print statement appeared. Thus I can conclude the problem lies in your Main class.

    I have several issues with your code:

    Your indentation is all over the place like a mad womans knitting which makes it a nightmare to read.

    String username = new String(jTextField1.getText());
    Why create a new String? The getText method returns a String so just assign it to the variable.

    Why have you declared instance variables for username and password and then in the actionPerformed method you use local variables with the same names and totally ignore the instance variables?

    Why have actionListeners for the textfields at all?

    Use meaningful names for variables, jLabel1 and jLabel2 etc means nothing to someone trying to read your code.

    my Main.java class code is written below.I have changed the username assignment as per you have shown above but still this it is not gowing to the Main.java file

    package pck1;
     
    import javax.swing.table.*;
    import javax.swing.BorderFactory;
    import javax.swing.JDesktopPane;
     
     
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
     
    import javax.swing.JTextField;
     
    import sun.font.FontFamily;
     
    import java.awt.Rectangle;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import java.awt.Point;
    import javax.swing.JButton;
    import javax.swing.JLabel;
     
     
    public class Main extends JDesktopPane {
     
    	private JDesktopPane DesktopPane2 = null;  //  @jve:decl-index=0:visual-constraint="67,10"
    	private JTextField TextField1 = null;
    	private JScrollPane ScrollPane = null;
    	private JTable Table1 = null;
    	private JTextField TextField2 = null;
    	private JButton Button1 = null;
     
     
     
     
    	public Main() {
    		super();
    		initialize();
    		this.setVisible(true);
    	}
     
     
     
     
     
     
     
    	/**
    	 * This method initializes this
    	 * 
    	 */
    	private void initialize() {
            this.setSize(new Dimension(942, 469));
            this.add(getDesktopPane2());
            this.setVisible(true);
     
    	}
     
    	/**
    	 * This method initializes DesktopPane2	
    	 * 	
    	 * @return javax.swing.JDesktopPane	
    	 */
    	private JDesktopPane getDesktopPane2() {
    		if (DesktopPane2 == null) {
    			DesktopPane2 = new JDesktopPane();
    			DesktopPane2.setSize(new Dimension(861, 400));
    			DesktopPane2.setBackground(Color.DARK_GRAY);
    			DesktopPane2.setLocation(new Point(44, 62));
    			DesktopPane2.add(getTextField1(), null);
    			DesktopPane2.add(getScrollPane(), null);
    			DesktopPane2.add(getTextField2(), null);
    			DesktopPane2.add(getButton1(), null);
     
    		}
    		return DesktopPane2;
    	}
     
     
     
    	/**
    	 * This method initializes TextField1	
    	 * 	
    	 * @return javax.swing.JTextField	
    	 */
    	private JTextField getTextField1() {
    		if (TextField1 == null) {
    			TextField1 = new JTextField();
    			TextField1.setBounds(new Rectangle(29, 14, 780, 45));
    			TextField1.setText("DYANMIC WEB SURVEY TOOL");
    			TextField1.setBorder(BorderFactory.createEmptyBorder(10,280, 10,10));
    			TextField1.setBackground(Color.PINK);
    			TextField1.setForeground(Color.DARK_GRAY);
    		//	TextField1.setFont(FontFamily.getAllFamilyNames()));
    		}
    		return TextField1;
    	}
     
    	/**
    	 * This method initializes ScrollPane	
    	 * 	
    	 * @return javax.swing.JScrollPane	
    	 */
    	private JScrollPane getScrollPane() {
    		if (ScrollPane == null) {
    			ScrollPane = new JScrollPane();
    			ScrollPane.setBounds(new Rectangle(90, 93, 655, 150));
    			ScrollPane.setViewportView(getTable1());
    		}
    		return ScrollPane;
    	}
     
    	/**
    	 * This method initializes Table1	
    	 * 	
    	 * @return javax.swing.JTable	
    	 */
    	private JTable getTable1() {            
    		if (Table1 == null) {
     
    			String header[]={"S.No.","Survey NO.","Survey Name","Status"};
    			String data[][]=new String[5][4];
    			DefaultTableModel model=new DefaultTableModel(data,header);
    			Table1 = new JTable(model);
    	        //JTableHeader headers = Table1.getTableHeader();		
    			Table1.setVisible(true);
     
    		}
    		return Table1;
    	}
     
    	/**
    	 * This method initializes TextField2	
    	 * 	
    	 * @return javax.swing.JTextField	
    	 */
    	private JTextField getTextField2() {
    		if (TextField2 == null) {
    			TextField2 = new JTextField();
    			TextField2.setBounds(new Rectangle(309, 298, 192, 31));
    			TextField2.setText("Create New Survey?");
    			TextField2.setBorder(BorderFactory.createEmptyBorder(10,35, 5,10));
    			TextField2.setBackground(Color.LIGHT_GRAY);
     
    		}
    		return TextField2;
    	}
    	/**
    	 * This method initializes Button1	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getButton1() {
    		if (Button1 == null) {
    			Button1 = new JButton();
    			Button1.setBounds(new Rectangle(348, 347, 115, 31));
    			Button1.setText("CREATE");
     
    		}
    		return Button1;
    	}
     
     
     
    	public static void main(String args[])
    	{
    		Main obj=new Main();
    		//obj.getDesktopPane2();
    		obj.setVisible(true);
    	}
    }  //  @jve:decl-index=0:visual-constraint="10,10"

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Login Page error

    Quote Originally Posted by abhiM View Post
    package pck1;
    Your main is not a pacakge.
    Try importing the package instead.

    import directory/pck1.*;


    And that is not how you define a main. Should be:
    public static void main(String Args[]) extends JDesktopPane {
    Last edited by Spidey1980; August 22nd, 2011 at 10:02 AM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Login Page error

    nothing happens after verifying the code.Can someone tell me where I am going wrong?
    I'm sure something happens. But not what you want. What do you expect to happen?
    Try debugging your code by adding some printlns to show where the execution flow goes. Add printlns to all the methods and constructors and inside of if statements so you will see from the print out where the execution goes.

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Login Page error

    Quote Originally Posted by Norm View Post
    I'm sure something happens. But not what you want. What do you expect to happen?
    Try debugging your code by adding some printlns to show where the execution flow goes. Add printlns to all the methods and constructors and inside of if statements so you will see from the print out where the execution goes.

    I tried debugging it.The control transfers to Main.java file but only executes some of the code like Main() constructor,initialize() and getDesktopPane2() and nothing is displayed.It should display the desktopPane,textfield,button and other things.But nothing is displayed on the screen.If I run only Main.java file then it is displaying all these things but when I access this file from no.java then it does not display it.
    Last edited by abhiM; August 22nd, 2011 at 11:44 PM.

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Login Page error

    Quote Originally Posted by Spidey1980 View Post
    Your main is not a pacakge.
    Try importing the package instead.

    import directory/pck1.*;


    And that is not how you define a main. Should be:
    public static void main(String Args[]) extends JDesktopPane {

    I couldnt understand that how inheriting JDesktopPane will affect the whole program?I have to access this Main.java from no.java.So technically I can even work with no main(String [] args) in Main.java also.I have written main(String [] args) to check that weather the code of Main.java is executing alone or not.And it is executing when I am running only Main.java but it is not executing from no.java

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Login Page error

    Why do you have the Main class extend the JDesktopPane class?
    Can a JDesktopPane class be displayed outside of a frame?

Similar Threads

  1. Need help in login page in struts
    By vinothkumarrvk in forum Web Frameworks
    Replies: 4
    Last Post: October 4th, 2011, 02:10 PM
  2. Unable to view jsp pages through same login page
    By Rajiv in forum Web Frameworks
    Replies: 1
    Last Post: August 17th, 2011, 11:43 AM
  3. Replies: 1
    Last Post: August 12th, 2011, 10:09 AM
  4. login page with image
    By balu424525 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 26th, 2011, 11:24 AM
  5. Error Page in JSP Having Problems.
    By goldest in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 15th, 2010, 09:03 AM

Tags for this Thread