Java GUI problem

GUI, Java, Multiple frames

Page 1 of 1

5 Replies - 2881 Views - Last Post: 16 October 2010 - 11:16 AM Rate Topic: -----

#1 biggie_011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-July 10

Java GUI problem

Posted 15 October 2010 - 12:37 PM

Hey everyone,
Writing a GUI based java program that would interact with a mySQL database. Where a new user can be created, existing user can log in, and password can be recovered if you answer a security question that is made up by the user upon registration. I haven't done the password recovery stuff yet. I'm just having problem getting the Existing User and Forgotten UserName/Password panels to display, where as the NewUser displays with no problem.
I have a feeling it's something really simple, but I just can't seem to find it.
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Main {

   
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("");
        MainWindow m = new MainWindow(frame);
        NewUser nu = new NewUser(frame, m);
        ExistingUser eu = new ExistingUser(frame, m);
        ForgottenUserPass fpu = new ForgottenUserPass(frame, m);

        m.Setter(nu, eu, fpu);

        frame.getContentPane().add(m, BorderLayout.CENTER);
        m.disp();
        frame.setSize(680, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        try {
            Connection con;
            Statement st;
            ResultSet rs;
            Class.forName("com.mysql.jdbc.Driver");
            String path = "jdbc:mysql://localhost/mysql";
            String user = "root";
            String password = "getsin0";
            con = DriverManager.getConnection(path, user, password);
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            String SQLCommand = "drop database if exists allUsers;";
            st.execute(SQLCommand);
            SQLCommand = "create database allUsers;";
            st.executeUpdate(SQLCommand);
            SQLCommand = "use allUsers;";
            st.executeUpdate(SQLCommand);
            SQLCommand = "create table currentUsers( userName varchar(8) primary key not null unique,"
                    + "password varchar(12), fName varchar(10), lName varchar(12), phoneNumber varchar(10) "
                    + "email varchar(20),accessCode int(9), secretQuestion varchar(20), answer varchar(12))  ";
            st.executeUpdate(SQLCommand);
            SQLCommand = "load data local infile 'D:/School/Java3/Week5/users.txt' into table allUsers lines terminated by '\r\n';";
            st.executeUpdate(SQLCommand);

        } catch (ClassNotFoundException cnfe) {
            System.out.println(cnfe.getMessage());
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
        } 
    }
}


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MainWindow extends JApplet {

    private JFrame jframe;
    private JPanel p1;
    private JLabel message;    
    private JButton newUser = new JButton("New User");
    private JButton existing = new JButton("Existing User");
    private JButton forgotten = new JButton("Forgotten UserName/Password");
    private NewUser nu;
    private ExistingUser eu;         
    private ForgottenUserPass fup;         

    public void setNu(NewUser nu) {
        this.nu = nu;
    }

    public void setEu(ExistingUser eu) {
        this.eu = eu;
    }

    public void setFup(ForgottenUserPass fup) {
        this.fup = fup;
    }


    public void disp() {
        jframe.setTitle("Main Window");
        jframe.getContentPane().removeAll();    
        jframe.getContentPane().add(this, BorderLayout.CENTER);
        message.setText("");        
        p1.updateUI();
        newUser.updateUI();
        existing.updateUI();
        forgotten.updateUI();
        jframe.setVisible(true);
    }

    public void Setter(NewUser NU, ExistingUser EU, ForgottenUserPass FUP) {
        this.setNu(NU);
        this.setEu(EU);
        this.setFup(FUP);
    }

    public MainWindow(JFrame jf) {
        
        jframe = jf;
        jframe.setTitle("Main Window");       
        jframe.getContentPane().add(this, BorderLayout.CENTER);        
        p1 = new JPanel();
        p1.setLayout(new GridLayout(1,3,3,3) );        
        p1.add(new JLabel("Please choose what you want to do: "));
        p1.add(newUser);
        p1.add(existing);
        p1.add(forgotten);

        add(p1, BorderLayout.CENTER);
        add(message = new JLabel(""), BorderLayout.NORTH);
        
        newUser.addActionListener(new ActionListener() {           
            public void actionPerformed(ActionEvent e) {                
                try {
                    nu.disp();
                } catch (Exception ex) {
                    message.setText("Error!");
                }
            }
        });
        existing.addActionListener(new ActionListener() {            
            public void actionPerformed(ActionEvent e) {
                try {
                    eu.disp();
                } catch (Exception ex) {
                    message.setText("Error!");
                }
            }
        });
        forgotten.addActionListener(new ActionListener() {            
            public void actionPerformed(ActionEvent e) {
                try {
                    fup.disp();
                } catch (Exception ex) {
                    message.setText("Error!");
                }
            }
        });

    }

}



import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class NewUser extends JApplet {
    private JFrame jframe;
    private JPanel p1;
    private JTextField fname, lname, phoneNumber, accessCode, emailId, userName, password, secretQuestion, answer;
    private JLabel message;    
    private JButton addU = new JButton("AddMe");
    private JButton back = new JButton("Back");
    private MainWindow mw;
    String fn, ln, pn, ei, un, ps, sq, a, SQLCommand;
    int ac;

    public void Reset() {
        message.setText("");
        this.Clear();
    }

    public void Clear() {
        fname.setText("");
        lname.setText("");
        phoneNumber.setText("");
        accessCode.setText("");
        emailId.setText("");
        userName.setText("");
        password.setText("");
        secretQuestion.setText("");
        answer.setText("");
    }

    public void disp() {
        jframe.setTitle("Add User");
        this.Reset();
        jframe.getContentPane().removeAll();   
        jframe.getContentPane().add(this, BorderLayout.CENTER);        
        p1.updateUI();
        addU.updateUI();
        back.updateUI();
        jframe.setVisible(true);
    }

    public NewUser(JFrame jf, MainWindow m) {
        
        jframe = jf;
        mw = m;
        jframe.setTitle("Add User");        
        jframe.getContentPane().add(this, BorderLayout.CENTER);      
        p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        p1.add(new JLabel("First Name: "));
        p1.add(fname = new JTextField(15));
        p1.add(new JLabel("Last Name: "));
        p1.add(lname = new JTextField(15));
        p1.add(new JLabel("Phone Number: "));
        p1.add(phoneNumber = new JTextField(10));
        p1.add(new JLabel("Access Code: "));
        p1.add(accessCode = new JTextField(10));
        p1.add(new JLabel("Email: "));
        p1.add(emailId = new JTextField(20));
        p1.add(new JLabel("User Name: "));
        p1.add(userName = new JTextField(8));
        p1.add(new JLabel("Password: "));
        p1.add(password = new JTextField(10));
        p1.add(new JLabel("Secret Question: "));
        p1.add(secretQuestion = new JTextField(20));
        p1.add(new JLabel("Answer to Secret Question (one word only): "));
        p1.add(answer = new JTextField(10));
        p1.add(addU);
        p1.add(message = new JLabel(""));
        add(p1, BorderLayout.CENTER);
        add(back, BorderLayout.SOUTH);
        
        back.addActionListener(new ActionListener() {        
            public void actionPerformed(ActionEvent e) {
                try {
                    mw.disp();
                } catch (Exception ex) {
                    message.setText("Error!");
                }
            }
        });

      
        addU.addActionListener(new ActionListener() {         
            public void actionPerformed(ActionEvent e) {    
                fn = fname.getText().trim();
                ln = lname.getText().trim();
                pn = phoneNumber.getText().trim();
                ei = emailId.getText().trim();
                un = userName.getText().trim();
                ps = password.getText().trim();
                sq =secretQuestion.getText().trim();
                a = answer.getText().trim();
                ac = Integer.parseInt(accessCode.getText().trim());

                if (inputValid()) {   

                                     try {
                    Connection con;
                    Statement st;
                    ResultSet rs;
                    Class.forName("com.mysql.jdbc.Driver");
                    String path = "jdbc:mysql://localhost/allUsers";
                    String user = "root";
                    String password = "getsin0";
                    con = DriverManager.getConnection(path, user, password);
                    st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
                    SQLCommand = "select userName from currentUsers where username = '"+ un +"';";
                    rs=st.executeQuery(SQLCommand);
                    if(rs.next() == true){
                        message.setText("UserName already taken");
                    }else{
                    SQLCommand = "insert into currentUsers(userName, password, fName,"
                            + "lName, phoneNumber, email, accessCode, secretQuestion, answer) values('" + un
                            + "', '" + ps + "', '" + fn +"', '" + ln +"', '" + pn + "', '" + ei + "', " + ac + ", '" + sq + "', '" + a + ");" ;
                    st.executeUpdate(SQLCommand);
                 String input = "\r\n"+ un + "\t" + ps + "\t" + fn + "\t" + ln + "\t" + pn + "\t" + ei + "\t" + ac + "\t" + sq + "\t" + a + "\t";
                FileWriter out = new FileWriter("D:/School/Java3/Week5/users.txt", true);
                BufferedWriter writer = new BufferedWriter(out);
                writer.write(input);
                writer.close();
                    }
                } catch (ClassNotFoundException cnfe) {
                    System.out.println(cnfe.getMessage());
                } catch (SQLException sqle) {
                    System.out.println(sqle.getMessage());
                } catch(IOException ioe){
                    System.out.println(ioe.getMessage());
                }
                    }

                }


            private boolean inputValid() {                
                if (fn.compareTo("") != 0 && ln.compareTo("") != 0
                        && pn.compareTo("") != 0 && ei.compareTo("") != 0 && un.compareTo("")!= 0
                        && ps.compareTo("") != 0 && sq.compareTo("") != 0
                        && a.compareTo("") !=0 && un.length() < 9) {

                    return true;
                } else {
                    return false;
                }
            }
        });
    }
}


import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class ExistingUser extends JApplet {

    private JFrame jframe;
    private JPanel p1;
    private JTextField userName, password;
    private JLabel message;
    private JButton login = new JButton("Login");
    private JButton back = new JButton("Back");
    private MainWindow mw;
    String un, ps, SQLCommand;

    public void Reset() {
        message.setText("");
        this.Clear();
    }

    public void Clear() {
        userName.setText("");
        password.setText("");
    }

    public void disp() {
        jframe.setTitle("Login");
        this.Reset();
        jframe.getContentPane().removeAll();
        jframe.getContentPane().add(this, BorderLayout.CENTER);
        p1.updateUI();
        login.updateUI();
        back.updateUI();
        jframe.setVisible(true);
    }

    public ExistingUser(JFrame jf, MainWindow m) {
        p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        p1.add(new JLabel("User Name: "));
        p1.add(userName = new JTextField(8));
        p1.add(new JLabel("Password: "));
        p1.add(password = new JPasswordField(10));
        p1.add(login);
        p1.add(message = new JLabel(""));
        add(p1, BorderLayout.CENTER);
        add(back, BorderLayout.SOUTH);


        back.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    mw.disp();
                } catch (Exception ex) {
                    message.setText("Error!");
                }
            }
        });
        login.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                un = userName.getText().trim();
                ps = password.getText().trim();

                if (inputValid()) {

                    try {
                        Connection con;
                        Statement st;
                        ResultSet rs;
                        Class.forName("com.mysql.jdbc.Driver");
                        String path = "jdbc:mysql://localhost/allUsers";
                        String user = "root";
                        String password = "getsin0";
                        con = DriverManager.getConnection(path, user, password);
                        st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
                        SQLCommand = "select userName, password from currentUsers where username = '" + un + "' and password = '" + ps + "';";
                        rs = st.executeQuery(SQLCommand);
                        if (rs.next()) {
                            message.setText("Welcome to my Website!");
                        } else {
                            message.setText("Sorry your username or password is incorrect. Login unsuccessful");
                        }

                    } catch (ClassNotFoundException cnfe) {
                        System.out.println(cnfe.getMessage());
                    } catch (SQLException sqle) {
                        System.out.println(sqle.getMessage());
                    }
                }
            }

            private boolean inputValid() {
                if (un.compareTo("") != 0 && ps.compareTo("") != 0 && un.length() < 9) {
                    return true;
                } else {
                    return false;
                }
            }
        });

    }
}


import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class ForgottenUserPass extends JApplet {

    private JFrame jframe;
    private JPanel p1;
    private JTextField fName, lName, phoneNum;
    private JLabel message;
    private JButton sub = new JButton("Submit");
    private JButton back = new JButton("Back");
    private MainWindow mw;
    String fn, ln, pn, SQLCommand;

    public void Reset() {
        message.setText("");
        this.Clear();
    }

    public void Clear() {
        fName.setText("");
        lName.setText("");
        phoneNum.setText("");
    }

    public void disp() {
        jframe.setTitle("Recover Password");
        this.Reset();
        jframe.getContentPane().removeAll();
        jframe.getContentPane().add(this, BorderLayout.CENTER);
        p1.updateUI();
        sub.updateUI();
        back.updateUI();
        jframe.setVisible(true);
    }

    public ForgottenUserPass(JFrame jf, MainWindow m) {
        p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        p1.add(new JLabel("First Name: "));
        p1.add(fName = new JTextField(8));
        p1.add(new JLabel("Password: "));
        p1.add(lName = new JTextField(10));
        p1.add(new JLabel("Phone Number: "));
        p1.add(phoneNum = new JTextField(10));
        p1.add(sub);
        p1.add(message = new JLabel(""));
        add(p1, BorderLayout.CENTER);
        add(back, BorderLayout.SOUTH);

        back.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    mw.disp();
                } catch (Exception ex) {
                    message.setText("Error!");
                }
            }
        });
        sub.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                fn = fName.getText().trim();
                ln = lName.getText().trim();
                pn = phoneNum.getText().trim();

                if (inputValid()) {
                    try {
                        Connection con;
                        Statement st;
                        ResultSet rs;
                        Class.forName("com.mysql.jdbc.Driver");
                        String path = "jdbc:mysql://localhost/allUsers";
                        String user = "root";
                        String password = "getsin0";
                        con = DriverManager.getConnection(path, user, password);
                        st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
                        SQLCommand = "select fName, lName, phoneNumber from currentUsers where fName = '" + fn + "' and lName = '"
                                + ln + "', '" + pn + "';";
                        rs = st.executeQuery(SQLCommand);
                        if (rs.next()) {
                            message.setText("Welcome to my Website!");
                        } else {
                            message.setText("Sorry your username or password is incorrect. Login unsuccessful");
                        }

                    } catch (ClassNotFoundException cnfe) {
                        System.out.println(cnfe.getMessage());
                    } catch (SQLException sqle) {
                        System.out.println(sqle.getMessage());
                    }
                }
            }

            private boolean inputValid() {
                if (fn.compareTo("") != 0 && ln.compareTo("") != 0 && pn.length() < 9) {
                    return true;
                } else {
                    return false;
                }
            }
        });

    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java GUI problem

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Java GUI problem

Posted 15 October 2010 - 02:48 PM

A lot of code, not a comment in sight. Can you boil it down for us to the key calls/classes/methods that don't seem to be working?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,368
  • Joined: 27-December 08

Re: Java GUI problem

Posted 15 October 2010 - 06:11 PM

When you modify the Layout of a JPanel or JFrame significantly, it's a good idea to invoke revalidate() (JComponent) or validate() (Component, like JFrame) to force a re-layout. This seems to be what is happening here.

Also, why are you using JApplets here? It would make more sense just to use JPanels. In addition, you are reinventing the wheel. Java comes with a LayoutManager for swapping between Components and Containers for a Container called CardLayout. It will make your life much easier rather than having to worry about invoking validate() or revalidate().
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java GUI problem

Posted 15 October 2010 - 08:18 PM

Do you really expect us to read more than 600 lines of code, without a single comment, and find by magic what your problem is ?

Narrow down your problem and tell us where it breaks. You should have run it before, after which addition it started to have an unexpected behaviour ?
Was This Post Helpful? 0
  • +
  • -

#5 biggie_011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-July 10

Re: Java GUI problem

Posted 16 October 2010 - 11:15 AM

Well I got it working, just missing a couple lines of code, thanks guys, sorry about the no comments, i kind of just went on a 2 hour programming binge and i didn't stop to make any comments.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,368
  • Joined: 27-December 08

Re: Java GUI problem

Posted 16 October 2010 - 11:16 AM

Out of curiosity, can you share what you did to get it working? :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1