0

Believe it or not, this question hasn't been asked on SO yet.

What can be considered a View in Java Swing MVC? A GUI component such as JTextField is a view? (It is actually a view-controller right?). Can a view be a combination of other views (a coumpound view)?

Please notice I'm not asking multiple questions, it is all in the umbrella of a single question, "what is a view in Java Swing MVC?".

Piovezan
  • 3,215
  • 1
  • 28
  • 45
  • I would think the view in Swing would the JPanel or similar, where multiple components like JTextField are placed. – pczeus May 10 '20 at 21:49
  • 1
    One of the problems you're facing is this idea that MVC defines distinct and seperate boundaries between it's individual concepts, where in a lot of cases, it doesn't. What I mean by this, is a view can actually act as a controller for child views. So in the case of `JTextField`, it's actually a self contained MVC - it has a view, it has model and it has controller (in the form of a UI delegate) - MVC is more of a concept which promotes "separation of concerns" and code decoupling. *"Can a view be a combination of other views (a coumpound view)?"*, yes, as I already said – MadProgrammer May 11 '20 at 00:20
  • 1
    Also remember, MVC is not a paradigm, it's a concept used to solve a particular problem at the time and was intended to provide a means to reduce complexity and define areas of responsibility - for example, a model should be view agnostic and not dictate how it should be displayed, this is so the view then gains flexibly to display the data in what ever way it needs to based on the context it's been used – MadProgrammer May 11 '20 at 00:29
  • @MadProgrammer I would argue that a JTextField’s model is in fact a separate class, namely `Document`. – VGR May 11 '20 at 01:55
  • 2
    @VGR Yes, you're right, it does, but a controller usually has a reference to the model and view - also, the UI delegate is a seperate class - I'd just say it's a "example" of a concept where a MVC can be self contained - so many people try and then force these components into a strict MVC and complain when it doesn't work - but the way MVC is taught is actually wrong - Swing is a good example of how a MVC concept can be applied reasonably well (it's not without it issues ;)) – MadProgrammer May 11 '20 at 01:58

1 Answers1

1

As per the usual definition of a View for MVC architecture, it is anything capable of interacting with the user on a visual level. Quote from medium.com says the following:

The Controller’s minions, generic reusable interface objects that the Controller is using to do its job. Ultimately, how the model is displayed on screen. Most of the objects in the view (Buttons, Sliders, text)

So essentially, any part of the Swing library which has a Visibility attribute is considered a View. You mentioned VM for JTextField, but it is actually the essential relation between a Controller and a View in this architecture, where you have an empty View, which you populate and present to the user by "throwing" data at it from the Controller.

As for the compound View, I'd say it depends how you look at it. It's essentially a mix of different Views which may or may not present one logical group. But technically speaking, I wouldn't really put those 2 in the same category.

ForWiz
  • 145
  • 1
  • 1
  • 10