-4

I would like to have a Java GUI library allowing something like this:

Window window1 = new Window();

window1.show();

Window window2 = new Window();

window2.show();

...

But a JavaFX window needs to be started within its own class extending Application and cannot be instantiated several times.

Why can a JavaFX window not be instanciated like other classes? Why is it called Application and not Window or Frame? Why does JavaFX breaks OOP? Why do we have to use singletons mixed with reflection? I want no magic, I want an OOP-behaving window object appearing on the screen.

Undo
  • 25,519
  • 37
  • 106
  • 129
Nick Müller
  • 61
  • 1
  • 7
  • 1
    use `Stage` for windows – Sergey Grinev Aug 04 '17 at 23:08
  • 4
    I'm voting to close this question as off-topic because the question seems to be of the form "why is this framework like this", which does not appear to be a programming question within the scope of this forum. (In addition, the question is based on assumptions that are clearly false.) – James_D Aug 05 '17 at 01:52

1 Answers1

6

But a JavaFX window needs to be started within its own class extending Application and cannot be instantiated several times.

This statement is wrong, there are restrictions on when you can invoke the constructor on the Window class, but they are not related to extending the Application class.

Why can a JavaFX window not be instanciated like other classes?

The constructor of the Window class is protected, so it can be "used only within the package and in sub-classes outside the package".

Instead, in your application, instantiate a class which is a sub-class of Window. The appropriate class to use is Stage. The constructors of Stage are public rather than protected like Window.

So, to show two stages (which are windows), you can write:

Stage stage1 = new Stage();
stage1.show();

Stage stage2 = new Stage();
stage2.show();

Why is it called Application and not Window or Frame?

An application is called Application because that what it represents. Within an application, you might have multiple Stages (which are instances of Windows), so it would not make sense to call an Application a Window. Anyway, an Application has a different purpose: It's main purpose is to implement the Application lifecycle interface and processing as described in the Application javadoc.

Why does JavaFX breaks OOP?

It doesn't. It uses a protected modifier on Window to enforce encapsulation, which is a key hiding technique of OOP. It provides subclasses of Windows via inheritance for different purposes such as Stages, which are your standard application windows, and PopupWindows, which are specialized window subsets helping with tasks such as creation of popup lists for context menus and combo boxes. It implements Separation of Concerns by differentiating between Application and Window functionality within separate class structures.

Your other questions and statements make no sense to me.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • I think we can add another reason for the existing architecture: it aids in the management of the JavaFX equivalent of the EDT. – Phil Freihofner Aug 05 '17 at 02:46