0

i was trying to build this management system. the login page is the main page if the user forgets their password then they can click on the forget password to change the password. but, whenever i call the forgot password from login frame, a small window opens rather than the coded window of forget page. NOTE:the forget window runs fine when executed alone.

login page

    `package hM_prac;
    import java.awt.BorderLayout;
    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;


import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPasswordField;

import java.sql.*;
public class login extends JFrame implements ActionListener {

private JPanel contentPane;
private JTextField txtUsername;
private JPasswordField pwdwhatever;
JLabel lblNewLabel,lblNewLabel_1,lblNewLabel_2;
JButton btnForgotPassword ,btnSignUp,btnLogin;

    /**
     * Launch the application.
     */
public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    login frame = new login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    Connection con;
    PreparedStatement ps;
    ResultSet rs;

    /**
     * Create the frame.
     */
    public login() {
        super("LOGIN");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 465, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new LineBorder(new Color(0, 0, 0)));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        lblNewLabel = new JLabel("Welcome to MYHOTEL");
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setFont(new Font("Verdana", Font.PLAIN, 18));
        lblNewLabel.setBounds(10, 11, 414, 23);
        contentPane.add(lblNewLabel);

        txtUsername = new JTextField();
        txtUsername.setText("");
        txtUsername.setBounds(222, 96, 94, 20);
        contentPane.add(txtUsername);
        txtUsername.setColumns(20);

        lblNewLabel_1 = new JLabel("Username:");
        lblNewLabel_1.setLabelFor(txtUsername);
        lblNewLabel_1.setBounds(121, 99, 69, 14);
        contentPane.add(lblNewLabel_1);

        lblNewLabel_2 = new JLabel("Password:");
        lblNewLabel_2.setBounds(121, 138, 69, 14);
        contentPane.add(lblNewLabel_2);

        btnLogin = new JButton("Login");
        btnLogin.setBounds(169, 166, 89, 23);
        contentPane.add(btnLogin);

        btnSignUp = new JButton("Sign Up");
        btnSignUp.setBounds(305, 215, 89, 23);
        contentPane.add(btnSignUp);

        btnForgotPassword = new JButton("Forgot password");
        btnForgotPassword.setBounds(37, 215, 135, 23);
        contentPane.add(btnForgotPassword);

        pwdwhatever = new JPasswordField();
        pwdwhatever.setText("");
        pwdwhatever.setBounds(222, 136, 94, 17);
        contentPane.add(pwdwhatever);

        btnLogin.addActionListener(this);
        btnSignUp.addActionListener(this);
        btnForgotPassword.addActionListener(this);
    }
        @Override
public void actionPerformed(ActionEvent ae) {
            // TODO Auto-generated method stub

Object o=ae.getSource();
if(o==btnLogin)
{               
String uname,pass;
uname=txtUsername.getText();
pass=pwdwhatever.getText(); 
    try
       {
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hm","hm");
ps=con.prepareStatement("select * from account where username=? and password=?");
ps.setString(1,uname);
ps.setString(2,pass);
rs=ps.executeQuery();
if(rs.next())
{
System.out.println("Valid User.");
JOptionPane.showMessageDialog(this,"Valid User","Done",JOptionPane.INFORMATION_MESSAGE);
this.dispose();
rs.close();
ps.close();
//new homepage();
}
else
{
JOptionPane.showMessageDialog(this,"Incorrect","Error",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
if(o==btnSignUp)
{
this.dispose();
new sign_up().setVisible(true);
}   
if(o==btnForgotPassword)
{
this.dispose();
new forget().setVisible(true);
}
}
}
`

forget page

   ` package hM_prac;

import java.awt.EventQueue;

import javax.swing.JFrame;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import  java.sql.*;
public class forget extends JFrame implements ActionListener {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;
    private JPasswordField passwordField;
    JButton btnSubmit,btnSubmitAnswer;
    JLabel label,lblNewLabel_3;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    System.out.println("hh");
                    forget window = new forget();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public forget() {
        //super("login");
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 374, 358);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("Username:");
        lblNewLabel.setBackground(Color.BLACK);
        lblNewLabel.setBounds(18, 77, 92, 14);
        frame.getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("Security Question:");
        lblNewLabel_1.setBackground(Color.BLACK);
        lblNewLabel_1.setBounds(18, 139, 132, 14);
        frame.getContentPane().add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("Answer:");
        lblNewLabel_2.setBackground(Color.BLACK);
        lblNewLabel_2.setBounds(18, 164, 92, 14);
        frame.getContentPane().add(lblNewLabel_2);


        //frame.getContentPane().add(lblNewLabel_3);

        JLabel lblNewLabel_5 = new JLabel("Forgot password");
        lblNewLabel_5.setFont(new Font("Tahoma", Font.BOLD, 19));
        lblNewLabel_5.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_5.setBounds(10, 11, 338, 23);
        frame.getContentPane().add(lblNewLabel_5);

        textField = new JTextField();
        textField.setBackground(Color.WHITE);
        textField.setBounds(156, 77, 182, 14);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        btnSubmit = new JButton("Submit ");
        btnSubmit.setBackground(Color.WHITE);
        btnSubmit.setBounds(184, 102, 142, 23);
        frame.getContentPane().add(btnSubmit);

        label = new JLabel("..");
        label.setBackground(Color.BLACK);
        label.setBounds(160, 139, 178, 14);
        frame.getContentPane().add(label);

        textField_1 = new JTextField();
        textField_1.setBounds(156, 164, 192, 14);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);

        btnSubmitAnswer = new JButton("Submit Answer");
        btnSubmitAnswer.setBackground(Color.WHITE);
        btnSubmitAnswer.setBounds(184, 189, 142, 23);
        frame.getContentPane().add(btnSubmitAnswer);


        //frame.getContentPane().add(btnSubmitPassword);



        btnSubmit.addActionListener(this);
        btnSubmitAnswer.addActionListener(this);

    }


Connection con;
PreparedStatement ps;
ResultSet rs;
private JButton btnSubmitPassword;

public void actionPerformed(ActionEvent ae) {
Object o=ae.getSource();
String uname;
if(o==btnSubmit)
{       System.out.println("ques");     
uname=textField.getText();
try
   {
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hm","hm");

ps=con.prepareStatement("select security_question from account where username=?");
ps.setString(1,uname);
rs=ps.executeQuery();
if(rs.next())
{
    String secq=rs.getString("security_question");
    System.out.println("question"+secq);
    label.setText(secq);
    frame.getContentPane().add(label);
}
else if(rs==null){
    JOptionPane.showMessageDialog(this,"No such user","Error",JOptionPane.ERROR_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(this,"Invalid User","Error",JOptionPane.ERROR_MESSAGE);
}

}
catch(Exception e)
{
System.out.println(e);
}
}
else if(o==btnSubmitAnswer)
{
    //System.out.println("jj");
    String ans;
    uname=textField.getText();
    ans=textField_1.getText();
    try
       {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hm","hm");
    ps=con.prepareStatement("select * from account where username=? and answer=?");
    ps.setString(1,uname);
    ps.setString(2,ans);
    rs=ps.executeQuery();
    if(rs!=null)
    { 
    System.out.println("jj");
    lblNewLabel_3 = new JLabel("New Password:");
    lblNewLabel_3.setBackground(Color.BLACK);
    lblNewLabel_3.setBounds(18, 226, 117, 14);
    frame.getContentPane().add(lblNewLabel_3);

    passwordField = new JPasswordField();
    passwordField.setBackground(Color.BLACK);
    passwordField.setBounds(160, 226, 178, 17);
    //frame.getContentPane().add(passwordField);    
    frame.getContentPane().add(passwordField);

    btnSubmitPassword = new JButton("Submit password");
    btnSubmitPassword.setBounds(82, 285, 182, 23);
    frame.getContentPane().add(btnSubmitPassword);

    btnSubmitPassword.addActionListener(this);      
    }
    else{
        JOptionPane.showMessageDialog(this,"Wrong answer","Error",JOptionPane.ERROR_MESSAGE);
    }

    }catch(Exception e){
        e.printStackTrace();
    }

}
else if(o==btnSubmitPassword){
    String pass;
    uname=textField.getText();
    pass=passwordField.getText();
    try
       {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hm","hm");
    ps=con.prepareStatement("update account set  password=? where username=?");
    ps.setString(1,pass);
    ps.setString(1,uname);
    rs=ps.executeQuery();
    if(rs==null)
    {
        JOptionPane.showMessageDialog(this,"Error","Error",JOptionPane.ERROR_MESSAGE);
    }
    else{
        JOptionPane.showMessageDialog(this,"Password reset","Error",JOptionPane.ERROR_MESSAGE);
        this.dispose();
        new login();
    }}catch(Exception e){
        e.printStackTrace();
    }
}
}
}


`
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Jul 02 '18 at 05:39
  • .. 3) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 4) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 5) Don't extend components without good reason. (I've never seen a compelling reason to extend a `JFrame`. – Andrew Thompson Jul 02 '18 at 05:40
  • `public class forget extends JFrame ` contains `private JFrame frame` - which one is actually been displayed (hint: Not the one you think) - Drop `extends JFrame` as a starting point – MadProgrammer Jul 02 '18 at 05:45
  • THANKS!!! i removed the JFrame had to do the workaround but it worked. – Yuvraj bhanot Jul 02 '18 at 10:02
  • *"THANKS!!! i .."* Tip: Add @MadProgrammer (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Jul 02 '18 at 10:29

1 Answers1

0

Here is a better way to show another window from a JFrame (as is in this case of showing a "forgot password" window from "login" window) is to define it as a modal JDialog. Here is some working code to demonstrate what is possible:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainWindow {
    public static void main(String [] args) {
        new MainWindow();
    }
    private JFrame frame;
    public MainWindow() {   
        frame = new JFrame("Main Window - login");  
        JButton button = new JButton("Click me to open dialog");
        button.addActionListener(new ButtonListener());
        frame.add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 300);
        frame.setLocation(200, 200);
        frame.setVisible(true);
    }
    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            frame.setVisible(false);
            new DialogWindow();  
        }
    }
}
class DialogWindow {
    DialogWindow() {
        JDialog dialog = new JDialog();
        dialog.setTitle("Dialog - forgot");
        dialog.setModal(true);  
        dialog.add(new JLabel("Do something here..."));
        dialog.setSize(250, 150);
        dialog.setLocation(600, 200);
        dialog.setVisible(true);
    }
}

In the above code the MainWindow can be assumed as the login window and the DialogWindow as the forgot password dialog. When the user clicks the button in the login window, the forgot dialog opens as a modal window. Then the user does some work in the forgot dialog; and on closing it returns to the main window.

Here is a link to Oracle's Java Swing tutorials with examples of how to use JFrame, JDialog and other GUI components.

prasad_
  • 12,755
  • 2
  • 24
  • 36
  • thank you for the answer !! but i dont want to open a dialog box . i want the login window to shut and forget password window to come up. But with my code the main of "forget" is not triggered which i dont know why? i created its object from the "login page" – Yuvraj bhanot Jul 02 '18 at 09:48
  • You can add the following code to `ButtonListener's actionPerformed` method: `frame.setVisible(false);` (note the `frame` variable need to be defined as an instance variable). – prasad_ Jul 02 '18 at 10:00