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 8 of 8

Thread: Extending JFrame + having multiple windows.

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Extending JFrame + having multiple windows.

    Hello,

    Is it possible to have multiple windows when extending JFrame? If so, could you give a quick example?

    Many thanks,

    Alex.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,923
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Extending JFrame + having multiple windows.

    Each instance of the class extending JFrame would have its own window.
    Create a class that extends JFrame, create multiple instances of that class and set them all visible.
    Something like this:
    new ClassThatExtendsJFrame().setVisible(true); // first window
    new ClassThatExtendsJFrame().setVisible(true); // second window
    new ClassThatExtendsJFrame().setVisible(true); // third window
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    RedCloudTsunami (August 5th, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Extending JFrame + having multiple windows.

    Many thanks!

  5. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Extending JFrame + having multiple windows.

    Would it be a good idea to have the other classes in different .java files that all extend JFrame? I'm still learning so it confuses me a lot. So far I have:

    public static void main( String[] args )
    { Window gui = new Window() ; }

    public Window()
    {
    content here
    }

    So if I wanted to call a new window/JFrame where would I go from here?

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,923
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Extending JFrame + having multiple windows.

    Define new classes as needed, not just to have more classes.

    Window is the name of a Java SE class. It will be better if you use a unique name vs one used by Java.

    to call a new window/JFrame
       public static void main( String[] args ) {
           MyWindow gui1 = new MyWindow() ;     // first
           MyWindow gui2 = new MyWindow() ;     // second 
           MyWindow gui3 = new MyWindow() ;     // third
       }
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Extending JFrame + having multiple windows.

    Ahh I see, I understand now. However, that means all three windows will be identical as they'll all be instances of the same constructor method, so I assume that new classes will need to be introduced.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,923
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Extending JFrame + having multiple windows.

    The constructors could be passed args they could use to make unique contents for their windows. Or there could be unique classes for each window. Its up to the programmer.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Extending JFrame + having multiple windows.

    Quote Originally Posted by RedCloudTsunami View Post
    Would it be a good idea to have the other classes in different .java files that all extend JFrame?
    It is always a good idea to put other classes in their own .java files. Though this does not mean you need multiple classes...


    Quote Originally Posted by RedCloudTsunami View Post
    ... However, that means all three windows will be identical as they'll all be instances of the same constructor method, so I assume that new classes will need to be introduced.
    Not necessarily. You can (as Norm stated) send different arguments to the constructor to get unique windows. You can also have multiple constructors in the same class and end up with very different windows with very different functionality.

    Edit: Depending on exactly what is going on, it may be a good idea to end up with different classes. Just because they all extend the same class does not mean you should have just one class. If the windows will be very different for very different uses it may make sense to use multiple classes which all extend the same class, or even extend one or more of your other classes etc. It really gets project specific at this point.
    Last edited by jps; August 5th, 2012 at 02:27 PM.

Similar Threads

  1. Extending class
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 8
    Last Post: January 10th, 2012, 05:06 PM
  2. Assigning a Value to a Class extending a Superclass
    By ubermoe in forum Object Oriented Programming
    Replies: 9
    Last Post: September 22nd, 2011, 12:17 PM
  3. displaying multiple jpanels in a jframe
    By tooktook22 in forum AWT / Java Swing
    Replies: 1
    Last Post: January 19th, 2011, 01:46 PM
  4. class extending
    By Reem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 18th, 2010, 01:49 AM
  5. Overlapping windows? in a Jframe?
    By chronoz13 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 21st, 2009, 12:01 PM