![]() ![]() ![]() ![]() |
| Topics this page:
|
|
| ![]()
| ![]() ![]() ![]() |
| ![]() ![]() ![]() |
|
![]() ![]() |
A JPanel does not contain a title bar, menu bar, or border with resizing corners. Output on an applet is drawn on a panel.
JWindow does not contain a title bar, so they are often used to build Splash Screens.
When a JDialog pop-up windows is defined with a title bar, the JFrame or a JApplet that owns them are also specified:
A Canvas encapsulates a blank window for drawing.
| ![]() ![]() ![]() |
| ![]() ![]() ![]() |
|
Event Type/Class | Trigger Source | JFC | Java AWT Event | AWT # |
Javascript Event | Supported by Javascript object |
---|---|---|---|---|---|---|
Low-Level Container Event | document or image is finished loading. | ADDED | LOAD_FILE | 1002 | onLoad | Image, Window |
user exits a document. | REMOVED ?? | SAVE_FILE | 1003 | onUnload | Window | |
an error is detected while loading an image | n/a | - | - | onError | Image, Window | |
Low-Level KeyEvent KEY_ | user presses a key on the keyboard. | _PRESS _ACTION | 401 403 | onKeyDown onKeyPress | Document, Image, Link, textarea | |
user releases a key on the keyboard. | _RELEASE _ACTION_RELEASE | 402 404 | onKeyUp | |||
Low-Level MouseEvent MOUSE_ | user clicks the mouse. | _CLICKED | _DOWN | 501 | onMouseDown | Document, Button, Link |
user moves a mouse off the element. | _MOVED | _MOVE | 503 | - | - | |
user moves a mouse off the element. | _RELEASED | _UP | 502 | onMouseOut | Layer, Link, Image | |
user moves a mouse over an element. | _ENTERED | _ENTER | 504 | onMouseOver | ||
user releases the mouse button. | _EXITED | _EXIT | 505 | onMouseUp | Document, Image, button elements, Link | |
an object (such as a shortcut or file) is dragged and dropped into the browser window. | _DRAGGED | _DRAG | 506 | onDragDrop | Window elements | |
Low-Level MouseWheel Event WHEEL_ | user moves a mouse wheel. | _BLOCK_SCROLL or _UNIT_SCROLL | (JDK 1.4+) | - | - | - |
Low-Level FocusEvent (Java Object or Javascript Element) | element is given input focus. | GAINED | GOT_FOCUS | 1004 | onFocus | Button, Checkbox, FileUpload, Password, Radio, Reset, Select, Submit, Text, textarea, Window |
element loses input focus. | LOST | LOST_FOCUS | 1005 | onBlur | Button, Checkbox, FileUpload, Layer, Password, Radio, Reset, Select, Submit, Text, textarea, Window | |
Low-Level Window Event WINDOW_ | JFrame | _ACTIVATED | - | - | - | - |
_OPENED | _EXPOSE | 202 | - | - | ||
_CLOSED | - | - | - | - | ||
_CLOSING | - | - | - | - | ||
_MOVED | _MOVE | 205 | onMove | Window | ||
_STATE _CHANGED | (JDK 1.4+) | - | - | - | ||
_DEACTIVATED | _DESTROY | 201 | onAbort | Image | ||
_ICONIFIED | _ICONIFY | 203 | - | - | ||
_DEICONIFIED | _DEICONIFY | 204 | - | - | ||
user drags a Window border. | ??? | - | - | onResize | Window | |
Low-Level Input Text Event | JTextField edit box | - | - | - | - | |
Low-Level Paint Event | - | - | - | - | - | |
Semantic Item Event | JCheckBox. JList | LIST_SELECT LIST_DESELECT | 701 702 | - | - | |
Semantic Adjustment Event | use manipulates scrollbars | SCROLL_LINE_UP SCROLL_LINE_DOWN SCROLL_PAGE_UP SCROLL_PAGE_DOWN SCROLL_ABSOLUTE | 601 602 603 604 605 | onSubmit | Form | |
Semantic Action Event | user selects or deslects an item or events text and moves input focus to another element. | JButton | - | - | onChange | Select, text, input elements |
user clicks once (performs a single click). | - | - | onClick | Button, Document, Checkbox, Link, Radio, Reset, Submit | ||
user clicks twice rapidly (IE only), generally to invoke an entirely new application program. | ACTION_EVENT | 1001 | onDblClick | Document, Image, button elements, Link | ||
user selects some of the text. | - | - | onSelect | Text, textarea | ||
user clicks a form's reset button. | n/a | - | - | onReset | Form | |
user clicks a form's submit element. | n/a | - | - | onSubmit | Form | |
Semantic Component Event | - | position MOVED, RESIZED, visibility HIDDEN, SHOWN | - | - | - |
Semantic events support the illusion that a graphical user interface (GUI) component on the screen is actually a mechanical input device. For example, the Button class is described in terms of pushing a button, when in reality; the mouse pointer is inside the button's territory of the screen.
|
Activity | Java Method |
---|---|
button's surface area is entered or exited | mouseEntered or mouseExited |
button is pressed | mousePressed |
button is pressed and released | mouseClicked |
button is pressed and moved | mouseDragged |
import java.awt.*; import java.awt.event.*; import javax.swing.*;public class MouseTracker extends JFrame implements MouseListener, MouseMotionListener { private JLabel statusBar; public myMouseTracker() { super( "Mouse Events Demo" ); statusBar = new JLabel(); getContentPane().add( statusBar, BorderLayout.SOUTH ); // application listens to its own mouse events: addMouseListener( this ); addMouseMotionListener( this ); setSize( 275, 100 ); show(); } // MouseListener event handlers: public void mouseClicked( MouseEvent e ) { statusBar.setText( "Clicked at [" + e.getX() + ", " + e.getY() + "]" ); repaint(); // redraw screen: calls paintComponent() } } }
| ![]() ![]() ![]() |
| ![]() ![]() ![]() |
|
java.awt. | Layout Manager | Description |
---|---|---|
LayoutManager |
Flow |
is the simplest — the default — layout manager.
It lays the components from left to right starting at the top.
When it runs out of space on one row, it goes to the next row.
By default the Flow layout leaves a gap of five pixels vertically and horizontally
between components, and also aligns components in the center of the window.
This behavior is controlled with the FlowLayout constructor:
FlowLayout.LEADING FlowLayout.LEFT FlowLayout.RIGHT FlowLayout.TRAILING FlowLayout always uses a component's preferred size. |
LayoutManager2 |
Border |
is the default for frames,
honors components' preferred size in directional regions: north (top), south (bottom), east (left), west (right).
CENTER is what's left over.
|
LayoutManager |
Box | |
LayoutManager2 |
Card | lays the components on different cards, and displays only one card at a time over time, so users can cycle through them using methods first, next, previous. |
LayoutManager |
Grid |
lays out the components on a grid of evenly spaced rows
and columns going from left to right and going from top to bottom.
A component's preferred size is ignored.
|
LayoutManager2 |
GridBag | provides maximum control over the layout of components and their sizes. Similar to the Grid structure, but the size of its 10 cells (such as NORTHWEST) can vary -- possibly occupying more than one cell (by spcifying GridBagConstraints object fields gridwidth and gridheight). Programming its GriBagConstraint statements can be tedious, though. To control stretchiness, specify weightx in only one component in each column and weighty only in one component in each row. |
It is futile to call setBounds() on a component because layout managers call it as well.
Public double type arrays define the minimum RowWeights and columnWeights.
To use no layout manager, call myContainer.setLayout(null).
|
Class | Description | Usage in 1.4+ |
---|---|---|
FlowLayout, etc. | Layout managers | same |
AWTEvent | Wrapper for (encapsulates) AWT events | |
AWTEventMulticaster | Dispatches events to several listeners | |
Button | Push button control component | JButton |
Canvas | Blank semantics-free window | |
Checkbox | Component | |
CheckboxGroup | Groups Checkbox components | |
CheckboxMenuItem | On/off menu item | |
Choice | Popup list | |
Color | Change colors | |
Component | Abstract superclass for components | |
Container | Subclass of Component class for other container type classes | |
Dialog | Creates a Dialog (pop-up) window | |
Dimension | width and height of an object, e.g. the applet window | |
Event | Wrapper for (encapsulates) events | |
EventQueue | Queues events | |
FileDialog | File selection box window | |
Font | Encapsulates type fonts | |
FontMetrics | Font related information to display text | |
Frame | Window with menu bar, title and resizing corners | JFrame |
Graphics | Encapsulates graphics context used to display on a window | |
GraphicsDevice | Screen or printer | |
Image | Wrapper for images | |
Label | Component | |
List | List box component | |
Menu | Pull down menu | |
Panel | Container | |
Point | Wrapper for x & y coordinates | |
Polygon | Wrapper for a polygon | |
PopupMenu | Wrapper for popup menu | |
Scrollbar | Component | |
SystemColor | Standard system colors for windows, text, etc. | |
textarea | Component | |
Window | Window with no frame, no menu bar, no title |
Listed alphabetically:
When programming with AWT, you can place a Panel object in a Frame object and place other components on the Panel object if you wish, or you can simply place other components directly on the viewable area of the Frame object.
|
Component | Description |
---|---|
JLabel | Area in which uneditable text or icons can be displayed, such as “tool tip” text appearing when a user mouses over an item. |
JTextField | Area that accepts keyboard input (text) from users. |
JPasswordField | Area that accepts keyboard input (text) from users and hides them. |
JButton | Area that triggers an event when clicked. |
JCheckBox | Area that logs whether or not it's checked. |
JComboBox | Dropdown list of selectable items. |
JList | List of selectable and executable items. (uses the MVS approach) |
JPanel | Container in which other components can be placed. |
Use setVisible( true ); instead of the deprecated show().
The viewable area of a Frame or a JFrame is the bounds minus the insets. Insets are used to account for the space covered by the borders and the values of the insets on all four sides are available by invoking the method named getInsets() on the object.
Objects (components) cannot be placed directly on the viewable area of a JFrame object.
To place components in the JFrame, we invoke the add() method of getContentPane(), which deals with the JRootPane container object automatically placed in the JFrame object. JRootPane owns the entire viewable area of the JFrame object. An object of type JLayeredPane An object of type JPanel (the glassPane) because it is usually transparent. JFrame object JRootPane 1. JPanel (the transparent "glassPane") automatically the first child So this is good for pop-up messages that should go on top. 2. layeredPane a. it contains a menuBar positioned at the upper edge b. contentPane, which has a BorderLayout manager by default. Components drawn on layers with high (algebraically signed) numbers are painted on top of components on layers with smaller (algebraically signed) numbers. The layer position for the contentPane is effectively layer number -30,000. -29,999 (we will place one there in our sample program later).
Swing Sightings demo Swing applications in use.
Swing components are not inherantly thread-safe. So use javax.swing.SwingUtilities.invokeLater() and invokeAndWait() in code called by several threads, such as cusom ListModels and TreeModels responding to non-local or non-GUI events.
|
JOptionPane. dialog type literal | Icon |
---|---|
PLAIN_MESSAGE | |
INFORMATION_MESSAGE | ![]() |
WARNING_MESSAGE | ![]() |
ERROR_MESSAGE | ![]() |
QUESTION_MESSAGE | ![]() |
JOptionPane.showMessageDialog( null, "Aloha!", JOptionPane.PLAIN_MESSAGE );
Avoid unnecessary user interaction. Don't require user response unless it is important to the user. So orovide a way for each user to customize how a message is displayed. Displaying a message within the application (without requiring the user to click OK).
A truly modal dialog window not only maintains focus but also prevents users from accessing the main window while it's showing. This kind of window is a good way to display user preference settings as well as to show details and user controls for summary data displayed on the main page.
| ![]() ![]() ![]() |
|
| ![]() ![]() ![]() |
|
| ![]() ![]() ![]() |
Related:
![]()
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page ![]() Thank you! |