Model View Controller With S W I N G And J F X - Part 1

Joe

Thành viên VIP
21/1/13
2,938
1,305
113
Hi

In general Model-View-Controller (MVC for short) is a framework that allows developers to design and test their GUI applications in a fast manner:

- Model is designed by description in plain text which is usually in XML (eXtensible Markup Language) format. The reason that XML is chosen is its syntactical simplicity.
- View is the visual form how the model looks like in reality. The "Viewer" is usually a small app that loads the model into a Modelling Mechanism which then produces (on the fly) the executable codes on a computer.
- Controller is, as its name says, a controlling mechanism which triggers actions or events of expected processes and to verify how the processes function. Controller is a module that is usually loaded by the Viewer or by the Model-Loader. Usually actions and events are "injected" into some Model GUI-Components for some special purpose. The actions or events are triggered by the users.


(source: Medium.com)

The Model GUI-Components are in general the buttons, input fields, mouse/hand movements, keystrokes, etc. The injected actions or events are reactions on the hitting of the keys, the mouse click or the exerting of the buttons. MVC GUI Components are dependent on such "injection". Hence it's also called Dependency Injection.

Before I discuss a bit more in details with you about MVC. I have some words about pure JAVA SWING MVC which is developed by myself (click HERE. The poll showed only one member who seems to have interst on this issue. The poll was closed on Nov. 24 2019.

JAVA offers its users TWO kinds of MVC GUI Components: Java FX (or simply JFX) and Java SWING (or simply SWING). Only JFX is developed fully as a MVC framework while SWING is left aside and the non-action is exploited by software companies which fill the gap with their own products -the framework. Spring Boot is, for example, the most noted product in the JAVA community. You can follow the [series Spring Boot] from CongdongJava posted by Admin quydtkt.

Nevertheless, MVC is a very complex technology and complexity is usually confusing for the newcomers. It preconditions that the learners are accomplished developers who master all the IT basics and GUI development. Those who just start to learn SWING or JFX run easily into a maze of rules and possibilities because Design is an innovative imagination. And imagination is abstract. Example: a lot of people can play guitar. But how many persons can play splendidly like this man`?


It's the innovative (or intuitive) Imagination in Music. Same for the Model Design.

Probably due to the complexity SWING was and is let "untouched" and becomes open for those who like the challenge to build a SWING MVC. There are some approaches like my own initiative HERE or Medium.com of Sylvain Saurel.

To reduce the complexity and to keep the versatility I design SWING MVC model that "leans" against the known JFX technique:
- Model in XML plain-text format (FXML)
- View with the API Application
- Controller is the development of "Dependency Injection"
In my SWING model the XML syntax bases simply on 2 formats: <key> and </key> and the elements are assigned by plain texts or strings enclosed by 2 double-quotes. Example:

PHP:
<frame> name=MyFrame tittle="MVC with JDialog" size=600,600 location=20,20
		close=true resize=false</frame>
- element name is the name of the frame (here: SWING JFrame)
- element tittle is the tittle of the displayed JFrame
- element size is the size of the JFrame
- element location is the position of JFrame on the screen (upper left corner: 0,0 for X/Y)
- element close JFrame is cloed on Exit
- element resize JFrame is fixed or resizable

Joe's SWING MVC in brief:

I. MVC-Keywords for SWING J-Components

The following SWING components are currently available:
PHP:
<frame> for JFrame
<panel> for JPanel
<dialog> for JDialog
<tabbedpane> for JTabbedPane
<button> for JButton
<radiobutton> for JRadioButton
<label> for JLabel
<textfield> for JTextField
<passwordfield> for JPasswordField
<formattedtextfield> for JFormattedTextField
<textarea> for JTextArea
<combobox> for JComboBox
<checkbox> for JCheckBox
Because of lack of interest (one Yes for my poll :( ) I just slowdown the implementation of the other J-Components (e.g. JPopupMenu, JFileChooser, etc.).

II. Rules

- <keyword> elements </keyword>
- comment always starts with // (double slashes) and ends at the end of the line (i.e. NewLine)
- continue on next line is allowed
- user is responsible for the structural locations (see: location)
- case sensitive
- either <frame> or <panel> or <dialog> and it must be the 1st line.
- other J-Components (e.g. <label>, etc.) can be in any arbitrary order -except the reserved 1st line for JFrame, JPanel and JDialog
- between keywords and values spaces should be avoided (e.g. location=10,20), as well as between values separated by comma (,)
- String must be enclosed by double quotes
- valid color for anyColor: red, blue, cyan,green, black, gray, darkgray, magenta, white, pink, red, lightgray
- given name must be unique among the other J-components

III. MVC architecture

- View: the main app with the "public static void main()" method
- Model: a plain text file with the keywords
- Controller: the part where actions, improvements (e.g. JDialog, JOptionPane, etc.)
and event handling are developed

VI. XML Syntax
PHP:
<frame>title="anyTitle" size=width, height bgimage=imgName location=a,b
	   resize=false close=false nolayout=false</frame>
obligatory: size
optional: bgimage, location (default 0,0 or upper left corner of screen) and resize,
		  title, close (default: true), nolayout (default true)
 
<dialog>name=anyName tittle="anyTitle" load=panel.txt size=w,h location=x,y</dialog>
obligatory: name, size
optional: location (absolute to screen), load (panelModel file), tittle
Note: if location is omitted then setLocationRelativeTo(owner) should be used
	  Controller. Otherwise it would be at the upper left corner of the screen.
 
<panel>name=name size=w,h location=x,y</panel>
obligatory: name, size, location
 
Loading a JPanel model into a JFrame or a JPanel or JDialog model
<panel>name=anyName file=modelFile size=w,h location=x,y</panel>
obligatory: name, file, size, location
 
<tabbedpane>name=name tabs=model_1.txt,...,model_X.txt tabtext="tex_1",...,"text_X"
			size=w,h location=x,y</tabbedpane>
obligatory: name
optional: tabs (where model_X.txt is the loading JPanel for tabX), tabtext (header for tabX)
 
<button>name=anyName text="anyText" size=w,h location=x,y icon=fName color=anyColor</button>
obligatory: name, size, location (relative to frame/panel), text or icon or both
optional: color
 
<radiobutton>name=anyName text="anyText" size=w,h location=x,y icon=fName color=anyColor</radiobutton>
obligatory: name, size, location (relative to frame/panel), text or icon or both
optional: color
 
<label>name=anyName text="anyText" size=w,h location=x,y icon=fName opaque=true color=anyColor</label>
obligatory: name, size, location (relative to frame/panel), text or icon or both
optional: opaque (default false), color
 
<textfield>name=anyName text="anyText" size=w,h location=x,y column=n color=anyColor</textfield>
obligatory: name, size, location (relative to frame/panel)
optional: text, color
 
<textarea>name=anyName text="anyText" size=w,h location=x,y edit=false
		  row=n column=n color=anyColor scroll=false</textarea>
obligatory: name, size, location (relative to frame/panel)
optional: text, color, scroll (default true), row, column, edit (default true)
 
<combobox>name=anyName items="string_1",..,"string_X" color=anyColor size=w.h location=x,y</combobox>
obligatory: name, size, location
optional: items, color
note: only String as items. Other things must be set by controller with add(item)
 
<checkbox>name=anyName text="text" icon=file selicon=file disicon=file
		 selected=true color=anyColor size=w,h location=x,y</checkbox>
obligatory: name, size, location,  text or icon or both
optional: color, selected, selicon, disicon
 
Sửa lần cuối: