Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [solved]Need to Change Value in Parent class.  (Read 1425 times)
0 Members and 1 Guest are viewing this topic.
Offline Cakey

Junior Member





« Posted 2009-04-02 22: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

Junior Member





« Reply #1 - Posted 2009-04-02 23: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
« Reply #2 - Posted 2009-04-02 23: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cakey

Junior Member





« Reply #3 - Posted 2009-04-02 23: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
« Reply #4 - Posted 2009-04-02 23: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

Junior Member





« Reply #5 - Posted 2009-04-02 23: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (12 views)
2014-08-28 18:26:30

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (37 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (29 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (28 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!