Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

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

Discussion in 'Java Update' started by Joe, 21/11/19.

Thread Status:
Not open for further replies.
  1. Joe

    Joe Thành viên VIP

    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=widthheight bgimage=imgName location=a,b
           resize
    =false close=false nolayout=false</frame>
    obligatorysize
    optional
    bgimagelocation (default 0,or upper left corner of screen) and resize,
              
    titleclose (default: true), nolayout (default true)
           
    <
    dialog>name=anyName tittle="anyTitle" owner=Name load=panel.txt size=w,h location=x,y</dialog>
    obligatorynamesize
    optional
    ownerlocationload (panelModel file), title. If owner is givenbut location is not then setLocationRelativeTo(ownershould be used to position the Dialog prompt.
     
    <
    panel>name=name size=w,h location=x,y</panel>
    obligatorynamesizelocation
     
    Loading a JPanel model into a JFrame 
    or a JPanel model
    <panel>name=anyName file=modelFile size=w,h location=x,y</panel>
    obligatoryfilesizelocation
     
    <tabbedpane>name=name tabs=panel1.txt,...,panelX.txt tabtext="text1",...,"textX"
                
    size=w,h location=x,y</tabbedpane>
    obligatoryname
    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>
    obligatorynamesizelocation (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>
    obligatorynamesizelocation (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>
    obligatorynamesizelocation (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>
    obligatorynamesizelocation (relative to frame/panel)
    optionaltextcolor
     
    <textarea>name=anyName text="anyText" size=w,h location=x,y edit=false
              row
    =n column=n color=anyColor scroll=false</textarea>
    obligatorynamesizelocation (relative to frame/panel)
    optionaltextcolorscroll (default true), rowcolumnedit (default true)
     
    <
    combobox>name=anyName items="string1",..,"stringX" color=anyColor size=w.h location=x,y</combobox>
    obligatorynamesizelocation
    optional
    itemscolor
    note
    only String as itemsOther 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>
    obligatorynamesizelocation,  text or icon or both
    optional
    colorselectedselicondisicon
    ----------------------------------------------------------------------------------
    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
    <framesize=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 fNamethrows Exception {
        
    ModelLoader ml = new ModelLoader(fName);
        
    JFrame jf = (JFrameml.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<StringObjectmap) {
        
    // get JButton, JTextField and JTextArea
        
    JButton but = (JButtonmap.get("But1");
        
    JTextField jtf = (JTextFieldmap.get("TxtF1");
        
    JTextArea jta  = (JTextAreamap.get("TxtA1");
        
    but.addActionListener(-> {
          
    but.setBackground(onColor.yellow:Color.green);
          
    jta.append("\nButton was clicked. Color changed to:"+(on?"YELLOW":"GREEN"));
          
    on = !on;
        });
        
    // read JTextField and write into JTextArea
        
    jtf.addActionListener(-> {
          
    jta.append("\nName is:"+jtf.getText());
        });
      }
      private 
    boolean on false;
    }
    ----------------------------------------------------------------------------------
    After compiling of TestMVC and TestController the result:
    [​IMG]

    When I complete this work I'll show you the similarity between JFX MVC and this "SWING MVC"
     
    Last edited: 24/11/19
  2. Joe

    Joe Thành viên VIP

    The poll will be closed on Nov. 24 ....
    Another application using JPanel as the model

    The model: panel.txt
    PHP:
    // the viewing JPanel
    <panelsize=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 fNamethrows Exception {
        
    ModelLoader ml = new ModelLoader(fName);
        
    JPanel jp = (JPanelml.load();
        
    PanelController pc = new PanelController(ml.getComponentMap());
        
    JFrame jf = new JFrame("PanelMVC");
        
    jf.setSize(400450);
        
    jf.setLocation(2020);
        
    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<StringObjectmap) {
        
    // get JButton, JTextField and JTextArea
        
    JButton but = (JButtonmap.get("But1");
        
    JTextField jtf = (JTextFieldmap.get("TxtF1");    
        
    JTextArea jta  = (JTextAreamap.get("TxtA1");
        
    but.addActionListener(-> {
          
    but.setBackground(onColor.yellow:Color.green);
          
    jta.append("\nButton was clicked. Color changed to:"+(on?"YELLOW":"GREEN"));
          
    on = !on;
        });
        
    // read JTextField and write into JTextArea
        
    jtf.addActionListener(-> {
          
    jta.append("\nName is:"+jtf.getText());
        });
        
    JComboBox cbx = (JComboBoxmap.get("cBox");
        
    cbx.addActionListener(-> {
          
    jta.append("\nYou've selected:"+(String)cbx.getSelectedItem());
        });
        
    c1 true;
        
    c2 c3 false;
        
    JCheckBox cb1 = (JCheckBoxmap.get("kBox1");
        
    cb1.addItemListener(-> {
           if (
    c1jta.append("\nYour choice: Ripe.");
           
    c1 = !c1;
           
    cb1.setForeground(c1?Color.red:Color.blue);
           
    cb1.setText(c1?"Ripe":"gone");
        });
        
    JCheckBox cb2 = (JCheckBoxmap.get("kBox2");
        
    cb2.addItemListener(-> {
           
    c2 = !c2;
           if (
    c2jta.append("\nYour choice: Green.");
           
    cb2.setForeground(c2?Color.blue:Color.green);
           
    cb2.setText(c2?"gone":"Green");
        });
        
    JCheckBox cb3 = (JCheckBoxmap.get("kBox3");
        
    cb3.addItemListener(-> {
           
    c3 = !c3;
           if (
    c3jta.append("\nYour choice: NeverMind.");
           
    cb3.setForeground(c3?Color.blue:Color.yellow);
           
    cb3.setText(c3?"gone":"Yellow");
        });
      }
      private 
    boolean on falsec1c2c3;
    }
    and after compiling
    [​IMG]
     
    Last edited: 22/11/19
  3. Joe

    Joe Thành viên VIP

    ...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
    <frametitle="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 fNamethrows Exception {
        
    ModelLoader ml = new ModelLoader(fName);
        
    JFrame jf = (JFrameml.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
    [​IMG]
    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
     
    Last edited: 22/11/19
  4. Joe

    Joe Thành viên VIP

    ...=))
    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
    <frametitle="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
    <panelname=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
    <panelname=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<StringObjectmap) {
        
    // get JButton, JTextField and JTextArea
        
    JButton but1 = (JButtonmap.get("But1");
        
    JTextField jtf1 = (JTextFieldmap.get("TxtF1");
        
    JTextArea jta1  = (JTextAreamap.get("TxtA1");
        
    but1.addActionListener(-> {
          
    but1.setBackground(on1Color.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(-> {
          
    jta1.append("\nName is:"+jtf1.getText());
        });
        
    JButton but2 = (JButtonmap.get("But2");
        
    JTextField jtf2 = (JTextFieldmap.get("TxtF2");
        
    JTextArea jta2  = (JTextAreamap.get("TxtA2");
        
    but2.addActionListener(-> {
          
    but2.setBackground(on2Color.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(-> {
          
    jta2.append("\nName is:"+jtf2.getText());
        });
        
    JComboBox cbx = (JComboBoxmap.get("cBox");
        
    cbx.addActionListener(-> {
          
    jta2.append("\nYou've selected:"+(String)cbx.getSelectedItem());
        });
      }
      private 
    boolean on1 falseon2 false;
    The viewer: TabbedMVC.java
    PHP:
    import joe.cdj.mvc.ModelLoader;
    // Joe Nartca (C)
    public class TabbedMVC {
      public 
    TabbedMVC(String fNamethrows Exception {
        
    ModelLoader ml = new ModelLoader(fName);
        
    JFrame jf = (JFrameml.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(); }
      }
    }
    [​IMG]
     

    Attached Files:

    Last edited: 24/11/19
  5. Joe

    Joe Thành viên VIP

    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
    <framename=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
    <panelname=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<StringObjectmap) {
        
    // get JButton, JTextField and JTextArea
        
    JButton start  = (JButtonmap.get("Start");
        
    JButton but    = (JButtonmap.get("But");
        
    JTextField jtf = (JTextFieldmap.get("TxtF");  
        
    JTextArea jta  = (JTextAreamap.get("TxtA");
      
        
    start.addActionListener(-> {
          
    JDialog jd = (JDialogmap.get("MyDialog");
          
    jta.append("\nMVC for JDialog started.");
          
    jd.setVisible(true);
        });
      
        
    but.addActionListener(-> {
          
    but.setBackground(onColor.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(-> {
          
    jta.append("\nName is:"+jtf.getText());
        });
        
    JComboBox cbx = (JComboBoxmap.get("cBox");
        
    cbx.addActionListener(-> {
          
    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 fNamethrows Exception {
        
    ModelLoader ml = new ModelLoader(fName);
        
    JFrame jf = (JFrameml.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
    [​IMG]
     
    Last edited: 24/11/19
Thread Status:
Not open for further replies.

Chia sẻ trang này

Loading...