Link to home
Start Free TrialLog in
Avatar of _Esam
_Esam

asked on

How to capture input from a swing component.

Hi,
My RMI based client program requires to promt for a log in window...
Currently I just have a JOptionPane window to get the password.
I have a standalone login app.
I need to know how I can integrate it with my app ...

Here is what I have for the main app:

private void setGui() {
    try {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               tabbedPane = new JTabbedPane();
      JPanel p = new JPanel();
               p.add(new JLabel("Some initial message to show"));
               tabbedPane.addTab("UST Course Planner", p);
      getContentPane().add(tabbedPane);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

     private boolean logon() {

          return "secret".equals(JOptionPane.showInputDialog("Enter password"));
     }

     private void makeRemainingTabs(){//Vector strs) throws RemoteException  {
            ttry{
              tabbedPane.addTab("View Courses", new JScrollPane(new JTable(new MyTableModel(myServerObject.getCoursesTaken(id)))));
        }
        catch (Exception e){
        }

        try{

          tabbedPane.addTab("View Requirements", new JScrollPane(new JTable(new MyTableModel(myServerObject.getRequiredCourses(id)))));
                   }
             catch (Exception e){}

     }


In my main method:
CoursePlanner f = new CoursePlanner(myServerObject);
    f.setGui();
    f.setSize(500, 300);
    f.setVisible(true);
          if (f.logon()) {
               f.makeRemainingTabs();
          }


My login app:

/* Logon.java
  3/9/05 */


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

public class Logon1 extends JFrame
{

      public Logon1()
      {
            // Create the window
            super("Log On Window");

            // Create the USD ID panel
            JPanel ustidPanel = new JPanel();
            final JTextField ustidField = new JTextField();

            ustidPanel.add(new JLabel("UST ID: "));
            ustidPanel.add(new JTextField(20));


            // Create the password panel
            JPanel passwordPanel = new JPanel();
            final JPasswordField passwordField = new JPasswordField();

            passwordPanel.add(new JLabel("Password: "));
            passwordPanel.add(new JTextField(20));

            // Create the Log on button
            JButton logonButton = new JButton("Log On");

            // Set layout manager, add UST ID panel, Password panel, and Log on button
            // to the window using Border layout
            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(ustidPanel);
            getContentPane().add(passwordPanel);
            //add(logonButton);

            // Display panels and button within border
            Container c = getContentPane();

            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(ustidPanel, BorderLayout.NORTH);
            getContentPane().add(passwordPanel, BorderLayout.CENTER);
            //getContentPane().add(logonButton, BorderLayout.CENTER);//
            getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(logonButton);


            // Pack window and pop it up
            setSize(310, 200);
            setVisible(true);

            // Create and add listener to the button
            class LogonListener implements ActionListener
            {
                  public void actionPerformed(ActionEvent event)
                  {
                        double id = Double.parseDouble(ustidField.getText());
                        double password = Double.parseDouble(passwordField.getText());
                  }
            }

            ActionListener listener = new LogonListener();
            logonButton.addActionListener(listener);

      }

      public static void main(String args[])
      {
            Logon1 app = new Logon1();

            app.addWindowListener(
                        new WindowAdapter()
                        {
                              public void windowClosing(WindowEvent e)
                              {
                                    System.exit(0);
                              }
                        }
            );

      }

}


Let me know if I can take some easy approach than this...
Please advise..


thanks.
_Esam
Avatar of Mayank S
Mayank S
Flag of India image

>> return "secret".equals(JOptionPane.showInputDialog("Enter password"));

You already have this in the logon method. What exactly is the problem?

Disadvantages of doing it that way are that you're hard-coding the correct-password and you're also allowing the user to enter the password in 'visible' format. You should ideally use a JPasswordField and use its getPassword () method to get the password.
Avatar of _Esam
_Esam

ASKER

Yes, I realized that I will need to use the JPassword and also with setecho???

Problem is I want to replace the logon with my Logon1 app ..
I am not familiar with the getPassword method.

And >>I need to compare a remote methods return value against the entered info..
Do I have to do that within this part?
 
               public void actionPerformed(ActionEvent event)
               {
                    double id = Double.parseDouble(ustidField.getText());
                    double password = Double.parseDouble(passwordField.getText());
               }
          }

          ActionListener listener = new LogonListener();
          logonButton.addActionListener(listener);


the remote method has the signature:
boolean verify(int, String)

So, I will need to change these two:
double id = Double.parseDouble(ustidField.getText());
                    double password = Double.parseDouble(passwordField.getText());

to:
int id = Int.parseInt(ustidField.getText());
String password = (passwordField.getText());  //please provide your alternative

Then I must have some global id and password  that I mast assign to these two....??
And if they match ; return true ???

Please let me know...

Thanks..
_Esam

ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> int id = Int.parseInt(ustidField.getText());

That would be: int id = Integer.parseInt ( ustidField.getText () ) ;
>> I need to compare a remote methods return value against the entered info..
>> Do I have to do that within this part?

It seems you need to compare in the remote method? In that case, you would just read the input in the actionPerformed () method in the way shown above, and then do the comparision in the remote method. It could return a true if they match.

>> Then I must have some global id and password  that I mast assign to these two....??

The remote method could validate it from a data-base or a file where you've saved valid user-IDs and their passwords.
BTW, why is user-ID an 'int'?
Avatar of _Esam

ASKER

I am confused how I can get the id and password and compare it???

I have it as:

class CoursePlanner {
int stid,
String stpwd;
thisclassobject;
     public CoursePlanner(MyObject myserverobject){thisclassobject = myserverobject)
 

void logon() {
 new Logon1();
  }
}

I have to get it from Logon1() in logon() method.>>Logon1 need access to a member variable defined in CoursePlanner>thisclassobject
Logon1() needs this object to execute the verify(id, string) method entered against the  
id and password field...and return true...

void logon() should be defined as boolean,

I am confused..
Let me know if you need clarification..

Thanks.
_Esam
>> class CoursePlanner {

Is this the remote class? Could you clarify as to what is the remote class and what is the class at the UI?
Avatar of _Esam

ASKER

Maybe instead of having a separate class "Logo1" class to display for user info...
I could have just made a JPanel in the "boolean logon()" method of "CoursePlanner" class.
Then it could be easier to access the thisserver object and execute the remote method "verify(id, password)" entered against the user.?

Please let me know..

Thanks.
_Esam
Avatar of _Esam

ASKER

The class CoursePlanner is the Client class.
It has a reference to the remote object.
Here is the complete definition (without desired log on).
It was working with the hardcoded "password"

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.lang.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;


public class CoursePlanner extends JFrame{
     private JTabbedPane tabbedPane;
     //String serverName = null; //argv[0];
     private static myRMIInterface myServerObject = null;
     private static int id = 1204;
     private static Vector rs = null;
     private static JTable tb1;
     //private static myTableModel mtm;

     public CoursePlanner(myRMIInterface server){
             myServerObject = server;}




     /* System.setSecurityManager(new RMISecurityManager());
                       ///if (argv.length != 1)
                              // {
                              // System.out.println("usage: java myRMIClient <IP address of host running RMI server>");
                              // System.exit(0);
                               //}
                       //String serverName = argv[0];
                       try
                               {
                               //bind server object to object in client
                               myRMIInterface myServerObject = (myRMIInterface) Naming.lookup("rmi://"+serverName+"/myRMIImplInstance");
                                           }
                       catch(Exception e)
                               {
                               System.out.println("Exception occured: " + e);
                               System.exit(0);
                               }
                System.out.println("RMI connection successful");

  //}
//catch (Exception ex)
//{
      //System.out.println("Exception occured: " + ex);*/
//}









       ///////private int id = 1204;
       //private static String pass = "nyymo";
       //private static Vector superClasses;



  private void setGui() {
    try {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               tabbedPane = new JTabbedPane();
      JPanel p = new JPanel();
               p.add(new JLabel("Some initial message to show"));
               tabbedPane.addTab("UST Course Planner", p);
      getContentPane().add(tabbedPane);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

     private void logon() {



            new Logon1();


          //return "secret".equals(JOptionPane.showInputDialog("Enter password"));
     }

     private void makeRemainingTabs(){//Vector strs) throws RemoteException  {
             //int stid = 0;
             //this.id = stid;
             //this.rs = strs;
             //rs = myServerObject.getCoursesTaken(id);
             try{
              tabbedPane.addTab("View Courses", new JScrollPane(new JTable(new MyTableModel(myServerObject.getCoursesTaken(id)))));
        //tabbedPane.addTab("View Courses", new JScrollPane(tb1));





        }
        catch (Exception e){
        }

        try{

          tabbedPane.addTab("View Requirements", new JScrollPane(new JTable(new MyTableModel(myServerObject.getRequiredCourses(id)))));
                   }
             catch (Exception e){}

     }






public static void main(String[] argv) //throws RemoteExcecpion
                {
               System.setSecurityManager(new RMISecurityManager());
                if (argv.length != 1)
                        {
                        System.out.println("usage: java myRMIClient <IP address of host running RMI server>");
                        System.exit(0);
                        }
                String serverName = argv[0];
                try
                        {
                        //bind server object to object in client

                         //myRMIInterface myServerObject = (myRMIInterface) Naming.lookup("rmi://"+serverName+"/myRMIImplInstance");
                        myServerObject = (myRMIInterface) Naming.lookup("rmi://"+serverName+"/myRMIImplInstance");
                                    //rs = myServerObject.getCoursesTaken(id);
                                    //tabbedPane.addTab("View Courses", new JScrollPane(new JTable(new MyTableModel(rs))));
                                    //tb1 = new (myTableModel(rs));


                                    }
                catch(Exception e)
                        {
                        System.out.println("Exception occured: " + e);
                        System.exit(0);
                        }
                System.out.println("RMI connection successful");

    CoursePlanner f = new CoursePlanner(myServerObject);
    f.setGui();
    f.setSize(500, 300);
    f.setVisible(true);
          //if (f.logon()) {
               f.logon();
               f.makeRemainingTabs();
          //}
  }

}