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!
SUBSCRIBE VIA eMAIL
Recent Posts
Archives
- December 2008 (5)
- November 2008 (15)
- October 2008 (17)
- September 2008 (9)
- August 2008 (12)
- July 2008 (19)
- June 2008 (22)
- May 2008 (17)
- April 2008 (2)
Categories
- java.sql (19)
- Examples (18)
- INTRODUCTION (9)
- JAVA APPLET (9)
- java.awt (8)
- java.net (8)
- java.beans (7)
- JAVA String Utility (6)
- Arrays (4)
- java.math (3)
- java.util.regex (3)
- Sort (2)
- Swing (2)
- java.security (2)
- java.util.zip (2)
- Catching Exceptions (1)
- Classes and Objects (1)
- Core Java Programs-part 1 (1)
- Core Java Programs-part 2 (1)
- Core Java Programs-part3 (1)
- Criticism of Java programming language (1)
- Falling Letters (1)
- File I/O and Streams (1)
- Fun With Letters and Words (1)
- Get current working directory (1)
- How do I convert String to Date object? (1)
- How do I convert string into InputStream? (1)
- How do i calculate directory size? (1)
- How to make executable jar files in JDK1.3.1? (1)
- Interfaces (1)
- JAVA Date Utility (1)
- Java Arithmetic Operators (1)
- Java Assignment Operators (1)
- Java Boolean Operators (1)
- Java Command Line Arguments (1)
- Java Comments (1)
- Java Conditional Operators (1)
- Java Data and Variables (1)
- Java Hello World Program (1)
- Java If-Else Statement (1)
- Java Increment and Decrement Operators (1)
- Java Loops (while (1)
- Java Relational Operators (1)
- Java Variables and Arithmetic Expressions (1)
- Java Virtual Machine (1)
- Know the current position of cursor (1)
- Letters (1)
- Methods (Includes Recursive Methods) (1)
- Rotating Lines (1)
- Send an email with attachment (1)
- Use for..each in Java (1)
- What is Autoboxing? (1)
- Write text file (1)
- connection to database (1)
- do-while and for loops) (1)


0 comments:
Post a Comment