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

Joe

Thành viên VIP
21/1/13
2,938
1,305
113
(continue of Model View Controller With SWING And JFX- Part 1-)

The Coordinates:
Every MVC app must be positioned somewhere on the screen. Left, center, right, etc. The coordination is usually referred to the X-Y axis of the screen: 0,0 at the upper left corner of the screen.


The frame (or JFrame) coordinates are absolute and relating to the coordinates of the screen (0,0 is the upper left corner) while the coordinates of its J-Components (e.g. JPanel, JLabel, etc.) are relating to the JFrame coordinates. JDialog model coordinates are similar to JFrame model. Dialog coordinates are absolute and relating to the coordinates of the screen.

Example: The model Jframe.txt
PHP:
<frame>name=Jframe size=400,710 bgimage=AIman.jpg location=200,0
		close=true resize=false</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
<label>name=Lab3 text="This is an IT man in Background", size=200,14 location=56,188</label>
// label and textarea
<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
Explanation:

For the JFrame <frame>.....</frame>
- name=Jframe: the model is a JFrame model with the name Jframe (access name for the Controller)
- size=400,710: the size of JFrame
- location=200,0: positioned at x=200, y=0 relating to the screen coordinates
- bgimage=AIman.jpg: the image for JFrame background is from the file AIman.jpg
- close=true: JFrame is closed when the X on the upper right corner of JFrame is clicked
- resize=false: JFrame is NOT resizable

For the J-Components, e.g.JTextField <textfield>....</textfield>
- name=TxtF: the access name of JTextField for the Controller
- text="....": the displayed default of JTextField
- size=250,30: the field width is 250 and height is 30
- location=10,75: positioned at x=10, y=75 relating to JFrame (NOT the window screen)
- color=yellow: the field background is yellow

Similar to the others: JLabel <label> and JTextArea <textarea>

And the "view" is on the screen


The Controller
PHP:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
//
import joe.cdj.mvc.*;
// Joe Nartca (C)
public class FrameController {
  public FrameController(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;
}
As you see, the controller "injects 2 dependencies" into JButton and JTextField. All J-Components are accessed via their given names (here: But1, TxF1 and TxtA1). Clean and simple, isn't it?

And the View is driven by this little app where the ModelLoader is executed to load the model and the controller. In Java FX the ModelLoader is the FXMLLoader.
PHP:
import javax.swing.*;
import java.awt.event.*;
//
import joe.cdj.mvc.ModelLoader;
// Joe Nartca (C)
public class FrameMVC {
  public FrameMVC(String fName) throws Exception {
	// load the model
	ModelLoader ml = new ModelLoader(fName);
	// get the JFrame
	JFrame jf = (JFrame) ml.load();
	// load the Controller
	FrameController tc = new FrameController(ml.getComponentMap());
	// show the JFrame
	jf.setVisible(true);
  }
  public static void main(String... a) {
	try {
	  UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
	  new FrameMVC(a.length == 0?"frame.txt":a[0]); // default: frame.txt
	} catch (Exception ex) {ex.printStackTrace(); }
  }
}