Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: extends question

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default extends question

    hi, Silly question however. I'm testing a couple of problems, For example disable all components in the swing class that extends JApplet.

    For example this code:

    public class gameLobby extends JApplet
    {
     
    }
     
    public class gameChat extends gameLobby
    {
       // How would you do the following
      this.getParent().setEnabled(false);
     OR 
    this.setEnabled(false);
    }
    note: "this" is refering to the extending class (gameLobby)

    How do you get that Applet from another class and set JApplet.enabled(false)
    not sure if it will set the components disabled, im guessing it will because how can the components be enabled if the parent isnt..

    Thanks guys


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: extends question

    disable all components
    What components are you referring to? Are you asking about components that have been added to a container?
    What does the API doc say for the various methods you are talking about?

    Write a small test program and try some of the techniques to see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: extends question

    First of all, I realize this is nitpicky, but it kind of matters, I highly suggest you follow Java naming conventions. Your classes should start with a capital letter (i.e. GameChat and GameLobby).

    'This' does not refer to the superclass. 'This' refers to the current instance of that object, so in your case the current instance of 'gameChat'.

    Calling getParent() has nothing to do with the object oriented structure of your program. Don't confuse Swing's layout system with your Object hierarchy!

    getParent() returns the parent container of the component. It's probably null for a JApplet.

    You get the Applet from another class simply by passing it in a method or constructor. You can then call any of its methods, including setEnabled(boolean).

    Example 1
    public class SomethingElse {
     
        public SomethingElse(gameLobby lobby) {
            lobby.setEnabled(false);
        }
     
    }

    Example 2
    private static gameLobby inst;
     
    public gameLobby() {
        inst = this;
    }
     
    public static gameLobby getInstance() {
        return inst;
    }

Similar Threads

  1. [SOLVED] how to send variable to different class by using extends
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2012, 05:05 PM
  2. Beginner: Error with 'extends JApplet'
    By chrisob in forum AWT / Java Swing
    Replies: 4
    Last Post: April 17th, 2012, 01:23 PM
  3. Teach me about inheritance and extends?
    By tripline in forum Java Theory & Questions
    Replies: 7
    Last Post: November 26th, 2011, 04:24 PM
  4. Interface and Extends problem
    By DannyGT in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2011, 01:01 PM
  5. keyword Extends
    By chronoz13 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 27th, 2009, 07:30 AM