Results 1 to 5 of 5
  1. #1
    mkkrish is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default How to pass user-id between .java programs

    Hi,
    Can any one help to get rid of this problem.

    I have login.java, admin_menu.java and user_menu.java classes.

    When user starts the application login.java is invoked and the user get appropriate menu like admin_menu.java or user_menu.java.

    I have added an option to change password in admin_menu.java and user_menu.java.

    How to pass user-id from login.java to admin_menu.java and user_menu.java
    after successful login.

    Any idea , please help.

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    login.java is invoked and the user get appropriate menu like ...
    Information can be passed from one class to another
    via a method call, a command line string, or through a file.
    It all depends on how control is passed from login.java to the menu.

    Let's assume you are making a new admin_menu or user_menu.
    I recommend creating another class of object just to keep the userId
    and other global information. The login class creates one of these
    communication objects and passes it to any children.
    Children can then access the global data from the object.

  3. #3
    mkkrish is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    Thank you.

    If it is possible , can you please give and example (very small).

    sothat I can incorporate the same idea. Now I have login program. I am listing it here. Kindly show what should be done to pass the use_id to
    adm_menu.java and clk_menu.java.


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import java.io.*;
    import java.sql.*;
    import java.util.*;
    import MyPack.*;
    public class bislogin extends JFrame
    {

    JLabel jl1,jl2,jl3,jl4;
    JTextField jt1;
    JPasswordField jpf1;
    JButton jb1;
    JPanel top,mid,bot;
    Connection con = null;

    public amlogin()
    {

    setTitle("Login To System");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = getContentPane();
    JPanel top = new JPanel();

    JLabel Title = new JLabel("AM Information system");
    Title.setFont(new Font("TimesRoman",Font.BOLD,25));
    Title.setForeground(new Color(254,255,232));
    top.setBackground(new Color(151,51,2));
    top.setPreferredSize(new Dimension(500,50));
    top.setMinimumSize(new Dimension(500,25));
    top.setMaximumSize(new Dimension(500,25));
    top.add(Title);
    pane.add("North",top);

    JPanel mid = new JPanel();
    mid.setLayout(null);
    mid.setBackground(new Color(251,206,198));
    mid.setBorder(new EmptyBorder(new Insets(20,20,20,20)));
    mid.setPreferredSize(new Dimension(500,80));
    mid.setMinimumSize(new Dimension(500,80));
    mid.setMaximumSize(new Dimension(500,80));
    pane.add("Center", mid);

    jl1 = new JLabel("UserName");
    jl1.setHorizontalAlignment(SwingConstants.LEFT);
    jl1.setForeground(new Color(0,0,255));

    jl2 = new JLabel("Password");
    jl2.setHorizontalAlignment(SwingConstants.LEFT);
    jl2.setForeground(new Color(0,0,255));

    jl3 = new JLabel("Enter Username and Password To Login");
    jl3.setHorizontalAlignment(SwingConstants.LEFT);
    jl3.setForeground(new Color(0,0,255));

    jl4 = new JLabel("BIS System Project is Developed for the Completion of IGNOU MCA");
    jl4.setForeground(new Color(254,255,232));

    jt1 =new JTextField();

    jt1.setForeground(new Color(0, 0, 255));
    jt1.setSelectedTextColor(new Color(0, 0, 255));
    jt1.setToolTipText("Enter your Username");
    jt1.addKeyListener(new KeyAdapter()
    {
    public void keyReleased(KeyEvent e)
    {
    JTextField textField = (JTextField) e.getSource();
    String text = textField.getText();
    textField.setText(text.toLowerCase());
    }
    });
    jt1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    // jt1_actionPerformed(e);
    }
    });
    jpf1 = new JPasswordField();
    jpf1.setForeground(new Color(0, 0, 255));
    jpf1.setSelectedTextColor(new Color(0, 0, 255));
    jpf1.setToolTipText("Enter your Password");
    jpf1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    //jpf1_actionPerformed(e);
    }
    });
    jb1 = new JButton();
    jb1.setBackground(new Color(204, 204, 204));
    jb1.setForeground(new Color(0, 0, 255));
    jb1.setText("Login");
    jb1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {

    String username = new String(jt1.getText());
    String password = new String(jpf1.getText());

    String uid="";
    String pwd="";
    String rol="";
    if(username.equals("") || password.equals(""))
    {
    jb1.setEnabled(false);
    JLabel errorFields = new JLabel("<HTML><FONT COLOR = Blue> "+
    " You must enter a username and password to login.</FONT></HTML>");
    JOptionPane.showMessageDialog(null,errorFields);
    jt1.setText("");
    jpf1.setText("");

    jb1.setEnabled(true);

    }
    else
    {

    try
    {
    con = db_connect.getConnection();
    String sql1 = "select user_id,user_pass,user_role from user_dir where "+
    "user_id=?";
    PreparedStatement ps1= con.prepareStatement(sql1);
    ps1.setString(1,jt1.getText());
    ResultSet prs1 = ps1.executeQuery();
    while (prs1.next())
    {
    uid = prs1.getString(1);
    pwd = prs1.getString(2);
    rol = prs1.getString(3);
    }
    prs1.close();
    ps1.close();
    }
    catch(Exception e1){e1.printStackTrace();};
    if(username.equals(uid) && password.equals(pwd) && rol.equals("ADM"))
    {

    jb1.setEnabled(false);
    jt1.setText(null);
    jpf1.setText(null);
    new admin_menu();
    }

    else if(username.equals(uid) && password.equals(pwd) && rol.equals("CLK"))
    {
    jb1.setEnabled(false);
    jt1.setText(null);
    jpf1.setText(null);
    new clk_menu();
    }
    else
    {
    JOptionPane.showMessageDialog(null,"Login Failed !!!!"+
    " Enter Proper Username/Password");
    jt1.setText(null);
    jpf1.setText(null);
    }

    }
    }

    });

    jl1.setBounds(50,100,106,18);
    jl2.setBounds(50,125,97,18);
    jl3.setBounds(50,50,300,18);
    jt1.setBounds(210,100,183,22);
    jpf1.setBounds(210,125,183,22);
    jb1.setBounds(210,175,83,28);
    mid.add(jl1);mid.add(jl2);mid.add(jl3);
    mid.add(jt1);mid.add(jpf1);mid.add(jb1);

    Graphics g = mid.getGraphics();


    JPanel bot = new JPanel();
    bot.setPreferredSize(new Dimension(500,50));
    bot.setMinimumSize(new Dimension(500,50));
    bot.setMaximumSize(new Dimension(500,50));
    bot.setBackground(new Color(151,51,2));
    pane.add("South",bot);
    bot.add("Center", jl4);


    pack();
    setSize(800,600);
    setVisible(true);
    setResizable(false);

    }
    public void paint(Graphics g)
    {
    super.paint(g);
    //g.drawLine(30,30,30,30);
    g.drawRect(40,100,700,400);
    //g.drawOval(60,400,120,120);

    }
    public static void main(String[] args)
    {
    new amlogin();
    }

    }

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Change names to java standard of capitalized words: ClickMenu
    Change the constructor of ClickMenu to have an argument.
    Java Code:
    class ClickMenu {
        String loginID;
        ClickMenu(String name) { loginID = name; }
    ...
    }
    instead of new clk_menu(), the code becomes
    Java Code:
    new ClickMenu(jt1.getText());

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 6
    Last Post: 04-20-2009, 04:34 AM
  2. Java Programs to Test Performance
    By testarosa in forum Advanced Java
    Replies: 4
    Last Post: 02-27-2009, 01:04 PM
  3. Running 3 Java programs in sequence.
    By loktamu in forum New To Java
    Replies: 2
    Last Post: 11-23-2008, 06:50 PM
  4. Problem in running java programs
    By aravind in forum New To Java
    Replies: 0
    Last Post: 07-14-2008, 11:19 AM
  5. Replies: 1
    Last Post: 01-05-2008, 07:48 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
  •