Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  [solved]Need to Change Value in Parent class.  (Read 817 times)
0 Members and 1 Guest are viewing this topic.
Offline Cakey

Full Member
**

Posts: 127



« on: 2009-04-02 18:45:48 »

Okay so I have a text field and a GLCanvas which is in my "Parent" class.

In my "game" in the GLCanvas I want to be able to alter the text field in the "Parent" class.

I know I could make a text file and read it or whatever, but that is reallly sloppy, and not Applet friendly. I'm sure this is more of a basic programming question than anything else.

I'm gonna see if there isn't a way I can have the text feild created and handled in the sub-class..

Or do I need to make an Abstract class? blah the lame things I've yet to be taught in CIT130...

Offline Cakey

Full Member
**

Posts: 127



« Reply #1 on: 2009-04-02 19:04:29 »

Nevermind I figured it out.

I just had to get pass the parent class into my subclasses constructor, save it globally to the subclass then call it. Woo!

Offline Hsaka

Jr. Member
**

Posts: 95
Medals: 2



« Reply #2 on: 2009-04-02 19:10:40 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public class Parent {
    private TextField textField = new TextField();

    public TextField getTextField()
    {
        return textField;
    }
}

public class Child extends Parent {

    public Child()
    {
        getTextField().setText("some text");  //modifies the parent's text field
   }
}
Games published by our own members! Go get 'em!
Offline Cakey

Full Member
**

Posts: 127



« Reply #3 on: 2009-04-02 19:16:16 »

Mine looked more like this but thank you for your response!  Smiley
1  
2  
3  
4  
5  
6  
7  
public class parent {
     private String text = "OLD TEXT";

     public void changeText(String newText){
           text = newText;
     }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public class child{
   //TOP
  private parent parentTOP;
   
   public child(parent par) {
       parentTOP = par;
   }

       public somethingHappened(){
            parentTOP.changeText("NEW TEXT");
       }
}


Offline Hsaka

Jr. Member
**

Posts: 95
Medals: 2



« Reply #4 on: 2009-04-02 19:22:19 »

Hey whatever works for you  Wink

Btw is your "child" class a subclass of your "parent" class?
(By that I mean: public class Child extends Parent)

If so, there is no need to pass the parent class as a parameter in your Child constructor.
You can just do something like:

1  
2  
3  
public somethingHappened() {
            changeText("NEW TEXT");
}


since the Child class has access to that method in the Parent class.
Offline Cakey

Full Member
**

Posts: 127



« Reply #5 on: 2009-04-02 19:29:57 »

nope, no inheritance. I worded that poorly.

I used to code in VB and VB .net and they use MDIParents and MDIChilds those are "forms" that are completely unrelated(but may coincide within one another). My lack of Java vocab led me to say something confusing and not what I wanted, as always haha.

But once again thanks. Grin

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.081 seconds with 21 queries.