Creating of Rich Client Applications using NetBeans 6 and

Download Report

Transcript Creating of Rich Client Applications using NetBeans 6 and

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