Self-developed Model-view-controller (mvc) In Java

Trạng thái
Chủ đề đã được đóng (không tiếp nhận thảo luận)

Joe

Thành viên VIP
21/1/13
2,938
1,304
113
Some words about pure JAVA SWING MVC
by Joe Nartca (C)​

Created: Nov. 20 2019
UPDATE: Nov. 21 2019
UPDATE: Nov. 22 2019
UPDATE: closed on Nov.24 1019


Keywords for SWING J-Components:
PHP:
<frame> for JFrame
<panel> for JPanel
<button> for JButton
<label> for JLabel
<textfield> for JTextField
<textarea> for JTextArea
<combobox> for JComboBox
<checkbox> for JCheckBox
----------------------------------------------------------------------------------
Rules:
  • <keyword> elements </keyword>
  • NO embedding (future release)
  • comment always starts with // and ends at the end of the line (i.e. NewLine)
  • continue on next line is allowed
  • the user is responsible for the structural locations (see: locazion)
  • case sensitive
  • either <frame> or <panel> and it must be the 1st line.
  • other J components (e.g. <label>, etc.) can be in any order -except the 1st line
  • between keywords and values spaces should be avoided (e.g. location=10,20), as well as between values separated by comma (,)

----------------------------------------------------------------------------------
MVC architechture:
  • View: the main app with the "public static void main()" method
  • Model: a plain text file with the keywords (see examples)
  • Controller: the part where action, improvements (e.g. JDialog, JOptionPane, etc.) and event handling
----------------------------------------------------------------------------------
Elements:
PHP:
<frame>tittle="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" owner=Name load=panel.txt size=w,h location=x,y</dialog>
obligatory: name, size
optional: owner, location, load (panelModel file), title. If owner is given, but location is not then setLocationRelativeTo(owner) should be used to position the Dialog prompt.
 
<panel>name=name size=w,h location=x,y</panel>
obligatory: name, size, location
 
Loading a JPanel model into a JFrame or a JPanel model
<panel>name=anyName file=modelFile size=w,h location=x,y</panel>
obligatory: file, size, location
 
<tabbedpane>name=name tabs=panel1.txt,...,panelX.txt tabtext="text1",...,"textX"
			size=w,h location=x,y</tabbedpane>
obligatory: name
optional: tabs (where panelX.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="string1",..,"stringX" 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
----------------------------------------------------------------------------------
Elements:
  • valid color for "anyColor": red, blue, cyan,green, black, gray, darkgray, magenta, white, pink, red, lightgray
  • text should be closed by double quote: "..."
  • given name must be unique among the other J-components

SWING J-components:
  • frame for JFrame
  • panel for JPanel
  • button for JButton
  • label for JLabel
  • combobox for JComboBox
  • checkbox for JCheckBox
  • textfield for JTextField
  • textarea for JTextArea
  • Others will be implemented
----------------------------------------------------------------------------------
examples

- The model: test.txt
PHP:
// the viewing frame
<frame> size=400,710 bgimage=AIman.jpg location=200,0
		close=true resize=false nolayout=true</frame>
// label and button
<label>name=Lab1 text="Please click", size=100,12 location=10,25</label>
<button>name=But1 text="MVCbutton" size=100,50 location=90,5 color=yellow</button>
// label and textfield
<label>name=Lab2 text="Name:", size=100,12 location=10,75</label>
<textfield>name=TxtF1 text="Your Name" size=250,30 location=90,65 color=yellow</textfield>
// label and textarea
<label>name=Lab3 text="This is an IT man in Background", size=200,14 location=56,188</label>
<label>name=Lab4 text="ReportArea", size=100,14 location=150,460</label>
<textarea>name=TxtA1 text="No Report" size=350,180 location=15,480 scroll=true edit=false</textarea>
// end of frame
- The view: TestMVC.java
PHP:
import joe.cdj.mvc.ModelLoader;
// Joe Nartca (C)
public class TestMVC {
  public TestMVC(String fName) throws Exception {
	ModelLoader ml = new ModelLoader(fName);
	JFrame jf = (JFrame) ml.load();
	TestController tc = new TestController(ml.getComponentList());
	jf.setVisible(true);
  }
  public static void main(String... a) {
	try {
	  UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
	  new TestMVC(a.length == 0?"test.txt":a[0]);
	} catch (Exception ex) {ex.printStackTrace(); }
  }
}
- The Controller: TestController.java
PHP:
import cdj.swing.mvc.*;
// Joe Nartca (C)
public class TestController {
  public TestController(HashMap<String, Object> map) {
	// get JButton, JTextField and JTextArea
	JButton but = (JButton) map.get("But1");
	JTextField jtf = (JTextField) map.get("TxtF1");
	JTextArea jta  = (JTextArea) map.get("TxtA1");
	but.addActionListener(e -> {
	  but.setBackground(on? Color.yellow:Color.green);
	  jta.append("\nButton was clicked. Color changed to:"+(on?"YELLOW":"GREEN"));
	  on = !on;
	});
	// read JTextField and write into JTextArea
	jtf.addActionListener(e -> {
	  jta.append("\nName is:"+jtf.getText());
	});
  }
  private boolean on = false;
}
----------------------------------------------------------------------------------
After compiling of TestMVC and TestController the result:


When I complete this work I'll show you the similarity between JFX MVC and this "SWING MVC"
 
Sửa lần cuối:

Joe

Thành viên VIP
21/1/13
2,938
1,304
113
The poll will be closed on Nov. 24 ....
Another application using JPanel as the model

The model: panel.txt
PHP:
// the viewing JPanel
<panel> size=400,450 location=200,0 nolayout=true</panel>
// label and button
<label>name=Lab1 text="Please click", size=100,12 location=10,25</label>
<button>name=But1 text="MVCbutton" size=100,50 location=90,5 color=yellow</button>
// label and textfield
<label>name=Lab2 text="Name:", size=100,12 location=10,75</label>
<textfield>name=TxtF1 text="Your Name" size=250,30 location=90,65 color=yellow</textfield>
// label and textarea
<label>name=Lab3 text="ReportArea", size=100,12 location=10,100</label>
<textarea>name=TxtA1 text="No Report" size=360,200 location=10,120 scroll=true</textarea>
// label, combobox
<label>name=Lab4 text="Make a choice", size=100,12 location=10,330</label>
<combobox>name=cBox items="Apple","Orange","Banana","Guava" size=200,30 location=100,320</combobox>
// label, 3 checkboxes
<label>name=Lab5 text="Make a check", size=100,12 location=10,360</label>
<checkbox>name=kBox1 text="Ripe" color=red selected=true size=50,30 location=100,352</checkbox>
<checkbox>name=kBox2 text="Green" color=green size=60,30 location=160,352</checkbox>
<checkbox>name=kBox3 text="Never mind" color=yellow size=100,30 location=235,352</checkbox>
// end of JPanel
The view: Panel.java
PHP:
import javax.swing.*;
import java.awt.event.*;
//
import joe.cdj.mvc.ModelLoader;
// Joe Nartca (C)
public class PanelMVC {
  public PanelMVC(String fName) throws Exception {
	ModelLoader ml = new ModelLoader(fName);
	JPanel jp = (JPanel) ml.load();
	PanelController pc = new PanelController(ml.getComponentMap());
	JFrame jf = new JFrame("PanelMVC");
	jf.setSize(400, 450);
	jf.setLocation(20, 20);
	jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	jf.add(jp);
	jf.setVisible(true);
  }
  public static void main(String... a) {
	try {
	  UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
	  new PanelMVC(a.length == 0?"panel.txt":a[0]);
	} catch (Exception ex) {ex.printStackTrace(); }
  }
}
The Controller: PanelController.java
PHP:
import java.awt.*;
import java.util.*;
import javax.swing.*;
//
import joe.cdj.mvc.*;
// Joe Nartca (C)
public class PanelController {
  public PanelController(HashMap<String, Object> map) {
	// get JButton, JTextField and JTextArea
	JButton but = (JButton) map.get("But1");
	JTextField jtf = (JTextField) map.get("TxtF1");	
	JTextArea jta  = (JTextArea) map.get("TxtA1");
	but.addActionListener(e -> {
	  but.setBackground(on? Color.yellow:Color.green);
	  jta.append("\nButton was clicked. Color changed to:"+(on?"YELLOW":"GREEN"));
	  on = !on;
	});
	// read JTextField and write into JTextArea
	jtf.addActionListener(e -> {
	  jta.append("\nName is:"+jtf.getText());
	});
	JComboBox cbx = (JComboBox) map.get("cBox");
	cbx.addActionListener(e -> {
	  jta.append("\nYou've selected:"+(String)cbx.getSelectedItem());
	});
	c1 = true;
	c2 = c3 = false;
	JCheckBox cb1 = (JCheckBox) map.get("kBox1");
	cb1.addItemListener(e -> {
	   if (c1) jta.append("\nYour choice: Ripe.");
	   c1 = !c1;
	   cb1.setForeground(c1?Color.red:Color.blue);
	   cb1.setText(c1?"Ripe":"gone");
	});
	JCheckBox cb2 = (JCheckBox) map.get("kBox2");
	cb2.addItemListener(e -> {
	   c2 = !c2;
	   if (c2) jta.append("\nYour choice: Green.");
	   cb2.setForeground(c2?Color.blue:Color.green);
	   cb2.setText(c2?"gone":"Green");
	});
	JCheckBox cb3 = (JCheckBox) map.get("kBox3");
	cb3.addItemListener(e -> {
	   c3 = !c3;
	   if (c3) jta.append("\nYour choice: NeverMind.");
	   cb3.setForeground(c3?Color.blue:Color.yellow);
	   cb3.setText(c3?"gone":"Yellow");
	});
  }
  private boolean on = false, c1, c2, c3;
}
and after compiling
 
Sửa lần cuối:

Joe

Thành viên VIP
21/1/13
2,938
1,304
113
...and the embedded JPanel in JFrame model (with the background of a RicePaddy from Vietnam)
The model: embedded.txt
PHP:
// the viewing frame with embedded JPanel from panel model
<frame> title="MVC with JFrame -embedded JPanel-" size=800,500 location=20,20
		bgimage=RicePaddy.jpg close=true resize=false nolayout=true</frame>
// embedded JPanel
<panel>name=EmbeddedJPanel file=panel.txt size=400,460 location=400,0</panel>
// end of frame: https://i.ibb.co/Vm5FQvC/embedded-Panel.png
The view: EmbeddedMVC
PHP:
import javax.swing.*;
import java.awt.event.*;
//
import joe.cdj.mvc.ModelLoader;
// Joe Nartca (C)
public class EmbeddedMVC {
  public EmbeddedMVC(String fName) throws Exception {
	ModelLoader ml = new ModelLoader(fName);
	JFrame jf = (JFrame) ml.load();
	PanelController ec = new PanelController(ml.getComponentMap());
	jf.setVisible(true);
  }
  public static void main(String... a) {
	try {
	  UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
	  new EmbeddedMVC(a.length == 0?"embedded.txt":a[0]); // <-- default: embedded.txt
	} catch (Exception ex) {ex.printStackTrace(); }
  }
}
The Controller: see above: PanelController.java

unzip the joemvc.zip for the joemvc.jar and include it in your global CLASSPATH or do it locally as following (here: windows) in your CMD window
Code:
set CLASSPATH=C:\cdj\java\joemvc.jar;%CLASSPATH%
joemvc.jar contains 2 classes: ModelLoader.class and Tools.class
 
Sửa lần cuối:

Joe

Thành viên VIP
21/1/13
2,938
1,304
113
...=))
It seems to me that people aren't interested in self-development. Well, Thread will be closed tomorrow and I'll start a series discussion generally about MVC with JAVA SWING and JFX.

The joemvc.zip contains the needed joemvc.jar and the given examples (incl. images). Last stand: JTabbedPane is included for your "testing".

The model: tabbed.txt
PHP:
// the viewing frame with JTabbedPane from panel model
<frame> title="MVC with JFrame -embedded JPanel-" size=500,500 location=20,20
		bgimage=RicePaddy.jpg close=true resize=false</frame>
<tabbedpane>name=Tabbed tabs=tab1.txt,tab2.txt tabtext="Tabbed_1","Tabbed_2"
		size=400,400 location=40,15</tabbedpane>
// end of frame
tab1.tx
PHP:
// panel for tab1
<panel> name=Pane1 size=400,500 location=200,20 </panel>
<label>name=Lab1 text="Please click", size=100,12 location=10,25</label>
<button>name=But1 text="MVCbutton" size=100,50 location=90,5 color=yellow</button>
<label>name=Lab2 text="Name:", size=100,12 location=10,75</label>
<textfield>name=TxtF1 text="Your Name" size=250,30 location=90,65 color=yellow</textfield>
<label>name=Lab3 text="ReportArea", size=100,12 location=10,100</label>
<textarea>name=TxtA1 text="No Report" size=360,200 location=10,120 scroll=true</textarea>
// end of pane1
and tab2.txt
PHP:
// panel for tab2
<panel> name=Pane2 size=400,500 location=200,20 </panel>
<label>name=Lab4 text="Please click", size=100,12 location=10,25</label>
<button>name=But2 text="MVCbutton" size=100,50 location=90,5 color=yellow</button>
<label>name=Lab5 text="Name:", size=100,12 location=10,75</label>
<textfield>name=TxtF2 text="Your Name" size=250,30 location=90,65 color=yellow</textfield>
<label>name=Lab6 text="ReportArea", size=100,12 location=10,100</label>
<textarea>name=TxtA2 text="No Report" size=360,200 location=10,120 scroll=true</textarea>
<label>name=Lab4 text="Make a choice", size=100,12 location=10,330</label>
<combobox>name=cBox items="Apple","Orange","Banana","Guava" size=200,30 location=100,320</combobox>
// end of pane1
The controller: TabbedController.java
PHP:
import joe.cdj.mvc.*;
// Joe Nartca (C)
public class TabbedController {
  public TabbedController(HashMap<String, Object> map) {
	// get JButton, JTextField and JTextArea
	JButton but1 = (JButton) map.get("But1");
	JTextField jtf1 = (JTextField) map.get("TxtF1");
	JTextArea jta1  = (JTextArea) map.get("TxtA1");
	but1.addActionListener(e -> {
	  but1.setBackground(on1? Color.yellow:Color.green);
	  jta1.append("\nButtonTab_1 was clicked. Color changed to:"+(on1?"YELLOW":"GREEN"));
	  on1 = !on1;
	});
	// read JTextField and write into JTextArea
	jtf1.addActionListener(e -> {
	  jta1.append("\nName is:"+jtf1.getText());
	});
	JButton but2 = (JButton) map.get("But2");
	JTextField jtf2 = (JTextField) map.get("TxtF2");
	JTextArea jta2  = (JTextArea) map.get("TxtA2");
	but2.addActionListener(e -> {
	  but2.setBackground(on2? Color.yellow:Color.green);
	  jta2.append("\nButtonTab_2 was clicked. Color changed to:"+(on2?"YELLOW":"GREEN"));
	  on2 = !on2;
	});
	// read JTextField and write into JTextArea
	jtf2.addActionListener(e -> {
	  jta2.append("\nName is:"+jtf2.getText());
	});
	JComboBox cbx = (JComboBox) map.get("cBox");
	cbx.addActionListener(e -> {
	  jta2.append("\nYou've selected:"+(String)cbx.getSelectedItem());
	});
  }
  private boolean on1 = false, on2 = false;
The viewer: TabbedMVC.java
PHP:
import joe.cdj.mvc.ModelLoader;
// Joe Nartca (C)
public class TabbedMVC {
  public TabbedMVC(String fName) throws Exception {
	ModelLoader ml = new ModelLoader(fName);
	JFrame jf = (JFrame) ml.load();
	TabbedController ec = new TabbedController(ml.getComponentMap());
	jf.setVisible(true);
  }
  public static void main(String... a) {
	try {
	  UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
	  new TabbedMVC(a.length == 0?"tabbed.txt":a[0]);
	} catch (Exception ex) {ex.printStackTrace(); }
  }
}
 

Attachments

Sửa lần cuối:

Joe

Thành viên VIP
21/1/13
2,938
1,304
113
Dear all,
poll is closed. I'm sorry to see only ONE vote for a continue. I'll stop here and start a perfunctory series about JFX and SWING MVC. FYI: A SWING MVC with JDialog
- the model: dialog.txt
PHP:
// the viewing frame with embedded JDialog
<frame> name=MyFrame title="MVC with JDialog" size=600,600 location=20,20
		close=true resize=false</frame>
<button>name=Start text="Start Dialog" size=100,100 location=230,230 color=yellow</button>
<dialog>name=MyDialog owner=MyFrame load=dPanel.txt size=500,500 location=60,90
		bgimage=RicePaddy.jpg </dialog>
// end of frame
dPanel.txt
PHP:
// panel for Dialog
<panel> name=Pane1 size=400,400 location=40,25 </panel>
<label>name=Lab1 text="Please click", size=100,12 location=10,25</label>
<button>name=But text="MVCbutton" size=100,50 location=90,5 color=yellow</button>
<label>name=Lab2 text="Name:", size=100,12 location=10,75</label>
<textfield>name=TxtF text="Your Name" size=250,30 location=90,65 color=yellow</textfield>
<label>name=Lab3 text="ReportArea", size=100,12 location=10,100</label>
<textarea>name=TxtA text="No Report" size=360,200 location=10,120 scroll=true</textarea>
<label>name=Lab4 text="Make a choice", size=100,12 location=10,330</label>
<combobox>name=cBox items="Apple","Orange","Banana","Guava" size=200,30 location=100,320</combobox>
// end of pane1
DialogController.java
PHP:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
//
import joe.cdj.mvc.*;
// Joe Nartca (C)
public class DialogController {
  public DialogController(HashMap<String, Object> map) {
	// get JButton, JTextField and JTextArea
	JButton start  = (JButton) map.get("Start");
	JButton but	= (JButton) map.get("But");
	JTextField jtf = (JTextField) map.get("TxtF");  
	JTextArea jta  = (JTextArea) map.get("TxtA");
  
	start.addActionListener(e -> {
	  JDialog jd = (JDialog) map.get("MyDialog");
	  jta.append("\nMVC for JDialog started.");
	  jd.setVisible(true);
	});
  
	but.addActionListener(e -> {
	  but.setBackground(on? Color.yellow:Color.green);
	  jta.append("\nButtonTab_1 was clicked. Color changed to:"+(on?"YELLOW":"GREEN"));
	  on = !on;
	});
	// read JTextField and write into JTextArea
	jtf.addActionListener(e -> {
	  jta.append("\nName is:"+jtf.getText());
	});
	JComboBox cbx = (JComboBox) map.get("cBox");
	cbx.addActionListener(e -> {
	  jta.append("\nYou've selected:"+(String)cbx.getSelectedItem());
	});
  }
  private boolean on = false;
}
DialogMVC.java
PHP:
import javax.swing.*;
import java.awt.event.*;
//
import joe.cdj.mvc.ModelLoader;
// Joe Nartca (C)
public class DialogMVC {
  public DialogMVC(String fName) throws Exception {
	ModelLoader ml = new ModelLoader(fName);
	JFrame jf = (JFrame) ml.load();
	DialogController ec = new DialogController(ml.getComponentMap());
	jf.setVisible(true);
  }
  public static void main(String... a) {
	try {
	  UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
	  new DialogMVC(a.length == 0?"dialog.txt":a[0]);
	} catch (Exception ex) {ex.printStackTrace(); }
  }
}
and the MVC itself
 
Sửa lần cuối:
Trạng thái
Chủ đề đã được đóng (không tiếp nhận thảo luận)