Results 1 to 5 of 5
  1. #1
    snoopy11 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default Calling a method from another class

    hi,

    first time posting so sorry if i have placed this question in the wrong place or anything. im currently working with 2 classes, one class whos job is to display a webpage. the code for which is below:

    Java Code:
    import javax.swing.text.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    
    public class gmailSignup {
    
      public static void gmailSignup(){ //main(String[] args) {
    
         JEditorPane jep = new JEditorPane();
         jep.setEditable(false);
    
         try {
           jep.setPage("http://mail.google.com/mail/signup");
         }
         catch (IOException e) {
           jep.setContentType("text/html");
           jep.setText("<html>Could not load http://mail.google.com/mail/signup </html>");
         }
    
         JScrollPane scrollPane = new JScrollPane(jep);
         JFrame f = new JFrame("Gmail Account Signup");
         // Next line requires Java 1.3
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.getContentPane().add(scrollPane);
         f.setSize(725, 860);
         f.setVisible(true);
    
      }
    
    }

    i have borrowed this code from somewhere else and adapted it to my purposes as i had much difficulty in doing it on my own. now this works when i run it on its own as a main method (which you can see ive just commented out for testing purposes). the difficulty i have is when trying to display it when calling it from my other class. my other class is simply a gui that has a number of buttons on it, what im trying to do is when one of these buttons is pressed the jframe from the class above opens up and displays the webpage. ive been trying to do this with the code below which is in the ActionEvent for the button:

    Java Code:
    new gmailSignup().setVisible(true);

    this is giving me a "cannot find symbol" compile time error and im not really sure what ive done wrong so any help or directions that you can give me would be greatly appreciated.

    thanks

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by snoopy11 View Post
    Java Code:
    new gmailSignup().setVisible(true);
    I don't see a 'setVisible( ... )' method definition in your gmailSignup class and neither did the compiler so it started whining.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    snoopy11 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Ok Jos, thanks for the quick response.

    should i instead be trying to call:

    Java Code:
    f.setVisible(true);

    as that does have a setVisible method and the jframe is what im ultimately trying to display. if so would you be able to show me how to go about doing that? as when i try i continue to get errors, primarily ones saying "package gmailSignup does not exist"

    thanks again,
    stephen

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default

    You can not. You can do something like
    Java Code:
    gmailSignup g = new gmailSignup();
    g.f.setVisible(true); // i'm not even sure if this could work

    But this is not the way you should think. You have to make your code more java.

    What you should do is to make next structure:

    Java Code:
    class declaration{
    
        components declatations
        ...
        
        constructor declaration{
            init method call
        }
    
        init method declaration{
            do whole job here:
                set your components here
                show your frame on the screen
                and you can load your page here
        }
    }

    Now when you call constructor new gmailSignup() from other class, it will run init method and init method will do all the job.
    This is some example, this task could be done in other ways of course but i think this is the easiest one

  5. #5
    snoopy11 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 04:01 PM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  3. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  4. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  5. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 06:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •