-1

Can anyone help me to correct this Java?

I want to pass the data from login obj JFrame to my mainform class but I do not know how to do it!

Here is my code:

Login.java

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

class Login extends JFrame implements ActionListener{

    private JTextField tfUser;
    private JPasswordField tfPass;
    private JButton btnLogin,btnCancel;
    private JComboBox cbotype;
    private String user,pass;
    private Connection con;
    private Statement stmt;
    private ResultSet rs;
    public Login(){
        super("Login");
        initGUI();
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==btnLogin) pLogin();
        else if(e.getSource()==btnCancel) cancel();
    }

    public void cancel(){ dispose();
    }
    public void pLogin(){
        if(isLogin())
            setVisible(false);
        System.out.print(user);
    }
    public boolean isLogin(){
        String sms="Welcome!!!",table=cbotype.getSelectedItem().toString();
        pass=tfPass.getText();
        user=tfUser.getText();
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
            String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=dbLibrary;user=sa;password=123;";
            Connection con = DriverManager.getConnection(connectionUrl);
            stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            rs=stmt.executeQuery("SELECT username,password FROM " + table +" Where username='"+ user +"'");     
            if(rs.next ()){
                rs.first();
                if(rs.getString(2).compareTo(pass)==0)
                {   JOptionPane.showMessageDialog(null,sms);
                    return true;
                }else{
                    sms="Incorrect Password! Please, try it again!";
                }
            }else
            {sms="Incorrect User or Password! Please, try again!";}
            JOptionPane.showMessageDialog(null,sms);
        }catch(SQLException e){ e.printStackTrace();
        }catch(ClassNotFoundException e1){e1.printStackTrace();
        }return false;
    }
    public void initGUI(){
        JPanel p1=new JPanel(new FlowLayout());
        p1.add(Interface.title(new JLabel("Login"),"images/login_0.png"));
        //con.add(p1);
        JPanel p2=new JPanel(new GridLayout(3,2));
        p2.add(Interface.label(new JLabel("Type:")));
        p2.add(cbotype=new JComboBox());
        cbotype.addItem("Employee");cbotype.addItem("Patron");
        p2.add(Interface.label(new JLabel("User:")));
        p2.add(tfUser=new JTextField(20));
        p2.add(Interface.label(new JLabel("Password:")));
        p2.add(tfPass=new JPasswordField(20));
        //con.add(p2);

        JPanel p3=new JPanel(new FlowLayout());
        p3.add(Interface.button(btnLogin=new JButton("Login"),"images/login_1.png"));
        p3.add(Interface.button(btnCancel=new JButton("Cancel"),"images/login_2.png"));

        JPanel p=new JPanel(new BorderLayout());
        p.add(p1,"North");
        p.add(p2,"Center");
        p.add(p3,"South");
        add(p);
        pack();

        btnLogin.addActionListener(this);
        btnCancel.addActionListener(this);
    }
    public String getUser(){
        return user;
    }
    public String getPass(){
        return pass;
    }
     public static void main(String[] args) {
        new Login();

    }
}

MainForm.java

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

public class MainForm extends JFrame implements ActionListener{

private Container con;

    private String user;

    private JButton btnshow;

    public MainForm() {
        initGUI();
        pack();
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public void initGUI(){
        Login login=new Login();
        user=login.getUser();
        //login.dispose();
        System.out.print(user);
        con=getContentPane();
        con.add(btnshow=new JButton("Show"));
        btnshow.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==btnshow) {System.out.print(user); 
        }
    }
    public static void main(String[] args) {
        new MainForm();
    }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Ouch, you're storing passwords in plain text? – Thomas Apr 21 '13 at 09:05
  • From MainForm you create a Login instance. Login has public getter for user + pass. You may call the reference to Login form MainForm to get the data. The rest is up to you ... – PeterMmm Apr 21 '13 at 09:06
  • *"in JAVA"* No need to put tags in titles, and it is spelled Java *"thx in advance!!!!"* Your thanks would be better expressed by spelling words properly. This is not a text message. – Andrew Thompson Apr 21 '13 at 09:07

2 Answers2

1

One way: When you call the Login constructor, is to parse the MainForm instance to it. Your Login class will keep a reference to the MainForm and when you need to let the MainForm know call a method on the MainForm from within the Login class. This is pretty much the same way listeners work...

In your MainForm class

Login login=new Login(this);

In your Login class when you want to let the MainForm know:

this.mainForm.someListenerMethod();
NeilA
  • 1,450
  • 2
  • 12
  • 20
1

See The Use of Multiple JFrames, Good/Bad Practice? The log-in should be a modal JDialog or a JOptionPane. Pop the dialog from the frame, then examine the relevant fields in the next line of code.

Try looking over this code for tips.

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

public class MainForm extends JFrame implements ActionListener{

private Container con;

    private String user;

    private JButton btnshow;

    public MainForm() {
        initGUI();
        pack();
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void initGUI(){
        Login login=new Login(this);
        user=login.getUser();
        //login.dispose();
        System.out.print(user);
        con=getContentPane();
        con.add(btnshow=new JButton("Show"));
        btnshow.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==btnshow) {System.out.print(user);
        }
    }
    public static void main(String[] args) {
        new MainForm();
    }
}

class Login extends JDialog implements ActionListener{

    private JTextField tfUser;
    private JPasswordField tfPass;
    private JButton btnLogin,btnCancel;
    private JComboBox cbotype;
    private String user,pass;
    private Connection con;
    private Statement stmt;
    private ResultSet rs;
    public Login(Frame f){
        // make it modal
        super(f, true);
        initGUI();
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==btnLogin) pLogin();
        else if(e.getSource()==btnCancel) cancel();
    }

    public void cancel(){ dispose();
    }
    public void pLogin(){
        //if(isLogin())
            setVisible(false);
        System.out.print(user);
    }
    public boolean isLogin(){
        String sms="Welcome!!!",table=cbotype.getSelectedItem().toString();
        // never store passwords as a String, left as an exercise for the user
        pass = new String(tfPass.getPassword());
        user= tfUser.getText();
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=dbLibrary;user=sa;password=123;";
            Connection con = DriverManager.getConnection(connectionUrl);
            stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            rs=stmt.executeQuery("SELECT username,password FROM " + table +" Where username='"+ user +"'");
            if(rs.next ()){
                rs.first();
                if(rs.getString(2).compareTo(pass)==0)
                {   JOptionPane.showMessageDialog(null,sms);
                    return true;
                }else{
                    sms="Incorrect Password! Please, try it again!";
                }
            }else
            {sms="Incorrect User or Password! Please, try again!";}
            JOptionPane.showMessageDialog(null,sms);
        }catch(SQLException e){ e.printStackTrace();
        }catch(ClassNotFoundException e1){e1.printStackTrace();
        }return false;
    }
    public void initGUI(){
        JPanel p1=new JPanel(new FlowLayout());
        p1.add(new JLabel("Login"));
        //con.add(p1);
        JPanel p2=new JPanel(new GridLayout(3,2));
        p2.add(new JLabel("Type:"));
        p2.add(cbotype=new JComboBox());
        cbotype.addItem("Employee");cbotype.addItem("Patron");
        p2.add(new JLabel("User:"));
        p2.add(tfUser=new JTextField(20));
        p2.add(new JLabel("Password:"));
        p2.add(tfPass=new JPasswordField(20));
        //con.add(p2);

        JPanel p3=new JPanel(new FlowLayout());
        p3.add(btnLogin =  new JButton("Login"));
        p3.add(btnCancel = new JButton("Cancel"));

        JPanel p=new JPanel(new BorderLayout());
        // Don't use magic numbers!  There are defined constants
        // for these constraints.
        p.add(p1,BorderLayout.NORTH);
        p.add(p2,BorderLayout.CENTER);
        p.add(p3,BorderLayout.SOUTH);
        add(p);
        pack();

        btnLogin.addActionListener(this);
        btnCancel.addActionListener(this);
    }
    public String getUser(){
        return user;
    }
    public String getPass(){
        return pass;
    }
}

Other tips

  1. Don't extend frame or dialog, just use instances of them.
  2. Swing & AWT GUIs should be created and updated on the Event Dispatch Thread.
  3. Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow.
  4. See further tips in code comments.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433