Results 1 to 20 of 40
- 06-09-2010, 05:22 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
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.
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.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 problem occurs when I try to make the title of the new GUI (anotherGUI()) the username that was entered.
I get null pointer exceptions.
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.Java Code:public class anotherGUI extends JFrame { Login login; public anotherGUI(){ super(login.getUsername); //some more code. } }
Any information is much appreciated.
Thanks in advance.
- 06-09-2010, 05:35 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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. }
- 06-09-2010, 05:56 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
Thanks. but how do i now declare anotherGUI(Login login) in the Login class? I need the Login class to call anotherGUI also
- 06-09-2010, 06:02 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Well, you already have:
Now this should be, presumably, assigned something in the Login?Java Code:new anotherGUI();
Java Code:anotherGUI = new anotherGUI(this); // "this" means the object the code is in, which I think is the login object?
- 06-09-2010, 06:03 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Just a stylistic note.
Classes start with capital letters, and variables start with lower case.
So that should really be AnotherGUI for the class.
- 06-09-2010, 06:18 PM #6
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
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.
-
Changing your code to reflect these standards will make it easier for others (including us) to understand your code and help you.
Without code and error messages, we're helpless to 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.
- 06-09-2010, 06:33 PM #8
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
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.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(); } } }Last edited by BeeGee; 06-09-2010 at 06:35 PM.
- 06-09-2010, 07:03 PM #9
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Have you tried implementing what was suggested to you in Comment #2 and Comment #4?
- 06-09-2010, 07:09 PM #10
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
I did and the issue now is trying to call anotherGUI() within the Login class.
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.anotherGUI = new anotherGUI(this); // "this" means the object the code is in, which I think is the login object?
This is what I have now.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(); } } }
I apologize for the great inconvenience.Java Code:public class AnotherGUI extends JFrame { Login login; public AnotherGUI(Login login){ super(login.getUsername()); //some more code. } }
- 06-09-2010, 07:12 PM #11
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Take a closer look at Comment #4
- 06-09-2010, 08:31 PM #12
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
I did look at comment 4 but I still don't see what I'm doing wrong. What am i missing?
Please please help!
- 06-09-2010, 10:30 PM #13
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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).
-
Have you tried passing Login.this as the parameter to the AnotherGui constructor?
- 06-10-2010, 09:01 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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:
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.Java Code:anotherGUI() = new AnotherGUI();
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);
- 06-10-2010, 02:01 PM #16
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
Thanks for your help. you are right about this:
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."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.
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
As this references the ButtonHandler method which results in an error.AnotherGUI anotherGUI = AnotherGUI(this);
If this is still confusing, is it okay if I send you my code privately?
Thanks.
- 06-10-2010, 02:14 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 06-10-2010, 02:22 PM #18
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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
where this used to be in you're first postJava Code:new AnotherGUI(this);
Java Code:new AnotherGUI();
- 06-10-2010, 03:33 PM #19
Member
- Join Date
- Apr 2010
- Posts
- 33
- Rep Power
- 0
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 }
- 06-10-2010, 03:41 PM #20
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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)
Similar Threads
-
Calling on a class
By gandalf5166 in forum New To JavaReplies: 12Last Post: 03-24-2010, 10:58 PM -
Child-Class Calling a Method in a Parent-Class
By Blah_ in forum New To JavaReplies: 5Last Post: 09-29-2009, 02:48 AM -
Calling a class method from another class
By caro in forum New To JavaReplies: 4Last Post: 06-10-2009, 01:12 AM -
problem calling function from class to class
By alin_ms in forum New To JavaReplies: 3Last Post: 12-19-2008, 07:35 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks