Learn about JFace data binding in Eclipse, part 1th: The pros and cons of data binding

Source: Internet
Author: User
Tags gettext

Many popular WEB applications have View-layer features, and the view layer is smart enough to synchronize request and answer variables with HTML input tags. This process can be done easily, because user input is routed through the structure layer and HTTP of the WEB application. On the other hand, Java GUI applications are often unable to support this feature. Whether they are written using Standard Widget Toolkit (SWT) or Swing, there is usually no well-defined path between the domain objects of these Java GUI applications and their GUI controls (often called components).

Sample (boilerplate) data synchronization

The worst result of the disruption sequence is data chaos, and the most fortunate result is a bug in the large sample sync code. Refer to the Code reference in Listing 1. This is the definition of a simple domain object called Formbean. When a dialog box needs to use this data, the dialog box must extract the data from the domain object and insert the data into the component to display, as shown in the method call at the end of the build program. Accordingly, after the user changes the information, the data must be extracted from the GUI component and put back into the domain model. This reciprocating process is performed through the syncbeantocomponents () and Synccomponentstobean () methods. Finally, note that references to GUI components must remain available within the scope of the object in order to access them in the synchronization method.


Listing 1. Swing dialog box with no data binding

package com.nfjs.examples;


import com.jgoodies.forms.layout.FormLayout;


import com.jgoodies.forms.layout.CellConstraints;


import Com.jgoodies.forms.builder.DefaultFormBuilder;





import javax.swing.*;


import java.awt.event.ActionEvent;





public class Nobindingexample {





private JFrame frame;


private JTextField Firstfield;


private JTextField Lastfield;


private JTextArea Descriptionarea;


private Formbean Bean;





public nobindingexample () {


frame = new JFrame ();





Firstfield = new JTextField ();


Lastfield = new JTextField ();


Descriptionarea = new JTextArea (6, 6);





Defaultformbuilder builder =


New Defaultformbuilder (New Formlayout ("R:p, 2dlu, f:p:g"));


Builder.setdefaultdialogborder ();


Builder.append ("A:", Firstfield);


Builder.append ("Last:", Lastfield);


Builder.appendrelatedcomponentsgaprow ();


builder.appendrow ("P");


Builder.add (New JLabel ("Description:"),


New Cellconstraints (1,


5, Cellconstraints.right,


cellconstraints.top),


new JScrollPane (Descriptionarea),


New Cellconstraints (3,


5, Cellconstraints.fill,


Cellconstraints.fill));





Builder.nextrow (2);


Builder.append (New JButton (new messageaction));





Frame.add (Builder.getpanel ());


frame.setsize (300, 300);





bean = new Formbean ();


syncbeantocomponents ();


  }





private void Syncbeantocomponents () {


Firstfield.settext (Bean.getfirst ());


Lastfield.settext (Bean.getlast ());


Descriptionarea.settext (Bean.getdescription ());


  }





private void Synccomponentstobean () {


Bean.setfirst (Firstfield.gettext ());


Bean.setlast (Lastfield.gettext ());


bean.setdescription (Descriptionarea.gettext ());


  }





public JFrame GetFrame () {


return frame;


  }





Private class Formbean {


private String A;


private String last;


private String description;





public Formbean () {


This.first = "Scott";


this.last = "Delap";


this.description = "description";


    }





public String GetFirst () {


return A;


    }





public void Setfirst (String a) {


This.first = A;


    }





public String GetLast () {


return to last;


    }





public void Setlast (String last) {


this.last = last;


    }





public String getdescription () {


return description;


    }





public void SetDescription (String description) {


this.description = description;


    }


  }





Private class Messageaction extends AbstractAction {


public messageaction () {


super ("message");


    }





public void actionperformed (ActionEvent e) {


Synccomponentstobean ();


Joptionpane.showmessagedialog (NULL, "The name is" + Bean.getfirst ());


    }


  }





public static void Main (string[] args) {


Nobindingexample example = new Nobindingexample ();


Example.getframe (). Show ();


  }


} 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.