JAVA-MANI.BLOGSPOT.COM
Friday, June 27, 2008
A Graphical User Interface (GUI) is a visual paradigm which allows a user to communicate with a program in an intuitive way. Its main features are widgets (aka controls) and event driven activities. Clients expect a graphical interface in an application. The next few tutorials will introduce Java's GUI packages as well as some layout design consideraions.

Java has two GUI packages, the original Abstract Windows Toolkit (AWT) and the newer Swing. AWT uses the native operating system's window routines and therefore the visual effect is dependent on the run-time system platform. But this is contrary to the concept of having a virtual model. Swing allows three modes: a unified 'Java' look and feel [the default], the native platform look, or a specific platform's look. Swing is built on the original objects and framework of AWT. Swing components have the prefix J to distinguish them from the original AWT ones (eg JFrame instead of Frame). To include Swing components and methods in your project you must import the java.awt.*, java.awt.event.*, and javax.swing.* packages.

Containers, Frames and Content Panes

Containers are widgets (GUI controls) that are used to hold and group other widgets such as text fields and checkboxes. Displayable frames are top-level containers which interface to the operating system's window manager. Non-displaying content panes are intermediate containers which organize the layout structure when multiple controls are being used.

JWindow is an unadorned container that has been superceded for the most part by JDialog. However it does provide a useful container for a splash screen.

Methods common to many different containers or widgets include: add(), requestFocus(), setToolTipText().

JFrame and JPanel

JFrame is the most commonly used top-level container. It adds basic functionality such as minimize, maximize, close, title and border to basic frames and windows. Some important JFrame methods are: setBounds(x,y,w,h), setLocation(x,y), setSize(w,h), setResizable(bool), setTitle(str), setVisible(bool), isResizable() and getTitle(). The setDefaultCloseOperation(constant) method controls the action that occurs when the close icon is clicked. Normally the constant used is JFrame.EXIT_ON_CLOSE.

JPanel is the most commonly used content pane. An instance of the pane is created and then added to a frame. The add() method allows widgets (GUI components) to be added to the pane. The way they are added is controlled by the current layout manager.

The following is a simple template that creates a JFrame container class using inheritance. The created subclass then adds a JPanel. This custom class will form the basis of many of our GUI examples.

Simple Dialog Boxes

Dialogs are short messages or information screens, confirmation boxes, and input prompts for simple string information which appear in a popup windows. Swing uses the JOptionPane class to provide predefined methods for each type of dialog. The JDialog class can be used to create customized dialog boxes. It provides a simple unadorned window.

Each JOptionPane method has a first parameter that points to a parent (ie. window that it appears in) or null (default to the current window). The second parameter is the message or prompt to be displayed. New instances of JOptionPane are not normally generated.

showMessageDialog() has two more optional parameters to set a dialog title and to select the dialog's icon. The dialog has a single 'ok' button for completion and no data is returned by this method.

JOptionPane.showMessageDialog(null,"This is just a message",
"Message Dialog",JOptionPane.PLAIN_MESSAGE);

showConfirmDialog() has three more optional parameters to set a dialog title, alter the button display, and select the dialogs icon. By default there are three buttons 'Yes', 'No' and Cancel for dialog completion. The returned value is one of JOptionPane.YES_OPTION, JOptionPane.NO_OPTION or JOptionPane.CANCEL_OPTION.

pressed = JOptionPane.showConfirmBox(null,"Everything aok");
if (pressed==JOptionPane.YES_OPTION)
{
// do the action for confirmation
}

showInputDialog() has two more optional parameters to set a dialog title and to select the dialog's icon. There is both an 'ok' button and a 'cancel' button for dialog completion. Any information typed into the entry box is returned as a string.

user_data = JOptionPane.showInputDialog(null,"What's your name");

The list of icon types that can be displayed (by predefined constant) contains ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE, and WARNING_MESSAGE.

NOTE: Current versions of swing do not allow button selection by first letter (eg. n for no) which is enabled in all windows type GUI's. This is a major pain for user clients who must either use the tab key or reach for their mouse... Blaah!


0 comments:

SUBSCRIBE VIA eMAIL

Enter your email address:

Delivered by FeedBurner

Recent Posts

Firefox 3

Counter

internet companies

Live Traffic Map

Subscribe Now