?
Solved

How to capture input from a swing component.

Posted on 2005-03-14
10
Medium Priority
?
331 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:_Esam
  • 6
  • 4
10 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 13533329
>> 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.
0
 

Author Comment

by:_Esam
ID: 13533458
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

0
 
LVL 30

Accepted Solution

by:
Mayank S earned 200 total points
ID: 13533506
>> double password = Double.parseDouble(passwordField.getText());

Why is password a double? It should ideally be a String.

String password = new String ( passwordField.getPassword () ) ;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 13533526
>> int id = Int.parseInt(ustidField.getText());

That would be: int id = Integer.parseInt ( ustidField.getText () ) ;
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13533537
>> 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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13533538
BTW, why is user-ID an 'int'?
0
 

Author Comment

by:_Esam
ID: 13533591
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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13533639
>> 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?
0
 

Author Comment

by:_Esam
ID: 13533640
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
0
 

Author Comment

by:_Esam
ID: 13533659
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();
          //}
  }

}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question