Page 1 of 2 12 LastLast
Results 1 to 20 of 40
  1. #1
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default Calling a class within another class

    Hello,

    I have two classes: A and B. Is it possible for class A to call Class B...and then have Class B call some function in Class A.

    I apologize for not providing the full code. This is due to the fact that I've posted questions with full code and my professor got upset with me as many of my classmates lifted them off the site without my knowledge.


    Java Code:
    public class Login extends JFrame{
     
    public Login(){
    
    I have some code here that sets up a gui which has labels and textfields
    }
    
    //gets the username entered
     public String getUserName() {
             return nametextField.getText();
    	 }
    private class ButtonHandler implements ActionListener {
    		// handle button event
    	 public void actionPerformed (ActionEvent event){
    
    String username = getUserName(); //returns the text contained in the  Text Component.
    
       new anotherGUI();
      }
    }
    }
    The above code works fine. A GUI which allows the user to login by entering username is created. Once the username is entered, another GUI is opened.
    The problem occurs when I try to make the title of the new GUI (anotherGUI()) the username that was entered.
    I get null pointer exceptions.

    Java Code:
    
    public class anotherGUI extends JFrame {
    
    Login login;
    
    public anotherGUI(){
    
    super(login.getUsername);
    
    //some more code.
    }
    
    }
    I hope this is not confusing. I am just trying to set the title of my new GUI to the username that was entered in the previous GUI.

    Any information is much appreciated.

    Thanks in advance.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Pass the Login into the newGUI constructor as a parameter:
    Java Code:
    public anotherGUI(Login login) {
        super(login.blahblah());
        this.login = login;  // assuming you actually need the login here for something else.
    }

  3. #3
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    Thanks. but how do i now declare anotherGUI(Login login) in the Login class? I need the Login class to call anotherGUI also

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Well, you already have:
    Java Code:
    new anotherGUI();
    Now this should be, presumably, assigned something in the Login?

    Java Code:
    anotherGUI = new anotherGUI(this);  // "this" means the object the code is in, which I think is the login object?

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Just a stylistic note.
    Classes start with capital letters, and variables start with lower case.
    So that should really be AnotherGUI for the class.

  6. #6
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    Thanks for the stylistic note...that's not actually how they are in my code but thanks for pointing that out.

    however, the code is not in the login object. it's in a button event as noted above. So when a button is pressed, anotherGui is created. I am still getting errors.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by BeeGee View Post
    Thanks for the stylistic note...that's not actually how they are in my code but thanks for pointing that out.
    Changing your code to reflect these standards will make it easier for others (including us) to understand your code and help you.

    however, the code is not in the login object. it's in a button event as noted above. So when a button is pressed, anotherGui is created. I am still getting errors.
    Without code and error messages, we're helpless to help you.

  8. #8
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    Java Code:
    public class Login extends JFrame{
     
    public Login(){
    
    I have some code here that sets up a gui which has labels and textfields
    }
    
    //gets the username entered
     public String getUserName() {
             return nametextField.getText();
    	 }
    
    private class ButtonHandler implements ActionListener {
    		// handle button event
    	 public void actionPerformed (ActionEvent event){
    
    String username = getUserName(); //returns the text contained in the  Text Component.
    
       new anotherGUI();
      }
    }
    }
    Thanks for your help. However I stated my reasons for not posting the code. My professor got upset with me because in the past, some of my classmates took my code off this site.. If this is still an issue, I will post the code.
    Last edited by BeeGee; 06-09-2010 at 06:35 PM.

  9. #9
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Have you tried implementing what was suggested to you in Comment #2 and Comment #4?

  10. #10
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    I did and the issue now is trying to call anotherGUI() within the Login class.

    anotherGUI = new anotherGUI(this); // "this" means the object the code is in, which I think is the login object?
    The problem is that I have anotherGUI = new anotherGUI(this) in the ButtonHandler function as this event is meant to occur when a button is pressed.

    Java Code:
    public class Login extends JFrame{
     
    public Login(){
    
    I have some code here that sets up a gui which has labels and textfields
    }
    
    //gets the username entered
     public String getUserName() {
             return nametextField.getText();
    	 }
    
    private class ButtonHandler implements ActionListener {
    		// handle button event
    	 public void actionPerformed (ActionEvent event){
    
    String username = getUserName(); //returns the text contained in the  Text Component.
    
       anotherGUI() = new AnotherGUI();
      }
    }
    }
    This is what I have now.

    Java Code:
    public class AnotherGUI extends JFrame {
    
    Login login;
    
    public AnotherGUI(Login login){
    
    super(login.getUsername());
    
    //some more code.
    }
    
    }
    I apologize for the great inconvenience.

  11. #11
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Take a closer look at Comment #4

  12. #12
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    I did look at comment 4 but I still don't see what I'm doing wrong. What am i missing?
    Please please help!

  13. #13
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Well you changed the constructor for the AnotherGUI class, maybe try changing the constructor call to make the AnotherGUI object?? Just a thought...

    You don't even need to assign it to a variable if it was working before(Even though this is usually a good idea).

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  15. #15
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    I think I might have misinterpreted this:
    "I need the Login class to call anotherGUI also "
    as meaning that the Login class needed to hold a copy of the GUI class, so each could reference the other.

    If you meant (as it looks like) that you simply wanted to create one inorder to display it, and that Login does not need to hold a copy, then we can correct this line:
    Java Code:
    anotherGUI() = new AnotherGUI();
    Now note that I really don't know what you are doing, so some of this is simply guesswork based on what you've said.

    I originally though Login would hold an attribute called anotherGUI which would be the AnotherGUI we are creating on this line. If that's not necessary then we can fix this.

    First off you are calling the constructor for AnotherGUI, which requires a Login as a parameter. So you need to pass the Login in, as mentioned...which would be "this".

    Second ditch the first set of brackets, which implies that anotherGUI is a method, which it isn't supposed to be. So you get:
    Java Code:
    AnotherGUI anotherGUI = AnotherGUI(this);

  16. #16
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    Thanks for your help. you are right about this:

    "I need the Login class to call anotherGUI also "
    as meaning that the Login class needed to hold a copy of the GUI class, so each could reference the other.
    However, I want the AnotherGUI() to show up as a result of a button pressed. That's why AnotherGUI() was originally called in the ButtonHandler method.
    With what you gave me, the Login interface and AnotherGUI() interface load up simultaneously.

    The Login interface is meant to show up first. And then upon the user entering the username and clicking a button, AnotherGUI shows up with the username as the title of AnotherGUI().

    I'm not getting that when I do this
    AnotherGUI anotherGUI = AnotherGUI(this);
    As this references the ButtonHandler method which results in an error.

    If this is still confusing, is it okay if I send you my code privately?

    Thanks.

  17. #17
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Then might i suggest having a controller class "above" the Login class that controls which of the Login and AnotherGUI are open?

    Login really shouldn't need to know anything about AnotherGui.
    It should merely hold a reference to the controller, which it then informs of a successful logon.

    Then that controller controls which of the two frames to display.

  18. #18
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    I'm just going to spell it out for you, Just put the constructor call to AnotherGUI inside the actionPerformed() as you were doing before.

    so put this
    Java Code:
    new AnotherGUI(this);
    where this used to be in you're first post
    Java Code:
    new AnotherGUI();

  19. #19
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    Thanks. I tried your method, Stormy Waters, and I get errors.
    I have posted most of my code below...may be this would help.
    I apologize for the trouble.

    Java Code:
    package one;
    
    import javax.swing.*;
    
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import java.awt.*;
    
    
    public class Login extends JFrame {
    	
    	 private JLabel nameLabel, noteLabel, note2Label;
    	 public JTextField nametextField;
    	 private JButton loginButton;
    	public AnotherGUI anothergui;
    	 
    	 
    	 public Login(){
    		 super ("Log In");
    		 Container contentPane = getContentPane();
    		 SpringLayout layout = new SpringLayout();
             contentPane.setLayout(layout);
             
             nameLabel = new JLabel ("Username:");
             nameLabel.setFont(new Font("Monospaced", Font.BOLD, 16));
             contentPane.add(nameLabel);
             noteLabel = new JLabel("Note: "");
             noteLabel.setFont(new Font("serif", Font.PLAIN, 14));
             noteLabel.setForeground(Color.red);
             note2Label = new JLabel("e.g. ");
             note2Label.setFont(new Font("serif", Font.PLAIN, 14));
             note2Label.setForeground(Color.red);
             contentPane.add(noteLabel);
             contentPane.add(note2Label);
             nametextField = new JTextField (18);
             nametextField.setFont(new Font("Monospaced", Font.PLAIN, 16));
             contentPane.add(nametextField);
             loginButton = new JButton("Log In");
             int width = 5, height = 5;
             loginButton.setSize(width, height);
             contentPane.add(loginButton);
             
             // create new ButtonHandler for button event handling
             ButtonHandler handler = new ButtonHandler();
             loginButton.addActionListener(handler);
             nametextField.addActionListener(handler);
            
             
             layout.putConstraint(SpringLayout.WEST, noteLabel, 5,
                     SpringLayout.WEST, contentPane);
             layout.putConstraint(SpringLayout.NORTH, noteLabel, 5,
                     SpringLayout.NORTH, contentPane);   
             layout.putConstraint(SpringLayout.WEST, note2Label, 7, SpringLayout.WEST, contentPane);
             layout.putConstraint(SpringLayout.NORTH, note2Label, 15, SpringLayout.NORTH, noteLabel);
             
             //makes the label's left edge 5 pixels away from the container's left edge
             layout.putConstraint(SpringLayout.WEST, nameLabel, 20,
                     SpringLayout.WEST, contentPane);
             layout.putConstraint(SpringLayout.NORTH, nameLabel, 28,
                     SpringLayout.NORTH, note2Label);
             layout.putConstraint(SpringLayout.NORTH, nametextField, 30,
                     SpringLayout.NORTH, note2Label);
             layout.putConstraint(SpringLayout.WEST, nametextField, 5,
                     SpringLayout.EAST, nameLabel);
             
             layout.putConstraint(SpringLayout.NORTH, loginButton, 15,
                     SpringLayout.SOUTH, nametextField);
             layout.putConstraint(SpringLayout.WEST, loginButton, 150,
                     SpringLayout.WEST, contentPane);
             layout.putConstraint(SpringLayout.EAST, contentPane,
                     5,
                     SpringLayout.EAST, noteLabel);
             layout.putConstraint(SpringLayout.SOUTH, contentPane,
                     5,
                     SpringLayout.SOUTH, loginButton);
             
         	//gridform = new Grid_Form(this);
             
    	 }
    	 
    	 public String getUserName() {
             return nametextField.getText();
    	 }
    	 
    
    	 
    	// inner class for button event handling
    	 private class ButtonHandler implements ActionListener {
    		// handle button event
    		 public void actionPerformed (ActionEvent event){
    			 
    			String username = getUserName(); //returns the text contained in the Text Component.
    			 
    			 if (username.matches("[A-Z]\\s+[A-Z][A-za-z]+")){
    				 System.out.println("Username correct");
    				 setVisible(false);
    				 
    				 new Grid_Form(this);
    				 
    			
    			 }
    			 else {
    				 JOptionPane.showMessageDialog(null,
    		        		    "Name Should Be in the Format :",
    		        		    "Username Error",
    		        		    JOptionPane.ERROR_MESSAGE);
    				 nametextField.setText("");
    				
    			 } 
    			 
    		 }
    	 }
    	 
    	
    }
    Java Code:
    package one;
    
    import java.awt.*;
    
    
    import javax.swing.*;
    
    public class AnotherGUI extends JFrame {
    	
    	private JLabel nameLabel;
    	
    	private JLabel genderLabel;
    	
    	private JTextField nametextField;
    	
    	private JTextField gendertextField;
    	
    	
    	public Grid_Form(Login login){
    		super(login.getUserName());
    		Container pane = getContentPane();
    		pane.setLayout(new FlowLayout());
    		
    		
    		Font monospacedFont = new Font("Monospaced", Font.PLAIN, 14);
    		
    		//Instantiates Labels, TextFields and Buttons
    		nameLabel = new JLabel("Name: ");
    		nameLabel.setFont(monospacedFont);
    		nametextField = new JTextField(2);
    		
    		genderLabel = new JLabel ("Gender: ");
    		genderLabel.setFont(monospacedFont);
    		gendertextField = new JTextField(6);
    		
    		
    		
    		//Adds the labels, textFields, and buttons to the pane
    		pane.add(nameLabel);
    		pane.add(nametextField);
    		
    		pane.add(genderLabel);
    		pane.add(gendertextField);
    		
    		pack();
    		setVisible(true);
    	}
    
    }
    Java Code:
    package one;
    
    import javax.swing.JFrame;
    
    public class Login_Test {
    
    	public static void main( String args[] )
    	   { 
    	      //Schedule a job for the event-dispatching thread:
    	        //creating and showing this application's GUI.
    	        javax.swing.SwingUtilities.invokeLater(new Runnable() {
    	            public void run() {
    	            	//JFrame.setDefaultLookAndFeelDecorated(true);
    	            	Login log = new Init_Login(); 
    	            	log.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	                log.setResizable(false);
    	       	      //log.setSize( 325, 100 ); // set frame size
    	       	        log.pack();
    	       	        log.setVisible( true ); // display frame
    	            }
    	        });
    	        
    	   } // end main
    	
    	
    }

  20. #20
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Constructors must have the same name as the class, either change the AnotherGUI class to Grid_Form class or rename the constructor to be AnotherGUI(Login login).

    Here's a tutorial, this is basic stuff.
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Page 1 of 2 12 LastLast

Similar Threads

  1. Calling on a class
    By gandalf5166 in forum New To Java
    Replies: 12
    Last Post: 03-24-2010, 10:58 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. Calling a class method from another class
    By caro in forum New To Java
    Replies: 4
    Last Post: 06-10-2009, 01:12 AM
  4. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 07:35 PM
  5. 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

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
  •