_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(J Frame.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(tabbe dPane);
}
catch (Exception e) {
e.printStackTrace();
}
}
private boolean logon() {
return "secret".equals(JOptionPan e.showInpu tDialog("E nter password"));
}
private void makeRemainingTabs(){//Vect or strs) throws RemoteException {
ttry{
tabbedPane.addTab("View Courses", new JScrollPane(new JTable(new MyTableModel(myServerObjec t.getCours esTaken(id )))));
}
catch (Exception e){
}
try{
tabbedPane.addTab("View Requirements", new JScrollPane(new JTable(new MyTableModel(myServerObjec t.getRequi redCourses (id)))));
}
catch (Exception e){}
}
In my main method:
CoursePlanner f = new CoursePlanner(myServerObje ct);
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(ustid Panel);
getContentPane().add(passw ordPanel);
//add(logonButton);
// Display panels and button within border
Container c = getContentPane();
getContentPane().setLayout (new BorderLayout());
getContentPane().add(ustid Panel, BorderLayout.NORTH);
getContentPane().add(passw ordPanel, BorderLayout.CENTER);
//getContentPane().add(log onButton, BorderLayout.CENTER);//
getContentPane().setLayout (new FlowLayout(FlowLayout.RIGH T));
getContentPane().add(logon Button);
// 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(ActionEven t event)
{
double id = Double.parseDouble(ustidFi eld.getTex t());
double password = Double.parseDouble(passwor dField.get Text());
}
}
ActionListener listener = new LogonListener();
logonButton.addActionListe ner(listen er);
}
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
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(J
tabbedPane = new JTabbedPane();
JPanel p = new JPanel();
p.add(new JLabel("Some initial message to show"));
tabbedPane.addTab("UST Course Planner", p);
getContentPane().add(tabbe
}
catch (Exception e) {
e.printStackTrace();
}
}
private boolean logon() {
return "secret".equals(JOptionPan
}
private void makeRemainingTabs(){//Vect
ttry{
tabbedPane.addTab("View Courses", new JScrollPane(new JTable(new MyTableModel(myServerObjec
}
catch (Exception e){
}
try{
tabbedPane.addTab("View Requirements", new JScrollPane(new JTable(new MyTableModel(myServerObjec
}
catch (Exception e){}
}
In my main method:
CoursePlanner f = new CoursePlanner(myServerObje
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
getContentPane().add(ustid
getContentPane().add(passw
//add(logonButton);
// Display panels and button within border
Container c = getContentPane();
getContentPane().setLayout
getContentPane().add(ustid
getContentPane().add(passw
//getContentPane().add(log
getContentPane().setLayout
getContentPane().add(logon
// 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(ActionEven
{
double id = Double.parseDouble(ustidFi
double password = Double.parseDouble(passwor
}
}
ActionListener listener = new LogonListener();
logonButton.addActionListe
}
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
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(ActionEven t event)
{
double id = Double.parseDouble(ustidFi eld.getTex t());
double password = Double.parseDouble(passwor dField.get Text());
}
}
ActionListener listener = new LogonListener();
logonButton.addActionListe ner(listen er);
the remote method has the signature:
boolean verify(int, String)
So, I will need to change these two:
double id = Double.parseDouble(ustidFi eld.getTex t());
double password = Double.parseDouble(passwor dField.get Text());
to:
int id = Int.parseInt(ustidField.ge tText());
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
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(ActionEven
{
double id = Double.parseDouble(ustidFi
double password = Double.parseDouble(passwor
}
}
ActionListener listener = new LogonListener();
logonButton.addActionListe
the remote method has the signature:
boolean verify(int, String)
So, I will need to change these two:
double id = Double.parseDouble(ustidFi
double password = Double.parseDouble(passwor
to:
int id = Int.parseInt(ustidField.ge
String password = (passwordField.getText());
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> int id = Int.parseInt(ustidField.ge tText());
That would be: int id = Integer.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.
>> 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'?
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){thisclasso bject = 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>thisclassobj ect
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
I have it as:
class CoursePlanner {
int stid,
String stpwd;
thisclassobject;
public CoursePlanner(MyObject myserverobject){thisclasso
void logon() {
new Logon1();
}
}
I have to get it from Logon1() in logon() method.>>Logon1 need access to a member variable defined in CoursePlanner>thisclassobj
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?
Is this the remote class? Could you clarify as to what is the remote class and what is the class at the UI?
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
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
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(myRMIInterfa ce 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://"+ser verName+"/ myRMIImplI nstance");
}
catch(Exception e)
{
System.out.println("Except ion occured: " + e);
System.exit(0);
}
System.out.println("RMI connection successful");
//}
//catch (Exception ex)
//{
//System.out.println("Exce ption occured: " + ex);*/
//}
///////private int id = 1204;
//private static String pass = "nyymo";
//private static Vector superClasses;
private void setGui() {
try {
setDefaultCloseOperation(J Frame.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(tabbe dPane);
}
catch (Exception e) {
e.printStackTrace();
}
}
private void logon() {
new Logon1();
//return "secret".equals(JOptionPan e.showInpu tDialog("E nter password"));
}
private void makeRemainingTabs(){//Vect or strs) throws RemoteException {
//int stid = 0;
//this.id = stid;
//this.rs = strs;
//rs = myServerObject.getCoursesT aken(id);
try{
tabbedPane.addTab("View Courses", new JScrollPane(new JTable(new MyTableModel(myServerObjec t.getCours esTaken(id )))));
//tabbedPane.addTab("View Courses", new JScrollPane(tb1));
}
catch (Exception e){
}
try{
tabbedPane.addTab("View Requirements", new JScrollPane(new JTable(new MyTableModel(myServerObjec t.getRequi redCourses (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://"+ser verName+"/ myRMIImplI nstance");
myServerObject = (myRMIInterface) Naming.lookup("rmi://"+ser verName+"/ myRMIImplI nstance");
//rs = myServerObject.getCoursesT aken(id);
//tabbedPane.addTab("View Courses", new JScrollPane(new JTable(new MyTableModel(rs))));
//tb1 = new (myTableModel(rs));
}
catch(Exception e)
{
System.out.println("Except ion occured: " + e);
System.exit(0);
}
System.out.println("RMI connection successful");
CoursePlanner f = new CoursePlanner(myServerObje ct);
f.setGui();
f.setSize(500, 300);
f.setVisible(true);
//if (f.logon()) {
f.logon();
f.makeRemainingTabs();
//}
}
}
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(myRMIInterfa
myServerObject = server;}
/* System.setSecurityManager(
///if (argv.length != 1)
// {
// System.out.println("usage:
// System.exit(0);
//}
//String serverName = argv[0];
try
{
//bind server object to object in client
myRMIInterface myServerObject = (myRMIInterface) Naming.lookup("rmi://"+ser
}
catch(Exception e)
{
System.out.println("Except
System.exit(0);
}
System.out.println("RMI connection successful");
//}
//catch (Exception ex)
//{
//System.out.println("Exce
//}
///////private int id = 1204;
//private static String pass = "nyymo";
//private static Vector superClasses;
private void setGui() {
try {
setDefaultCloseOperation(J
tabbedPane = new JTabbedPane();
JPanel p = new JPanel();
p.add(new JLabel("Some initial message to show"));
tabbedPane.addTab("UST Course Planner", p);
getContentPane().add(tabbe
}
catch (Exception e) {
e.printStackTrace();
}
}
private void logon() {
new Logon1();
//return "secret".equals(JOptionPan
}
private void makeRemainingTabs(){//Vect
//int stid = 0;
//this.id = stid;
//this.rs = strs;
//rs = myServerObject.getCoursesT
try{
tabbedPane.addTab("View Courses", new JScrollPane(new JTable(new MyTableModel(myServerObjec
//tabbedPane.addTab("View Courses", new JScrollPane(tb1));
}
catch (Exception e){
}
try{
tabbedPane.addTab("View Requirements", new JScrollPane(new JTable(new MyTableModel(myServerObjec
}
catch (Exception e){}
}
public static void main(String[] argv) //throws RemoteExcecpion
{
System.setSecurityManager(
if (argv.length != 1)
{
System.out.println("usage:
System.exit(0);
}
String serverName = argv[0];
try
{
//bind server object to object in client
//myRMIInterface myServerObject = (myRMIInterface) Naming.lookup("rmi://"+ser
myServerObject = (myRMIInterface) Naming.lookup("rmi://"+ser
//rs = myServerObject.getCoursesT
//tabbedPane.addTab("View Courses", new JScrollPane(new JTable(new MyTableModel(rs))));
//tb1 = new (myTableModel(rs));
}
catch(Exception e)
{
System.out.println("Except
System.exit(0);
}
System.out.println("RMI connection successful");
CoursePlanner f = new CoursePlanner(myServerObje
f.setGui();
f.setSize(500, 300);
f.setVisible(true);
//if (f.logon()) {
f.logon();
f.makeRemainingTabs();
//}
}
}
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.