3 Replies Latest reply: Jan 19, 2004 8:19 AM by 807582 RSS

    Calling another class

    807582
      I have a problem of calling another class from one class.How can i call another class from one
      my class1 format is, both are frame class, Second class has the same format

      class cl extends JFrame implements ActionListener
      {
      public cl()
      {
      // initialization
      }
      public void action performed(...........)

      if ( e.getSource()== button1)
      {
      // Here I want to call the second class class2. How?
      }
      }
      }
      class class1
      {
      public static void main(String arg[])
      {
      new cl();
      }
      }
        • 1. Re: Calling another class
          807582
          public void actionPerformed(ActionEvent ae) {
            if(ae.getSource() == button1) {
              Class2 the_other_class_object = new Class2();
              ...
            }
          }
          if you want the object of Class2 to be available in the whole class (Class1) you have to define a globale variable:
          public class Class1 extends JFrame implements ActionListener {
            private Class2 class2_object;
           
            public Class1() {
              ...
            }
           
            public void actionPerformed(ActionEvent ae) {
              if(ae.getSource() == button1) {
                class2_object = new Class2();
              }
            }
          }
          • 2. Re: Calling another class
            807582
            my program is a chat model, i created two frames ,one for user login (that is only two textfields for userid
            and password) and when the username and password satisfies with the data stored in the database(MSaccess) i want to call the second frame.

            My programming model is

            class a extends JFrame implements ActionListener
            {
            public a() // constructor to initialize .
            {

            }
            public void actionPerformed(ActionEvent e)
            {
            if (e.getSource==Button1)
            {

            // Here i want to call the second frame with few Buttons and text area
            // How to call? Second frame also has the same format as the first
            }
            }
            }
            public class Login
            {
            public static void main(String arg[])
            {
            new a();
            }
            }
            • 3. Re: Calling another class
              807582
              1st: please have a look to the forum help - how to use formattings (http://forum.java.sun.com/faq.jsp#format) - this makes it much easier to read your code...

              2nd: your prob:
              A) your solution:
              class A extends JFrame implements ActionListener {
                public A() {
                }
                public void actionPerformed(ActionEvent e) {
                  if(e.getSource == button1) {
                    B b = new B();
                    // maybe close the current frame
                  }
                }
              }
              B) another solution:
              class MyChat extends JFrame implements ActionListener {
               
                private LoginDialog login = new LoginDialog();
               
                public MyChat() {
                  // construct the main frame (chat application)
                  ...
                  this.setVisible(true);
                  // popup a JDialog for login
                  int result = login.showLoginDialog(this);
                  if(result != LoginDialog.APPROVE_OPTION || !login.checkLogin()) {
                    // user didn't press the approve button OR the login is incorrect
                    System.exit(1);
                  }
                }
               
                // insert your chatting code here
                // ...
               
                public static void main(String[] args) {
                  MyChat chat = new MyChat();
                }
              }
               
               
              public class LoginDialog implements ActionListener {
               
                private final static int ANY_OTHER_OPTION = 0;
                private final static int APPROVE_OPTION = 1;
               
                private JTextField user_name = new JTextField();
                private JPasswordField password = new JPasswordField();
              
                private JDialog login;
               
                public LoginDialog() {
                  // ...
                }
               
                public boolean checkLogin() {
                  String user = user_name.getText();
                  String pwd = password.getText();
                  // check the database if there is a user named "user" with password "pwd"
                  if(...) { // user exists
                    return true;
                  }
                  else {
                    return false;
                  }
                }
               
                public int showLoginDialog(JFrame parent) {
                  login = new JDialog(parent, "Login", true);
                  // initiate the login gui
                  // make sure: only if user presses the OK or Login button -> return APPROVE_OPTION
                  // else: return ANY_OTHER_OPTION (or however you want to call this)
               
                  // you'll have to add a WindowListener to return ANY_OTHER_OPTION if the window is closed
                  return X;
                }
              }
              have fun... ;-)