Creating of Rich Client Creating of Rich Client Applications using NetBeans Applications using NetBeans
6 and Java Swing6 and Java Swing
Miroslav NachevMiroslav Nachev
ContentsContents
SwingXSwingX Simple Database ApplicationSimple Database Application Another simple Database Application Another simple Database Application
with MasterTable and DetailTablewith MasterTable and DetailTable Rich Client Application exampleRich Client Application example Swing Application FrameworkSwing Application Framework Q & AQ & A
SwingXSwingX
Contains extensions to the Swing GUI Contains extensions to the Swing GUI toolkit, including new and enhanced toolkit, including new and enhanced components that provide components that provide functionality commonly required by functionality commonly required by rich client applications.rich client applications.
SwingX DemoSwingX Demo
SwingX highlights include:SwingX highlights include:
Sorting, filtering, highlighting for tables, Sorting, filtering, highlighting for tables, trees, and liststrees, and lists
Date picker componentDate picker component Find/searchFind/search Auto-completionAuto-completion Login/authentication frameworkLogin/authentication framework TreeTable componentTreeTable component Collapsible panel componentCollapsible panel component Tip-of-the-Day componentTip-of-the-Day component
SwingX linksSwingX links
Links:Links: http://www.swinglabs.org/http://www.swinglabs.org/ https://swingx.dev.java.net/https://swingx.dev.java.net/ https://swinglabs-demos.dev.java.nethttps://swinglabs-demos.dev.java.net// https://swinghelper.dev.java.net/https://swinghelper.dev.java.net/ http://wiki.java.net/bin/view/Javadesktophttp://wiki.java.net/bin/view/Javadesktop
/SwingLabsSwingX/SwingLabsSwingX
Simple Database Simple Database ApplicationApplication
Creating a Database “car_db”Creating a Database “car_db” Creating the Application “CarsApp”Creating the Application “CarsApp” Transaction type: JTA in a JavaEE and Transaction type: JTA in a JavaEE and
RESOURCE_LOCAL in a JavaSERESOURCE_LOCAL in a JavaSE Transaction model: persists all and Transaction model: persists all and
commit to save or rollback to cancelcommit to save or rollback to cancel .properties files containing the labels .properties files containing the labels
in the user interfacein the user interface
Features that are already built Features that are already built into the applicationinto the application
Car.java entity beanCar.java entity bean Bean to Table relation using annotationsBean to Table relation using annotations Property Change notificationProperty Change notification
Persistence unit: META-INF/persistence.xml, Persistence unit: META-INF/persistence.xml, defines a connection between the database and defines a connection between the database and the entity class.the entity class.
Using beans binding (JSR 295) to connect the Using beans binding (JSR 295) to connect the properties of the entity class with the properties properties of the entity class with the properties of the JTable component.of the JTable component.
The entityManager, query, and list objects, which The entityManager, query, and list objects, which are defined in the CarsView classare defined in the CarsView class
Adding More ControlsAdding More Controls
““Tire Size” sliderTire Size” slider ““ModernnessModernness” slider” slider ““SpoilerSpoiler”” checkbox checkbox ““RoofRoof”” checkbox checkbox
Binding Controls to Values in Binding Controls to Values in the Tablethe Table
In the form, right-click the first slider and In the form, right-click the first slider and choose Bind > value.choose Bind > value.
In the Binding Source drop-down list of the In the Binding Source drop-down list of the Binding dialog box, select masterTable.Binding dialog box, select masterTable.
In the Binding Expression drop-down list, In the Binding Expression drop-down list, select selectedElement > tiresize.select selectedElement > tiresize.
Click the Advanced tab.Click the Advanced tab. Select the Unreadable Source Value Select the Unreadable Source Value
checkbox.checkbox. Click the ellipsis (...)button that is next to Click the ellipsis (...)button that is next to
the Unreadable Source Value checkbox.the Unreadable Source Value checkbox.
Binding Controls to Values in Binding Controls to Values in the Tablethe Table
In the Incomplete Path Value dialog box, select In the Incomplete Path Value dialog box, select Custom Code from the drop-down list. Then type Custom Code from the drop-down list. Then type the integer 0.the integer 0.
Right-click the checkbox and choose Customize Right-click the checkbox and choose Customize Code.Code.
In white line under the In white line under the bindingGroup.addBinding(binding) line, type:bindingGroup.addBinding(binding) line, type:
binding.setSourceUnreadableValue(false);binding.setSourceUnreadableValue(false);
Run application and verify into DatabseRun application and verify into Databse
Setting up a Custom Setting up a Custom ComponentComponent
Adding Car Preview packageAdding Car Preview package Drag the CarPreview.java class Drag the CarPreview.java class
(JavaBeans component) to the form(JavaBeans component) to the form Bind all the binding properties of the Bind all the binding properties of the
CarPreview component to the CarPreview component to the corresponding selectedElement corresponding selectedElement attributes of the masterTableattributes of the masterTable
Another simple Database Another simple Database Application with MasterTable Application with MasterTable
and DetailTableand DetailTable Creating a new Database Connection Creating a new Database Connection
to Swing_Demoto Swing_Demo Creating the Application Creating the Application
“MasterDetailDemo”“MasterDetailDemo” Adding annotation @GeneratedValue Adding annotation @GeneratedValue
to Entity beansto Entity beans
Changes in detailTableChanges in detailTable
Switch to Binding category in Properties Switch to Binding category in Properties windowwindow
Select elements propertySelect elements property Invoke property customizer (Press ... Invoke property customizer (Press ...
button)button) Switch to Advanced tab.Switch to Advanced tab. Check 'Unreadable Source Value' (<none> Check 'Unreadable Source Value' (<none>
should be selected in combo next to this should be selected in combo next to this check-a1box).check-a1box).
Starting of the application with Starting of the application with OpenJPA agentOpenJPA agent
Select root node of your project in Projects Select root node of your project in Projects window.window.
Invoke Properties action from contextual Invoke Properties action from contextual menu of this node.menu of this node.
Select Run node (in the tree on the left).Select Run node (in the tree on the left). Enter:Enter:
-javaagent:<PATH_TO_OPENJPA_JAR>/-javaagent:<PATH_TO_OPENJPA_JAR>/openjpa-1.0.1.jar into VM Options field.openjpa-1.0.1.jar into VM Options field.
Rich Client Application Rich Client Application exampleexample
Creating the Application Creating the Application “RichClientApp”“RichClientApp”
Creating a Swing ApplicationCreating a Swing Application Separating Business Logic Interfaces Separating Business Logic Interfaces
from EJB Implementation (Module)from EJB Implementation (Module)
Swing Application Swing Application FrameworkFramework
Framework ArchitectureFramework Architecture. Two classes help . Two classes help you manage your application (one-to-one you manage your application (one-to-one relationship):relationship): ApplicationContextApplicationContext ApplicationApplication
ApplicationContext services:ApplicationContext services: Localizable resource managementLocalizable resource management Task services and monitoringTask services and monitoring Event-action managementEvent-action management Session-state storageSession-state storage
From where to startFrom where to start
All Swing Application Framework applications All Swing Application Framework applications must subclass either the Application class or must subclass either the Application class or its SingleFrameApplication subclass.its SingleFrameApplication subclass.
The SingleFrameApplication adds a default The SingleFrameApplication adds a default main GUI frame, retrieves and injects default main GUI frame, retrieves and injects default resources, and uses the ApplicationContext resources, and uses the ApplicationContext to save and restore simple session state. to save and restore simple session state. Session state includes UI component Session state includes UI component location, size, and configuration.location, size, and configuration.
Application Life CycleApplication Life Cycle
launch -- You must call this framework launch -- You must call this framework method.method.
initialize -- The framework will invoke this initialize -- The framework will invoke this optional overridden method.optional overridden method.
startup -- The framework will invoke this startup -- The framework will invoke this overridden method.overridden method.
ready -- The framework will invoke this ready -- The framework will invoke this optional overridden method.optional overridden method.
exit -- You must call this framework method.exit -- You must call this framework method. shutdown -- The framework will invoke this shutdown -- The framework will invoke this
optional overridden method.optional overridden method.
Example subclasses the Example subclasses the Application classApplication class
public class BasicFrameworkApp extends Applicationpublic class BasicFrameworkApp extends Application{{
private JFrame mainFrame; private JLabel label;private JFrame mainFrame; private JLabel label;protected void startup() protected void startup() {{
mainFrame = new JFrame("BasicFrameworkApp"); mainFrame = new JFrame("BasicFrameworkApp"); mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
mainFrame.addWindowListener(new WindowAdapter() {mainFrame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
mainframe.setVisible(false);mainframe.setVisible(false);exit();exit();
}}});});label = new JLabel("Hello, world!");label = new JLabel("Hello, world!");mainFrame.add(label);mainFrame.add(label);mainFrame.pack();mainFrame.pack();mainFrame.setVisible(true);mainFrame.setVisible(true);
}}public static void main(String[] args) {public static void main(String[] args) {
Application.launch(BasicFrameworkApp.class, args);Application.launch(BasicFrameworkApp.class, args);}}
} }
Example subclasses the Example subclasses the SingleFrameApplication classSingleFrameApplication class
public class BasicSingleFrameApp extends SingleFrameApplicationpublic class BasicSingleFrameApp extends SingleFrameApplication {{JLabel label;JLabel label;
protected void startup() {protected void startup() {getMainFrame().setTitle("BasicSingleFrameApp");getMainFrame().setTitle("BasicSingleFrameApp");label = new JLabel("Hello, world!");label = new JLabel("Hello, world!");label.setFont(new Font("SansSerif", Font.PLAIN, 22));label.setFont(new Font("SansSerif", Font.PLAIN, 22));show(label);show(label);
}}
public static void main(String[] args) {public static void main(String[] args) {Application.launch(BasicSingleFrameApp.class, args);Application.launch(BasicSingleFrameApp.class, args);
}}
}}
ExitListener interfaceExitListener interface
The ExitListener interface has two The ExitListener interface has two methods:methods: public boolean canExit(EventObject e)public boolean canExit(EventObject e) public void willExit(EventObject e)public void willExit(EventObject e)