Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1248
  • Last Modified:

How to refresh Swing JTabs : JAVA, RMI, SWING

Hi,
I have a client side Swing JTabs.
Once the user is verified in the logOn tab, the other two tabs are created, the makeRemainingTabs() method.

Problem is: If another user tries to log on, the other tabs are recreated with the old user id.
This probably is due to how I made the logic:

In logOn  ()

if ( userVerified == true) {
   makeRemainingTabs();
}


So, once a user is verified and logs in, other tabs are shown.
Now, if another user tried to log on, other tabs are recreated with an old id.

I would like other tabs to be non-visible, if another user tries to log in, i.e. clicks on the log on tab.

Not sure exactly how to do that..

Let me know..

Here is the complete code:

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 CoursePlannerV1 extends JFrame{
      private JTabbedPane tabbedPane;
    private static myRMIInterface myServerObject = null;
    private boolean isVerified;
    private int id = 0;
    private String password = null;
    private JTextField idField;
    private JPasswordField passwordField;
    private boolean userVerified;

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


        private void logOn() {
    try {
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          tabbedPane = new JTabbedPane();

            JPanel logonPanel = new JPanel();

            logonPanel.setLayout( null );

            JLabel label1 = new JLabel( "UST ID:" );
            label1.setBounds( 10, 15, 150, 20 );
            logonPanel.add( label1 );

            //TextField
            idField = new JTextField();
            idField.setBounds( 10, 35, 150, 20 );
            logonPanel.add( idField );

            JLabel label2 = new JLabel( "Password:" );
            label2.setBounds( 10, 60, 150, 20 );
            logonPanel.add( label2 );

            //JPasswordField
            passwordField = new JPasswordField();
            passwordField.setBounds( 10, 80, 150, 20 );
            logonPanel.add( passwordField );

            JButton submitButton = new JButton("Log On");
            submitButton.setBounds(80, 120, 80, 20);
            logonPanel.add(submitButton);

            submitButton.addActionListener (new ActionListener(){
                        public void actionPerformed(ActionEvent event)
                                      {



///////////***********************i get the id from this idField
                                            try{
                                            id = Integer.parseInt(idField.getText());
                                            final String password = new String (passwordField.getPassword());
                                            System.out.println("ID = " + id);

                                                  try
                                                {
                                                    //verify user with a remote verification method
                                                      boolean userVerified = myServerObject.verify(id, password);
                                          System.out.println("Verification is " + userVerified); //print to see if verification is true
///////if verified, make other tabs.
                                                      if (userVerified == true) {
                                                            makeRemainingTabs();
                                                      }

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

                                            catch(Exception e){
                                          e.printStackTrace();

                                          }
                                    }
                              }
                              );


            tabbedPane.addTab("Log On", logonPanel);
            getContentPane().add(tabbedPane);




      }

      catch (Exception e) {
            e.printStackTrace();
      }
        }//end of logOn

///////////////////i make the other tabs using the idField value from logOn()

   private void makeRemainingTabs(){

            try{
                  tabbedPane.addTab("View Courses", new JScrollPane(new JTable(new MyTableModel(myServerObject.getCoursesTaken(Integer.parseInt(idField.getText()))))));
          }
          catch (Exception e){
                  e.printStackTrace();
          }


          try{
                  tabbedPane.addTab("View Requirements", new JScrollPane(new JTable(new MyTableModel(myServerObject.getRequiredCourses(Integer.parseInt(idField.getText()))))));
             }
             catch (Exception e){
                  e.printStackTrace();
            }

     }





      public static void main(String[] argv){
            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
                          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");

          CoursePlannerV1 cpv1 = new CoursePlannerV1(myServerObject);

          cpv1.logOn();
          cpv1.setSize(500, 300);
          cpv1.setVisible(true);
    }
}











Thanks.

_Esam

0
_Esam
Asked:
_Esam
  • 4
  • 2
1 Solution
 
TrigunaCommented:
before adding elements to panel,

do this, panel.removeAllElements() and panel.setVisible(false);

then panel.add(tab); and panel.setVisible(true);
0
 
_EsamAuthor Commented:
I am not sure if I understand...
I am adding the tabs in the container..not the panels ,,, right?

I pretty much showed the code above....

Could you please indicate how i can rearrange the code to reflect your suggestion?

Thanks.
_Esam

0
 
TrigunaCommented:
tabbedPane.addChangeListener(new ChangeListener(){

   public void stateChanged(ChangeEvent ce){
    if(tabbedPane.selectedIndex==0)
    {
           //I thought it as a log on window.
           new LogOn();
    }
    }
});

add the above code, after adding the tab to the frame.

After this,

create a class named,

public class LogOn extends JWindow
{
        public LogOn()
        {
              //Add your tools here

             //On Close, make setVisible(false);

              setBounds(100,200,300,400);
              setVisible(true);
        }
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
_EsamAuthor Commented:
 >>         //I thought it as a log on window.

I gave the code ...

_Esam
0
 
TrigunaCommented:
Have you seen my comment
0
 
TrigunaCommented:
tabbedPane.addChangeListener(new ChangeListener(){

   public void stateChanged(ChangeEvent ce){
    if(tabbedPane.selectedIndex==0)
    {
           //This is your login window.
           new LogOn();
    }
    }
});

add the above code, after adding the tab to the frame.

After this,

create a class named,

public class LogOn extends JWindow
{
        public LogOn()
        {
              //Add your tools here

             //On Close, make setVisible(false);

              setBounds(100,200,300,400);
              setVisible(true);
        }
}
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now