APPENDIX B



APPENDIX B

Java Client User Interface Components

Swing components are Java program units written to conform to a specific (JavaBean) protocol. These components control the layout and visibility of your data.

The Data Control Palette

This palette displays the available data model components for the application module. It’s function is to allow you to drag and drop data model components associated with a user interface object into the visual editor, creating the object and binding it to the data model component in a single step.

The Component Palette

This palette becomes visible when you are using the UI Editor. Here is a sample shot when using JDeveloper's Visual Editor.

Component Palette

[pic]

Component Palette

[pic]

The pull-down list box has the following options:

• AWT

• Code Snippets

• JClient controls

• Swing

• Swing Containers

AWT – Abstract Windowing Toolkit Component Palette

These are JavaBeans distributed as part of the Sun's Java SDK. JavaBeans use the underlying operating system-specific controls to display the user-interface components.

[pic]

Although they are easy to use, AWT components are less flexible than their Swing component counterparts. To ensure consistency across platforms, Swing components are recommended.

AWT Component Descriptions

Button A standard push button with a text label, icon, or both.

Checkbox A box that toggles between checked (true) or unchecked (false).

CheckboxGroup A container for Checkbox components

Choice A pulldown list presenting multiple values for the user to select from.

Label A text label (can add an icon).

List Presents a list of text strings without a scrollbar (AWT provides one but Swing does not).

MenuBar Allows creation of menus and pulldown menus.

PopupMenu Allows creation of popup menus which appear with the used right clicks a component.

Panel A container into which you can place other components.

ScrollBar Allow creation of vertical and horizontal scrollbars.

ScrollPane A pane containing vertical and horizontal scrollbars.

TextArea Provides a text editing area.

TextField A single-row editing area foe entering/displaying data.

Code Snippets Component Palette

You can create a reusable code snippet (a small amount of code that you may want to use over and over again in your projects), and store it on the Code Snippets Component Palette.

JClient Controls Component Palette

These are supplied by JDeveloper to supplement the standard Swing components.

[pic]

These controls implement the JClient facility and allow normal Swing components to be bound to ADF BC objects.

dataPanel

A container used to hold data controls in your project. It has ADF BC binding capability.

JUImage

Binds to a ADF BC view object to support LONG RAW, RAW BLOB datatypes, and interMedia IMAGE types. It uses the JClient attribute model to display the ADF BC attribute values.

JULabel

A non-editable field, that binds to a ADF BC attribute to retrieve data. It uses the JClient attribute model to display the ADF BC attribute values. Useful for displaying data in a non-editable form.

JUNavigationBar

A toolbar that can be bound to a view-object usage , to control items in the same panel.

JURadioButtonGroupPanel

A panel control used in binding ADF BC view attribute values for a set of radio buttons.

JUStatusBar

A container for displaying the status of data items in a panel bound to a ADF BC view usage.

JUArrayComboBox

A pulldown that presents values from one ADF BC view attribute.

MediaControl

A display object that is bound to a ADF BC attribute for multimedia data.

Swing Component Palette

The components are part of the Java Foundation Classes library in the Sun Java SDK.

[pic]

Swing component names usually begin a J and are contained in the javax.swing package. They are an extension of the AWT controls, but have a more complete set of properties.

Swing Component Descriptions

JButton A standard push button with a text label, icon, or both.

JCheckbox A box that toggles between checked (true) or unchecked (false).

JComboBox A pulldown list showing multiple values the user can select from.

JEditorPane A box that can display text formatted in HTML or RTF.

JLabel A text label (can add an icon).

JList Presents a list of text strings without a scrollbar

(use the JScrollBarPane container if you need a scrollbar for this component).

JPasswordField Like a JTextField except hides input characters with *'s.

JProgressBar Graphically shows the completion percentage for a process.

JRadioButton A single selection button usually within a group of other radio buttons for selecting options etc. Only one can be selected in a group.

JScrollBar A vertical or horizontal scrollbar with a button.

JSeparator Used to separate menu items with a horizontal bar or as a spacer/separator vertical line between buttons on on a toolbar.

JSlider A visual control similar to a scrollbar. Usually used to change values.

JTable A spreadsheet like grid for displaying data.

JTextArea Another text box for displaying text strings. Has no scrollbar but a JScrollBar component can be added.

JTextField A single row text editing area.

JTextPane A subclass of JEditorPane allowing you to embed images and other components within that text.

JTree Used to display hierarchical data similar to Windows Explorer.

JToggleButton Looks like a JButton control, but when pressed sets a state to "True" until it is pressed again (like check boxes).

Swing Containers Component Palette

This palette contains Swing components used as containers for other components.

[pic]

JMenuBar

A component to which you can add menus and menu items. The menu bar appears under the title bar of a window and includes a pulldown menu system. (Chapter 19 has more details)

JPanel

It is the main container that is used in many places in the application, and acts as a generic container to hold other objects. A JPanel is non-visible, but you can specify a border for it. It is used a lot for organizing objects in the various layout managers (discussed in Chapter 20).

JPopupMenu

Used to define a menu that pops up when the user right clicks a component. (more on this in Chapter 19)

JScrollPane

Allows you to define horizontal and vertical scrollbars.

JSplitPane

A container providing two work areas with a movable bar for controlling the width/height of the work areas.

JTabbedPane

Offers a standard tab folder interface with multiple pages. You can specify which edge the tabs appear on, and use a other container component for the pages.

JToolBar

Provides a container for buttons and other components. It is floatable so that you can drag it out of the window into its own window. A JToolBar can be defined with a horizontal or vertical orientation. (discussed further in Chapter 19)

Using Swing Components

In JDeveloper, Swing component objects can be added three ways:

Placed into the Structure window in a tree structure

Dropped directly into the UI Editor

Added in the code

Getting the components on the screen and coupling them to ADF BC is straightforward.

Adding Swing Components to a Program

If a component you want to add is in the Component Palette (UI Editor is open) you can select it and click on the UI Editor where you want it to appear or in the Structure window in the desired position. You will probably find it easier to add components to the Structure window and then position them using the UI Editor. It is important that each component be in its correct logical place. This is sometimes difficult because a layout manager forces a particular behavior.

When a Swing component is added, JDeveloper generates code into your program. For example, when a JButton object is added this line of code is generated:

private JButton jButton1 = new JButton();

Next, in the jbInit() method of the code one or more properties are set such as:

jButton1.setText("jButton1");

Finally, the component is added to the container:

dataPanel1.add(jButton, BorderLayout.CENTER);

Also, appropriate import statements are automatically added like:

import javax.swing.JButton;

If the component is renamed or its properties modified, the code will automatically be updated accordingly.

Categories of Swing Components

Swing components can be grouped as follows:

• Container Windows, frames, panes, and panels. They act as areas on the screen into which you place visual components.

• Data Text fields, checkboxes, tables, trees, etc.

• Action Buttons, sliders, free-standing scrollbars, etc Components that control other components, generate actions or values, or respond to events.

• Static Label and graphic items

• Nonvisual Timers and other components that are not displayed

• Dialog Help messages, modal alert boxes (JOptionPane)

Container Objects

These are usually the first Swing components you add to your user interface application, and act as windows/canvases on which to place other objects. Container components need to be bound to ADF BC so that the data components within the containers will be able to access data.

In addition to the JPanel, JTabbedPane, JScrollPane, and JSplitPane from above, other common container are:

• JFrame The main container frame for the application. It appears as a window on the screen, and there is usually only one JFrame in a program.

• JInternalFrame Appears as a window in the user interface, and is used to support the Multiple Document Interface (MDI).

• JDesktopPane An enclosing panel to support the JInternalFrame windows.

Laying out the frames and panels for an application is an IMPORTANT step. Containers are often nested.

Container Layout Guidelines

In designing the user interface of a program, layout panels and frames is critical and often complicated.

Frames, panels, and panes are created three ways:

Use of wizards

This is where you typically start your development.

Use the JDeveloper UI Editor and Property Inspector

This method is most commonly used to add containers.

Write code manually in the Code Editor

You can use the UI Editor or Structure window to add

containers more quickly and easily, but you can type in your

own code (the hard way).

What Containers Should Be Used Where?

JFrame and JPanel

You should always start a Java application with a JFrame as the outside container (the window). Then you access its contentPane and place a JPanel directly inside it. The JPanel serves as your main container to partition the screen into different areas.

JScrollPane

This is a special container used whenever you want a scrollbar to traverse large quantities of data or images that will not fit on the available screen size.

JTabbedPane

Allows you to create a tab container in your program and then add a JPanel for each tab page.

JSplitPane

Contains two JPanels that share the area defined for the JSplitPane. JSplitPanes can divide a given area in either a horizontal or vertical direction.

JDesktopPane

If you want multiple independent or floating windows in your program, you can add a parent container called a JDesktopPane.

Then add child JInternalFrame containers for each window you wish to display.

Container Layout Example

Here is an example for a standard master-detail application to interact with the LOCATIONS (master) and DEPARTMENTS (detail) tables.

• The Frame Class

The Panel Class

JPanel (BorderLayout)

The Master Class

jPanel (BorderLayout)

The Detail Class

jPanel (BorderLayout)

Modifying Swing Components

The Property Inspector is usually used to modify the most common properties of a Swing component. You are modifying your code when you make changes to the Property Inspector.

If you change the border property of a button to be a raised bevel border, the following code is generated:

jButton1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));

And the following import statements are also added:

import javax.swing.BorderFactory;

import javax.swing.border.BevelBorder;

You can also directly modify the component by calling the appropriate method. JDeveloper's Code Sight allows you to see a list of methods available for each component. It is activated by keying in the name of the component followed by a ".". After a slight pause the list will appear. The following is an example of a JButton component:

[pic]

Component objects can be manipulated without restriction at runtime.

Also, objects are created at runtime without restriction.

Defining Events

Swing components are JavaBean classes that include the ability to fire events. Events allow you to execute code based on user interaction with your application. An example might be a mouseClicked event for button component.

Events require two types of code:

• An event listener to detect the event and to call an event handler

• A method (event handler) with code that executes when the

listener determines that the event has occurred.

JDeveloper's Property Inspector's Events tab below is the easiest way of specifying events for a component.

[pic]

You select the event in the Events tab and click the "..." button for that event. If you had selected the mouseClicked event the following dialog box will appear:

[pic]

You can rename the method stub if necessary. When you click OK, the dialog will create both a listener and the code stub (empty method) for you, and place the cursor in the code stub so that you can add the event code.

[pic]

Swing Component Methods and Events

JButton methods:

setText()

Sets the button's text.

xxx.setText("Xxxx");

setMnemonic()

Sets the keyboard mnemonic for the button. The mnemonic is the key which when combined with the Alt key will activate this button if focus is contained somewhere within this button's ancestor window. A mnemonic must correspond to a single key on the keyboard and should be specified using one of the VK_XXX keycodes defined in java.awt.event.KeyEvent. Mnemonics are case-insensitive, therefore a key event with the corresponding keycode would cause the button to be activated whether or not the Shift modifier was pressed. If the character defined by the mnemonic is found within the button's label string, the first occurrence of it will be underlined to indicate the mnemonic to the user.

xxx.setMnemonic(KeyEvent.VK_I);

xxx.setMnemonic('I'); // now absolete

JButton Events:

keyPressed(KeyEvent e)

private void xxx_keyPressed(KeyEvent e) {

if ( e.getKeyCode() == KeyEvent.VK_I ||

e.getKeyCode() == KeyEvent.VK_ENTER )

frmInquire = new InquireForm(workout);

}

mouseClicked(MouseEvent e)

private void xxx_mouseClicked(MouseEvent e) {

frmInquire = new InquireForm(workout);

}

The KeyEvent Class

Generated when keyboard input occurs. There are three types of key events, identified by the KEY_PRESSED, KEY_RELEASED, and KEY_TYPED integer constants. The VK constants specify virtual key codes and are independent of any modifiers (control, shift, or alt.)

|KeyEvent Class Field Summary (constants) |

|static char |CHAR_UNDEFINED An invalid character. |

|static int |KEY_PRESSED The "key pressed" event. |

|static int |KEY_RELEASED       The "key released" event. |

|static int |KEY_TYPED           The "key typed" event. |

|static int |VK_0 thru VK_9      The ASCII '0' thru '9' (0x30 - 0x39) |

|static int |VK_A thru VK_Z      The ASCII 'A' thru 'Z' (0x41 - 0x5A) |

|static int |VK_ALT |

|static int |VK_AMPERSAND |

|static int |VK_ASTERISK |

|static int |VK_AT           Constant for the "@" key. |

|static int |VK_BACK_QUOTE            |

|static int |VK_BACK_SLASH            |

| static int |VK_BACK_SPACE            |

|static int |VK_BRACELEFT            |

|static int |VK_BRACERIGHT            |

|static int |VK_CANCEL            |

|static int |VK_CAPS_LOCK |

|static int |VK_CLEAR             |

|static int |VK_CLOSE_BRACKET             |

|static int |VK_COLON           Constant for the ":" key. |

|static int |VK_COMMA             |

|KeyEvent Class Field Summary (constants) - continued |

|static int |VK_CONTROL            |

|static int |VK_COPY             |

|static int |VK_CUT             |

|static int |VK_DECIMAL            |

|static int |VK_DELETE             |

|static int |VK_DIVIDE            |

|static int |VK_DOLLAR           Constant for the "$" key. |

|static int |VK_DOWN Constant for non-numpad down arrow key. |

|static int |VK_END             |

|static int |VK_ENTER            |

|static int |VK_EQUALS            |

|static int |VK_ESCAPE            |

|static int |VK_EXCLAMATION_MARK   Constant for the "!" key. |

|static int |VK_F1 to VK_F24     Constant for F1 thru F24 function keys. |

|static int |VK_GREATER             |

|static int |VK_HELP             |

|static int |VK_HOME             |

|static int |VK_INSERT            |

|static int |VK_KP_DOWN  Constant for num keypad down arrow key. |

|static int |VK_KP_LEFT   Constant for num keypad left arrow key. |

|static int |VK_KP_RIGHT  Constant for num keypad right arrow key. |

|static int |VK_KP_UP   Constant for num keypad up arrow key. |

|static int |VK_LEFT  Constant for non-numpad left arrow key. |

|static int |VK_LEFT_PARENTHESIS    Constant for the "(" key. |

|static int |VK_LESS             |

|static int |VK_MINUS           Constant for the "-" key. |

|static int |VK_MULTIPLY             |

|static int |VK_NUM_LOCK          |

|KeyEvent Class Field Summary (constants) - continued |

|static int |VK_NUMBER_SIGN    Constant for the "#" key. |

|static int |VK_NUMPAD0 thru VK_NUMPAD9             |

|static int |VK_OPEN_BRACKET             |

|static int |VK_PAGE_DOWN             |

|static int |VK_PAGE_UP             |

|static int |VK_PASTE             |

|static int |VK_PAUSE            |

|static int |VK_PERIOD          |

|static int |VK_PLUS           Constant for the "+" key. |

|static int |VK_PRINTSCREEN             |

|static int |VK_QUOTE            |

|static int |VK_QUOTEDBL             |

|static int |VK_RIGHT   Constant for the non-numpad right arrow key. |

|static int |VK_RIGHT_PARENTHESIS     Constant for the ")" key. |

|static int |VK_SCROLL_LOCK             |

|static int |VK_SEMICOLON             |

|static int |VK_SEPARATOR    Constant for the Numpad Separator key. |

|static int |VK_SHIFT             |

|static int |VK_SLASH            |

|static int |VK_SPACE            |

|static int |VK_STOP             |

|static int |VK_SUBTRACT             |

|static int |VK_TAB             |

|static int |VK_UNDERSCORE       Constant for the "_" key. |

|static int |VK_UNDO             |

|static int |VK_UP   Constant for the non-numpad up arrow key. |

Swing Component Methods and Events

JTextField methods:

setText()

Sets the text field's text.

xxx.setText("Xxxx");

getText()

Returns the text field's text as a String.

str = xxx.getText();

setColumns()

Sets the width of the text field to a number of columns.

xxx.setColumns(12);

selectAll()

Selects all the text in the JTextField.

xxx.selectAll();

JTextField events:

focusGained(FocusEvent e)

Invoked when a component gains the keyboard focus.

private void xxx_focusGained(FocusEvent e) {

.

}

focusLost(FocusEvent e)

Invoked when a component loses the keyboard focus.

private void xxx_focusLost(FocusEvent e) {

.

}

WindowAdaptor Class

An abstract adapter class for receiving window events. The methods in this class are empty. This class exists as convenience for creating listener objects.

public void windowOpened(WindowEvent e)

Invoked when a window has been opened.

public void windowClosing(WindowEvent e)

Invoked when a window is in the process of being closed. The

close operation can be overridden at this point.

public void windowClosed(WindowEvent e)

Invoked when a window has been closed.

public void windowIconified(WindowEvent e)

Invoked when a window is iconified.

public void windowDeiconified(WindowEvent e)

Invoked when a window is de-iconified.

public void windowActivated(WindowEvent e)

Invoked when a window is activated.

public void windowDeactivated(WindowEvent e)

Invoked when a window is de-activated.

public void windowStateChanged(WindowEvent e)

Invoked when a window state is changed.

public void windowGainedFocus(WindowEvent e)

Invoked when the Window is set to be the focused Window,

which means that the Window, or one of its subcomponents, will

receive keyboard events.

public void windowLostFocus(WindowEvent e)

Invoked when the Window is no longer the focused Window,

which means that keyboard events will no longer be delivered to

the Window or any of its subcomponents.

StringTokenizer Class

The string tokenizer class allows an application to break a string into tokens. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments. The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a per-token basis.

Formats:

StringTokenizer( String str, String delim );

StringTokenizer( String str, String delim, boolean returndelims );

An instance of StringTokenizer behaves in one of two ways, depending on whether it was created with the returnDelims flag having the value true or false:

• If the flag is false, delimiter characters serve to separate tokens. A token is a sequence of consecutive characters that are not delimiters.

• If the flag is true, delimiter characters are themselves considered to be tokens. A token is thus either one delimiter character, or a sequence of consecutive characters that are not delimiters.

Example:

// define a StringTokenizer variable

static private StringTokenizer stok;

private String parsedData;

private String dataIn = "abc?1234?xyz?jkl";

// create an instance of StringTokenizer passing the variable

// containing the data to be parsed and the delimiter character(s)

stok = new StringTokenizer( dataIn, "?" );

// loop until no more delimiter characters are found

while ( stok.hasMoreTokens() )

parsedData = parsedData + stok.nextToken();

After execution, parsedData will contain "abc1234xyzjkl".

Methods of the StringTokenizer class are:

int countTokens()

Calculates the number of times that this tokenizer's nextToken

method can be called before it generates an exception.

boolean hasMoreElements()

        Returns the same value as the hasMoreTokens method.

boolean hasMoreTokens()  

Tests if there are more tokens available from this tokenizer's

string.

Object nextElement()

Returns the same value as the nextToken method, except that

its declared return value is Object rather than a String. Throws a

NoSuchElementException if there are no more tokens in this

tokenizer's string.

String nextToken()

Returns the next token from this string tokenizer. Throws a

NoSuchElementException if there are no more tokens in this

tokenizer's string.

String nextToken(String delim)

Returns the next token in this string tokenizer's string. Throws a

NoSuchElementException if there are no more tokens in this

tokenizer's string.

-----------------------

JPanel (BorderLayout)

JFrame (Content Pane – GridLayout)

has menu bar added to it

Panel Class – Data Panel

Status Bar

Detailed JScrollPane

Can be removed

JScrollPane

Detail Class

Master Class

JScrollPane

Navigation Bar

LOCATIONS

LOCATIONS

FIELDS

LOCATIONS

FIELDS

JPanel

(GridBagLayout)

jPanel(BorderLayout

jScrollPane

DEPT

JTable

Navigation Bar

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download