Hello,
Is it possible to have multiple windows when extending JFrame? If so, could you give a quick example?
Many thanks,
Alex.
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.
Hello,
Is it possible to have multiple windows when extending JFrame? If so, could you give a quick example?
Many thanks,
Alex.
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.
RedCloudTsunami (August 5th, 2012)
Many thanks!
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?
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/JFramepublic 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.
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.
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.
It is always a good idea to put other classes in their own .java files. Though this does not mean you need multiple classes...
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 01:27 PM.